Skip to content

Commit 0529963

Browse files
authored
Add files via upload
1 parent bf13665 commit 0529963

1 file changed

Lines changed: 71 additions & 17 deletions

File tree

pyarchivefile.py

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9352,6 +9352,19 @@ def convert_foreign_to_neo(infile, outfile=None, formatspecs=__file_format_multi
93529352
intmp = InFileToArray(infile, 0, 0, False, True, False, formatspecs, False, False)
93539353
return RePackArchiveFile(intmp, outfile, "auto", compression, False, compression_level, compressionlistalt, False, 0, 0, checksumtypes, False, [], {}, formatspecs, False, False, returnfp)
93549354

9355+
def detect_cwd(ftp, file_dir):
9356+
"""
9357+
Test whether cwd into file_dir works. Returns True if it does,
9358+
False if not (so absolute paths should be used).
9359+
"""
9360+
if not file_dir or file_dir in ("/", ""):
9361+
return False # nothing to cwd into
9362+
try:
9363+
ftp.cwd(file_dir)
9364+
return True
9365+
except all_errors:
9366+
return False
9367+
93559368
def download_file_from_ftp_file(url):
93569369
urlparts = urlparse(url)
93579370
file_name = os.path.basename(urlparts.path)
@@ -9390,26 +9403,46 @@ def download_file_from_ftp_file(url):
93909403
except socket.timeout:
93919404
log.info("Error With URL "+url)
93929405
return False
9406+
if(urlparts.scheme == "ftps" or isinstance(ftp, FTP_TLS)):
9407+
try:
9408+
ftp.auth()
9409+
except all_errors:
9410+
pass
93939411
ftp.login(urlparts.username, urlparts.password)
93949412
if(urlparts.scheme == "ftps" or isinstance(ftp, FTP_TLS)):
9395-
ftp.prot_p()
9413+
try:
9414+
ftp.prot_p()
9415+
except all_errors:
9416+
ftp.prot_c()
9417+
# UTF-8 filenames if supported
9418+
try:
9419+
ftp.sendcmd("OPTS UTF8 ON")
9420+
ftp.encoding = "utf-8"
9421+
except all_errors:
9422+
pass
9423+
is_cwd_allowed = detect_cwd(ftp, file_dir)
9424+
ftpfile = MkTempFile()
93969425
# Try EPSV first, then fall back
93979426
try:
93989427
ftp.force_epsv = True
93999428
ftp.sendcmd("EPSV") # request extended passive
9400-
ftp.retrlines("LIST", callback=lambda line: None)
9429+
if(is_cwd_allowed):
9430+
ftp.retrbinary("RETR "+file_name, ftpfile.write)
9431+
else:
9432+
ftp.retrbinary("RETR "+urlparts.path, ftpfile.write)
94019433
except all_errors:
94029434
try:
94039435
ftp.set_pasv(True)
9404-
ftp.retrlines("LIST", callback=lambda line: None)
9436+
if(is_cwd_allowed):
9437+
ftp.retrbinary("RETR "+file_name, ftpfile.write)
9438+
else:
9439+
ftp.retrbinary("RETR "+urlparts.path, ftpfile.write)
94059440
except all_errors:
94069441
ftp.set_pasv(False)
9407-
ftp.retrlines("LIST", callback=lambda line: None)
9408-
ftpfile = MkTempFile()
9409-
if file_dir and file_dir not in ("/", ""):
9410-
ftp.cwd(file_dir)
9411-
ftp.retrbinary("RETR "+file_name, ftpfile.write)
9412-
#ftp.retrbinary("RETR "+urlparts.path, ftpfile.write)
9442+
if(is_cwd_allowed):
9443+
ftp.retrbinary("RETR "+file_name, ftpfile.write)
9444+
else:
9445+
ftp.retrbinary("RETR "+urlparts.path, ftpfile.write)
94139446
ftp.close()
94149447
ftpfile.seek(0, 0)
94159448
return ftpfile
@@ -9468,25 +9501,46 @@ def upload_file_to_ftp_file(ftpfile, url):
94689501
except socket.timeout:
94699502
log.info("Error With URL "+url)
94709503
return False
9504+
if(urlparts.scheme == "ftps" or isinstance(ftp, FTP_TLS)):
9505+
try:
9506+
ftp.auth()
9507+
except all_errors:
9508+
pass
94719509
ftp.login(urlparts.username, urlparts.password)
94729510
if(urlparts.scheme == "ftps" or isinstance(ftp, FTP_TLS)):
9473-
ftp.prot_p()
9511+
try:
9512+
ftp.prot_p()
9513+
except all_errors:
9514+
ftp.prot_c()
9515+
# UTF-8 filenames if supported
9516+
try:
9517+
ftp.sendcmd("OPTS UTF8 ON")
9518+
ftp.encoding = "utf-8"
9519+
except all_errors:
9520+
pass
9521+
is_cwd_allowed = detect_cwd(ftp, file_dir)
9522+
ftpfile.seek(0, 0)
94749523
# Try EPSV first, then fall back
94759524
try:
94769525
ftp.force_epsv = True
94779526
ftp.sendcmd("EPSV") # request extended passive
9478-
ftp.retrlines("LIST", callback=lambda line: None)
9527+
if(is_cwd_allowed):
9528+
ftp.storbinary("STOR "+file_name, ftpfile)
9529+
else:
9530+
ftp.storbinary("STOR "+urlparts.path, ftpfile)
94799531
except all_errors:
94809532
try:
94819533
ftp.set_pasv(True)
9482-
ftp.retrlines("LIST", callback=lambda line: None)
9534+
if(is_cwd_allowed):
9535+
ftp.storbinary("STOR "+file_name, ftpfile)
9536+
else:
9537+
ftp.storbinary("STOR "+urlparts.path, ftpfile)
94839538
except all_errors:
94849539
ftp.set_pasv(False)
9485-
ftp.retrlines("LIST", callback=lambda line: None)
9486-
if file_dir and file_dir not in ("/", ""):
9487-
ftp.cwd(file_dir)
9488-
ftp.storbinary("STOR "+file_name, ftpfile)
9489-
#ftp.storbinary("STOR "+urlparts.path, ftpfile)
9540+
if(is_cwd_allowed):
9541+
ftp.storbinary("STOR "+file_name, ftpfile)
9542+
else:
9543+
ftp.storbinary("STOR "+urlparts.path, ftpfile)
94909544
ftp.close()
94919545
ftpfile.seek(0, 0)
94929546
return ftpfile

0 commit comments

Comments
 (0)