download: Unquote filenames - transferwee - Download/upload file via wetransfer.com
(HTM) git clone https://github.com/iamleot/transferwee
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit ddeb05d476531f202aea1a6b2e9b47188d2c79b1
(DIR) parent 6ae8fc2adf06cff947d35e08dd211628536ec1be
(HTM) Author: Leonardo Taccari <iamleot@gmail.com>
Date: Sun, 15 Dec 2019 12:33:04 +0100
download: Unquote filenames
Introduce a _file_unquote() function to unquote filename so file will
be downloaded without %-encoded names properly extended.
Should address issue #9.
Diffstat:
M transferwee.py | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
---
(DIR) diff --git a/transferwee.py b/transferwee.py
@@ -112,6 +112,15 @@ def download_url(url: str) -> str:
return j.get('direct_link')
+def _file_unquote(file: str) -> str:
+ """Given a URL encoded file unquote it.
+
+ All occurences of `\', `/' and `../' will be ignored to avoid possible
+ directory traversals.
+ """
+ return urllib.parse.unquote(file).replace('../', '').replace('/', '').replace('\\', '')
+
+
def download(url: str) -> None:
"""Given a `we.tl/' or `wetransfer.com/downloads/' download it.
@@ -120,7 +129,7 @@ def download(url: str) -> None:
working directory.
"""
dl_url = download_url(url)
- file = urllib.parse.urlparse(dl_url).path.split('/')[-1]
+ file = _file_unquote(urllib.parse.urlparse(dl_url).path.split('/')[-1])
r = requests.get(dl_url, stream=True)
with open(file, 'wb') as f: