@@ -9363,6 +9363,19 @@ def convert_foreign_to_neo(infile, outfile=None, formatspecs=__file_format_multi
93639363 intmp = InFileToArray (infile , 0 , 0 , False , True , False , formatspecs , False , False )
93649364 return RePackArchiveFile (intmp , outfile , "auto" , compression , False , compression_level , compressionlistalt , False , 0 , 0 , checksumtypes , False , [], {}, formatspecs , False , False , returnfp )
93659365
9366+ def detect_cwd (ftp , file_dir ):
9367+ """
9368+ Test whether cwd into file_dir works. Returns True if it does,
9369+ False if not (so absolute paths should be used).
9370+ """
9371+ if not file_dir or file_dir in ("/" , "" ):
9372+ return False # nothing to cwd into
9373+ try :
9374+ ftp .cwd (file_dir )
9375+ return True
9376+ except all_errors :
9377+ return False
9378+
93669379def download_file_from_ftp_file (url ):
93679380 urlparts = urlparse (url )
93689381 file_name = os .path .basename (urlparts .path )
@@ -9401,26 +9414,46 @@ def download_file_from_ftp_file(url):
94019414 except socket .timeout :
94029415 log .info ("Error With URL " + url )
94039416 return False
9417+ if (urlparts .scheme == "ftps" or isinstance (ftp , FTP_TLS )):
9418+ try :
9419+ ftp .auth ()
9420+ except all_errors :
9421+ pass
94049422 ftp .login (urlparts .username , urlparts .password )
94059423 if (urlparts .scheme == "ftps" or isinstance (ftp , FTP_TLS )):
9406- ftp .prot_p ()
9424+ try :
9425+ ftp .prot_p ()
9426+ except all_errors :
9427+ ftp .prot_c ()
9428+ # UTF-8 filenames if supported
9429+ try :
9430+ ftp .sendcmd ("OPTS UTF8 ON" )
9431+ ftp .encoding = "utf-8"
9432+ except all_errors :
9433+ pass
9434+ is_cwd_allowed = detect_cwd (ftp , file_dir )
9435+ ftpfile = MkTempFile ()
94079436 # Try EPSV first, then fall back
94089437 try :
94099438 ftp .force_epsv = True
94109439 ftp .sendcmd ("EPSV" ) # request extended passive
9411- ftp .retrlines ("LIST" , callback = lambda line : None )
9440+ if (is_cwd_allowed ):
9441+ ftp .retrbinary ("RETR " + file_name , ftpfile .write )
9442+ else :
9443+ ftp .retrbinary ("RETR " + urlparts .path , ftpfile .write )
94129444 except all_errors :
94139445 try :
94149446 ftp .set_pasv (True )
9415- ftp .retrlines ("LIST" , callback = lambda line : None )
9447+ if (is_cwd_allowed ):
9448+ ftp .retrbinary ("RETR " + file_name , ftpfile .write )
9449+ else :
9450+ ftp .retrbinary ("RETR " + urlparts .path , ftpfile .write )
94169451 except all_errors :
94179452 ftp .set_pasv (False )
9418- ftp .retrlines ("LIST" , callback = lambda line : None )
9419- ftpfile = MkTempFile ()
9420- if file_dir and file_dir not in ("/" , "" ):
9421- ftp .cwd (file_dir )
9422- ftp .retrbinary ("RETR " + file_name , ftpfile .write )
9423- #ftp.retrbinary("RETR "+urlparts.path, ftpfile.write)
9453+ if (is_cwd_allowed ):
9454+ ftp .retrbinary ("RETR " + file_name , ftpfile .write )
9455+ else :
9456+ ftp .retrbinary ("RETR " + urlparts .path , ftpfile .write )
94249457 ftp .close ()
94259458 ftpfile .seek (0 , 0 )
94269459 return ftpfile
@@ -9479,25 +9512,46 @@ def upload_file_to_ftp_file(ftpfile, url):
94799512 except socket .timeout :
94809513 log .info ("Error With URL " + url )
94819514 return False
9515+ if (urlparts .scheme == "ftps" or isinstance (ftp , FTP_TLS )):
9516+ try :
9517+ ftp .auth ()
9518+ except all_errors :
9519+ pass
94829520 ftp .login (urlparts .username , urlparts .password )
94839521 if (urlparts .scheme == "ftps" or isinstance (ftp , FTP_TLS )):
9484- ftp .prot_p ()
9522+ try :
9523+ ftp .prot_p ()
9524+ except all_errors :
9525+ ftp .prot_c ()
9526+ # UTF-8 filenames if supported
9527+ try :
9528+ ftp .sendcmd ("OPTS UTF8 ON" )
9529+ ftp .encoding = "utf-8"
9530+ except all_errors :
9531+ pass
9532+ is_cwd_allowed = detect_cwd (ftp , file_dir )
9533+ ftpfile .seek (0 , 0 )
94859534 # Try EPSV first, then fall back
94869535 try :
94879536 ftp .force_epsv = True
94889537 ftp .sendcmd ("EPSV" ) # request extended passive
9489- ftp .retrlines ("LIST" , callback = lambda line : None )
9538+ if (is_cwd_allowed ):
9539+ ftp .storbinary ("STOR " + file_name , ftpfile )
9540+ else :
9541+ ftp .storbinary ("STOR " + urlparts .path , ftpfile )
94909542 except all_errors :
94919543 try :
94929544 ftp .set_pasv (True )
9493- ftp .retrlines ("LIST" , callback = lambda line : None )
9545+ if (is_cwd_allowed ):
9546+ ftp .storbinary ("STOR " + file_name , ftpfile )
9547+ else :
9548+ ftp .storbinary ("STOR " + urlparts .path , ftpfile )
94949549 except all_errors :
94959550 ftp .set_pasv (False )
9496- ftp .retrlines ("LIST" , callback = lambda line : None )
9497- if file_dir and file_dir not in ("/" , "" ):
9498- ftp .cwd (file_dir )
9499- ftp .storbinary ("STOR " + file_name , ftpfile )
9500- #ftp.storbinary("STOR "+urlparts.path, ftpfile)
9551+ if (is_cwd_allowed ):
9552+ ftp .storbinary ("STOR " + file_name , ftpfile )
9553+ else :
9554+ ftp .storbinary ("STOR " + urlparts .path , ftpfile )
95019555 ftp .close ()
95029556 ftpfile .seek (0 , 0 )
95039557 return ftpfile
0 commit comments