Skip to content

Commit 23e4ef6

Browse files
authored
Add files via upload
1 parent 85f1b04 commit 23e4ef6

1 file changed

Lines changed: 36 additions & 36 deletions

File tree

pycatfile.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ def to_text(s, encoding="utf-8", errors="ignore"):
102102

103103
# URL Parsing
104104
try:
105-
from urllib.parse import urlparse, urlunparse
105+
from urllib.parse import urlparse, urlunparse, unquote
106106
except ImportError:
107-
from urlparse import urlparse, urlunparse
107+
from urlparse import urlparse, urlunparse, unquote
108108

109109
# Windows-specific setup
110110
if os.name == "nt":
@@ -9379,8 +9379,8 @@ def detect_cwd(ftp, file_dir):
93799379

93809380
def download_file_from_ftp_file(url):
93819381
urlparts = urlparse(url)
9382-
file_name = os.path.basename(urlparts.path)
9383-
file_dir = os.path.dirname(urlparts.path)
9382+
file_name = os.path.basename(unquote(urlparts.path))
9383+
file_dir = os.path.dirname(unquote(urlparts.path))
93849384
if(urlparts.username is not None):
93859385
ftp_username = urlparts.username
93869386
else:
@@ -9420,7 +9420,7 @@ def download_file_from_ftp_file(url):
94209420
ftp.auth()
94219421
except all_errors:
94229422
pass
9423-
ftp.login(urlparts.username, urlparts.password)
9423+
ftp.login(ftp_username, ftp_password)
94249424
if(urlparts.scheme == "ftps" or isinstance(ftp, FTP_TLS)):
94259425
try:
94269426
ftp.prot_p()
@@ -9441,20 +9441,20 @@ def download_file_from_ftp_file(url):
94419441
if(is_cwd_allowed):
94429442
ftp.retrbinary("RETR "+file_name, ftpfile.write)
94439443
else:
9444-
ftp.retrbinary("RETR "+urlparts.path, ftpfile.write)
9444+
ftp.retrbinary("RETR "+unquote(urlparts.path), ftpfile.write)
94459445
except all_errors:
94469446
try:
94479447
ftp.set_pasv(True)
94489448
if(is_cwd_allowed):
94499449
ftp.retrbinary("RETR "+file_name, ftpfile.write)
94509450
else:
9451-
ftp.retrbinary("RETR "+urlparts.path, ftpfile.write)
9451+
ftp.retrbinary("RETR "+unquote(urlparts.path), ftpfile.write)
94529452
except all_errors:
94539453
ftp.set_pasv(False)
94549454
if(is_cwd_allowed):
94559455
ftp.retrbinary("RETR "+file_name, ftpfile.write)
94569456
else:
9457-
ftp.retrbinary("RETR "+urlparts.path, ftpfile.write)
9457+
ftp.retrbinary("RETR "+unquote(urlparts.path), ftpfile.write)
94589458
ftp.close()
94599459
ftpfile.seek(0, 0)
94609460
return ftpfile
@@ -9477,8 +9477,8 @@ def download_file_from_ftps_string(url):
94779477

94789478
def upload_file_to_ftp_file(ftpfile, url):
94799479
urlparts = urlparse(url)
9480-
file_name = os.path.basename(urlparts.path)
9481-
file_dir = os.path.dirname(urlparts.path)
9480+
file_name = os.path.basename(unquote(urlparts.path))
9481+
file_dir = os.path.dirname(unquote(urlparts.path))
94829482
if(urlparts.username is not None):
94839483
ftp_username = urlparts.username
94849484
else:
@@ -9518,7 +9518,7 @@ def upload_file_to_ftp_file(ftpfile, url):
95189518
ftp.auth()
95199519
except all_errors:
95209520
pass
9521-
ftp.login(urlparts.username, urlparts.password)
9521+
ftp.login(ftp_username, ftp_password)
95229522
if(urlparts.scheme == "ftps" or isinstance(ftp, FTP_TLS)):
95239523
try:
95249524
ftp.prot_p()
@@ -9539,20 +9539,20 @@ def upload_file_to_ftp_file(ftpfile, url):
95399539
if(is_cwd_allowed):
95409540
ftp.storbinary("STOR "+file_name, ftpfile)
95419541
else:
9542-
ftp.storbinary("STOR "+urlparts.path, ftpfile)
9542+
ftp.storbinary("STOR "+unquote(urlparts.path), ftpfile)
95439543
except all_errors:
95449544
try:
95459545
ftp.set_pasv(True)
95469546
if(is_cwd_allowed):
95479547
ftp.storbinary("STOR "+file_name, ftpfile)
95489548
else:
9549-
ftp.storbinary("STOR "+urlparts.path, ftpfile)
9549+
ftp.storbinary("STOR "+unquote(urlparts.path), ftpfile)
95509550
except all_errors:
95519551
ftp.set_pasv(False)
95529552
if(is_cwd_allowed):
95539553
ftp.storbinary("STOR "+file_name, ftpfile)
95549554
else:
9555-
ftp.storbinary("STOR "+urlparts.path, ftpfile)
9555+
ftp.storbinary("STOR "+unquote(urlparts.path), ftpfile)
95569556
ftp.close()
95579557
ftpfile.seek(0, 0)
95589558
return ftpfile
@@ -9691,8 +9691,8 @@ def download_file_from_http_string(url, headers=geturls_headers_pyfile_python_al
96919691
if(haveparamiko):
96929692
def download_file_from_sftp_file(url):
96939693
urlparts = urlparse(url)
9694-
file_name = os.path.basename(urlparts.path)
9695-
file_dir = os.path.dirname(urlparts.path)
9694+
file_name = os.path.basename(unquote(urlparts.path))
9695+
file_dir = os.path.dirname(unquote(urlparts.path))
96969696
sftp_port = urlparts.port
96979697
if(urlparts.port is None):
96989698
sftp_port = 22
@@ -9719,7 +9719,7 @@ def download_file_from_sftp_file(url):
97199719
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
97209720
try:
97219721
ssh.connect(urlparts.hostname, port=sftp_port,
9722-
username=urlparts.username, password=urlparts.password)
9722+
username=sftp_username, password=urlparts.password)
97239723
except paramiko.ssh_exception.SSHException:
97249724
return False
97259725
except socket.gaierror:
@@ -9730,7 +9730,7 @@ def download_file_from_sftp_file(url):
97309730
return False
97319731
sftp = ssh.open_sftp()
97329732
sftpfile = MkTempFile()
9733-
sftp.getfo(urlparts.path, sftpfile)
9733+
sftp.getfo(unquote(urlparts.path), sftpfile)
97349734
sftp.close()
97359735
ssh.close()
97369736
sftpfile.seek(0, 0)
@@ -9752,8 +9752,8 @@ def download_file_from_sftp_string(url):
97529752
if(haveparamiko):
97539753
def upload_file_to_sftp_file(sftpfile, url):
97549754
urlparts = urlparse(url)
9755-
file_name = os.path.basename(urlparts.path)
9756-
file_dir = os.path.dirname(urlparts.path)
9755+
file_name = os.path.basename(unquote(urlparts.path))
9756+
file_dir = os.path.dirname(unquote(urlparts.path))
97579757
sftp_port = urlparts.port
97589758
if(urlparts.port is None):
97599759
sftp_port = 22
@@ -9770,7 +9770,7 @@ def upload_file_to_sftp_file(sftpfile, url):
97709770
else:
97719771
sftp_password = ""
97729772
if(urlparts.scheme == "ftp"):
9773-
return upload_file_to_ftp_file(url)
9773+
return upload_file_to_ftp_file(sftpfile, url)
97749774
elif(urlparts.scheme == "http" or urlparts.scheme == "https"):
97759775
return False
97769776
if(urlparts.scheme != "sftp"):
@@ -9780,7 +9780,7 @@ def upload_file_to_sftp_file(sftpfile, url):
97809780
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
97819781
try:
97829782
ssh.connect(urlparts.hostname, port=sftp_port,
9783-
username=urlparts.username, password=urlparts.password)
9783+
username=sftp_username, password=sftp_password)
97849784
except paramiko.ssh_exception.SSHException:
97859785
return False
97869786
except socket.gaierror:
@@ -9790,7 +9790,8 @@ def upload_file_to_sftp_file(sftpfile, url):
97909790
log.info("Error With URL "+url)
97919791
return False
97929792
sftp = ssh.open_sftp()
9793-
sftp.putfo(sftpfile, urlparts.path)
9793+
sftpfile.seek(0, 0)
9794+
sftp.putfo(sftpfile, unquote(urlparts.path))
97949795
sftp.close()
97959796
ssh.close()
97969797
sftpfile.seek(0, 0)
@@ -9812,8 +9813,8 @@ def upload_file_to_sftp_string(url):
98129813
if(havepysftp):
98139814
def download_file_from_pysftp_file(url):
98149815
urlparts = urlparse(url)
9815-
file_name = os.path.basename(urlparts.path)
9816-
file_dir = os.path.dirname(urlparts.path)
9816+
file_name = os.path.basename(unquote(urlparts.path))
9817+
file_dir = os.path.dirname(unquote(urlparts.path))
98179818
sftp_port = urlparts.port
98189819
if(urlparts.port is None):
98199820
sftp_port = 22
@@ -9836,8 +9837,8 @@ def download_file_from_pysftp_file(url):
98369837
if(urlparts.scheme != "sftp"):
98379838
return False
98389839
try:
9839-
pysftp.Connection(urlparts.hostname, port=sftp_port,
9840-
username=urlparts.username, password=urlparts.password)
9840+
sftp = pysftp.Connection(urlparts.hostname, port=sftp_port,
9841+
username=sftp_username, password=sftp_password)
98419842
except paramiko.ssh_exception.SSHException:
98429843
return False
98439844
except socket.gaierror:
@@ -9846,9 +9847,8 @@ def download_file_from_pysftp_file(url):
98469847
except socket.timeout:
98479848
log.info("Error With URL "+url)
98489849
return False
9849-
sftp = ssh.open_sftp()
98509850
sftpfile = MkTempFile()
9851-
sftp.getfo(urlparts.path, sftpfile)
9851+
sftp.getfo(unquote(urlparts.path), sftpfile)
98529852
sftp.close()
98539853
ssh.close()
98549854
sftpfile.seek(0, 0)
@@ -9870,8 +9870,8 @@ def download_file_from_pysftp_string(url):
98709870
if(havepysftp):
98719871
def upload_file_to_pysftp_file(sftpfile, url):
98729872
urlparts = urlparse(url)
9873-
file_name = os.path.basename(urlparts.path)
9874-
file_dir = os.path.dirname(urlparts.path)
9873+
file_name = os.path.basename(unquote(urlparts.path))
9874+
file_dir = os.path.dirname(unquote(urlparts.path))
98759875
sftp_port = urlparts.port
98769876
if(urlparts.port is None):
98779877
sftp_port = 22
@@ -9888,14 +9888,14 @@ def upload_file_to_pysftp_file(sftpfile, url):
98889888
else:
98899889
sftp_password = ""
98909890
if(urlparts.scheme == "ftp"):
9891-
return upload_file_to_ftp_file(url)
9891+
return upload_file_to_ftp_file(sftpfile, url)
98929892
elif(urlparts.scheme == "http" or urlparts.scheme == "https"):
98939893
return False
98949894
if(urlparts.scheme != "sftp"):
98959895
return False
98969896
try:
9897-
pysftp.Connection(urlparts.hostname, port=sftp_port,
9898-
username=urlparts.username, password=urlparts.password)
9897+
sftp = pysftp.Connection(urlparts.hostname, port=sftp_port,
9898+
username=sftp_username, password=sftp_password)
98999899
except paramiko.ssh_exception.SSHException:
99009900
return False
99019901
except socket.gaierror:
@@ -9904,8 +9904,8 @@ def upload_file_to_pysftp_file(sftpfile, url):
99049904
except socket.timeout:
99059905
log.info("Error With URL "+url)
99069906
return False
9907-
sftp = ssh.open_sftp()
9908-
sftp.putfo(sftpfile, urlparts.path)
9907+
sftpfile.seek(0, 0)
9908+
sftp.putfo(sftpfile, unquote(urlparts.path))
99099909
sftp.close()
99109910
ssh.close()
99119911
sftpfile.seek(0, 0)

0 commit comments

Comments
 (0)