@@ -9364,6 +9364,19 @@ def convert_foreign_to_neo(infile, outfile=None, formatspecs=__file_format_multi
93649364 intmp = InFileToArray (infile , 0 , 0 , False , True , False , formatspecs , False , False )
93659365 return RePackArchiveFile (intmp , outfile , "auto" , compression , False , compression_level , compressionlistalt , False , 0 , 0 , checksumtypes , False , [], {}, formatspecs , False , False , returnfp )
93669366
9367+ def detect_cwd (ftp , file_dir ):
9368+ """
9369+ Test whether cwd into file_dir works. Returns True if it does,
9370+ False if not (so absolute paths should be used).
9371+ """
9372+ if not file_dir or file_dir in ("/" , "" ):
9373+ return False # nothing to cwd into
9374+ try :
9375+ ftp .cwd (file_dir )
9376+ return True
9377+ except all_errors :
9378+ return False
9379+
93679380def download_file_from_ftp_file (url ):
93689381 urlparts = urlparse (url )
93699382 file_name = os .path .basename (urlparts .path )
@@ -9402,26 +9415,46 @@ def download_file_from_ftp_file(url):
94029415 except socket .timeout :
94039416 log .info ("Error With URL " + url )
94049417 return False
9418+ if (urlparts .scheme == "ftps" or isinstance (ftp , FTP_TLS )):
9419+ try :
9420+ ftp .auth ()
9421+ except all_errors :
9422+ pass
94059423 ftp .login (urlparts .username , urlparts .password )
94069424 if (urlparts .scheme == "ftps" or isinstance (ftp , FTP_TLS )):
9407- ftp .prot_p ()
9425+ try :
9426+ ftp .prot_p ()
9427+ except all_errors :
9428+ ftp .prot_c ()
9429+ # UTF-8 filenames if supported
9430+ try :
9431+ ftp .sendcmd ("OPTS UTF8 ON" )
9432+ ftp .encoding = "utf-8"
9433+ except all_errors :
9434+ pass
9435+ is_cwd_allowed = detect_cwd (ftp , file_dir )
9436+ ftpfile = MkTempFile ()
94089437 # Try EPSV first, then fall back
94099438 try :
94109439 ftp .force_epsv = True
94119440 ftp .sendcmd ("EPSV" ) # request extended passive
9412- ftp .retrlines ("LIST" , callback = lambda line : None )
9441+ if (is_cwd_allowed ):
9442+ ftp .retrbinary ("RETR " + file_name , ftpfile .write )
9443+ else :
9444+ ftp .retrbinary ("RETR " + urlparts .path , ftpfile .write )
94139445 except all_errors :
94149446 try :
94159447 ftp .set_pasv (True )
9416- ftp .retrlines ("LIST" , callback = lambda line : None )
9448+ if (is_cwd_allowed ):
9449+ ftp .retrbinary ("RETR " + file_name , ftpfile .write )
9450+ else :
9451+ ftp .retrbinary ("RETR " + urlparts .path , ftpfile .write )
94179452 except all_errors :
94189453 ftp .set_pasv (False )
9419- ftp .retrlines ("LIST" , callback = lambda line : None )
9420- ftpfile = MkTempFile ()
9421- if file_dir and file_dir not in ("/" , "" ):
9422- ftp .cwd (file_dir )
9423- ftp .retrbinary ("RETR " + file_name , ftpfile .write )
9424- #ftp.retrbinary("RETR "+urlparts.path, ftpfile.write)
9454+ if (is_cwd_allowed ):
9455+ ftp .retrbinary ("RETR " + file_name , ftpfile .write )
9456+ else :
9457+ ftp .retrbinary ("RETR " + urlparts .path , ftpfile .write )
94259458 ftp .close ()
94269459 ftpfile .seek (0 , 0 )
94279460 return ftpfile
@@ -9480,25 +9513,46 @@ def upload_file_to_ftp_file(ftpfile, url):
94809513 except socket .timeout :
94819514 log .info ("Error With URL " + url )
94829515 return False
9516+ if (urlparts .scheme == "ftps" or isinstance (ftp , FTP_TLS )):
9517+ try :
9518+ ftp .auth ()
9519+ except all_errors :
9520+ pass
94839521 ftp .login (urlparts .username , urlparts .password )
94849522 if (urlparts .scheme == "ftps" or isinstance (ftp , FTP_TLS )):
9485- ftp .prot_p ()
9523+ try :
9524+ ftp .prot_p ()
9525+ except all_errors :
9526+ ftp .prot_c ()
9527+ # UTF-8 filenames if supported
9528+ try :
9529+ ftp .sendcmd ("OPTS UTF8 ON" )
9530+ ftp .encoding = "utf-8"
9531+ except all_errors :
9532+ pass
9533+ is_cwd_allowed = detect_cwd (ftp , file_dir )
9534+ ftpfile .seek (0 , 0 )
94869535 # Try EPSV first, then fall back
94879536 try :
94889537 ftp .force_epsv = True
94899538 ftp .sendcmd ("EPSV" ) # request extended passive
9490- ftp .retrlines ("LIST" , callback = lambda line : None )
9539+ if (is_cwd_allowed ):
9540+ ftp .storbinary ("STOR " + file_name , ftpfile )
9541+ else :
9542+ ftp .storbinary ("STOR " + urlparts .path , ftpfile )
94919543 except all_errors :
94929544 try :
94939545 ftp .set_pasv (True )
9494- ftp .retrlines ("LIST" , callback = lambda line : None )
9546+ if (is_cwd_allowed ):
9547+ ftp .storbinary ("STOR " + file_name , ftpfile )
9548+ else :
9549+ ftp .storbinary ("STOR " + urlparts .path , ftpfile )
94959550 except all_errors :
94969551 ftp .set_pasv (False )
9497- ftp .retrlines ("LIST" , callback = lambda line : None )
9498- if file_dir and file_dir not in ("/" , "" ):
9499- ftp .cwd (file_dir )
9500- ftp .storbinary ("STOR " + file_name , ftpfile )
9501- #ftp.storbinary("STOR "+urlparts.path, ftpfile)
9552+ if (is_cwd_allowed ):
9553+ ftp .storbinary ("STOR " + file_name , ftpfile )
9554+ else :
9555+ ftp .storbinary ("STOR " + urlparts .path , ftpfile )
95029556 ftp .close ()
95039557 ftpfile .seek (0 , 0 )
95049558 return ftpfile
0 commit comments