From c002bc698b3338600ba95ef13aa94d2423416c3f Mon Sep 17 00:00:00 2001 From: echo724 Date: Fri, 29 Dec 2023 11:39:32 +0900 Subject: [PATCH] fix: fix encoded filename is too long case --- notion2md/convertor/block.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/notion2md/convertor/block.py b/notion2md/convertor/block.py index 7dc481e..9e252c6 100644 --- a/notion2md/convertor/block.py +++ b/notion2md/convertor/block.py @@ -2,7 +2,7 @@ import hashlib import os import urllib.request as request -from urllib.parse import urlparse +from urllib.parse import urlparse,unquote from cleo.io.io import IO @@ -129,31 +129,32 @@ def collect_info(self, payload: dict) -> dict: info["cells"] = payload["cells"] return info - def download_file(self, url: str) -> str: + def download_file(self, url: str) -> tuple[str, str]: file_name = os.path.basename(urlparse(url).path) + unquoted_file_name = unquote(file_name) if self._config.download: - if file_name: - name, extension = os.path.splitext(file_name) + if unquoted_file_name: + name, extension = os.path.splitext(unquoted_file_name) if not extension: - return file_name, url + return unquoted_file_name, url url_hash = hashlib.blake2s( urlparse(url).path.encode() ).hexdigest()[:8] - downloaded_file_name = f"{url_hash}_{file_name}" + downloaded_file_name = f"{url_hash}_{unquoted_file_name}" fullpath = os.path.join( self._config.tmp_path, downloaded_file_name ) if self._io: - self._io.write_line(status("Downloading", f"{file_name}")) + self._io.write_line(status("Downloading", f"{unquoted_file_name}")) request.urlretrieve(url, fullpath) self._io.write_line( success( "Downloaded", - f'"{file_name}" -> "{downloaded_file_name}"', + f'"{unquoted_file_name}" -> "{downloaded_file_name}"', ) ) else: @@ -163,7 +164,7 @@ def download_file(self, url: str) -> str: if self._io: self._io.write_line(error(f"invalid {url}")) else: - return file_name, url + return unquoted_file_name, url def to_string(self, blocks: dict) -> str: return self.convert(blocks)