Skip to content

Commit 073994a

Browse files
author
Eun-chan Cho
committed
fix: fix encoded filename is too long case (#59)
1 parent 8eb7747 commit 073994a

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

notion2md/convertor/block.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import hashlib
33
import os
44
import urllib.request as request
5-
from urllib.parse import urlparse
5+
from urllib.parse import urlparse,unquote
66

77
from cleo.io.io import IO
88

@@ -129,31 +129,32 @@ def collect_info(self, payload: dict) -> dict:
129129
info["cells"] = payload["cells"]
130130
return info
131131

132-
def download_file(self, url: str) -> str:
132+
def download_file(self, url: str) -> tuple[str, str]:
133133
file_name = os.path.basename(urlparse(url).path)
134+
unquoted_file_name = unquote(file_name)
134135
if self._config.download:
135-
if file_name:
136-
name, extension = os.path.splitext(file_name)
136+
if unquoted_file_name:
137+
name, extension = os.path.splitext(unquoted_file_name)
137138

138139
if not extension:
139-
return file_name, url
140+
return unquoted_file_name, url
140141

141142
url_hash = hashlib.blake2s(
142143
urlparse(url).path.encode()
143144
).hexdigest()[:8]
144-
downloaded_file_name = f"{url_hash}_{file_name}"
145+
downloaded_file_name = f"{url_hash}_{unquoted_file_name}"
145146

146147
fullpath = os.path.join(
147148
self._config.tmp_path, downloaded_file_name
148149
)
149150

150151
if self._io:
151-
self._io.write_line(status("Downloading", f"{file_name}"))
152+
self._io.write_line(status("Downloading", f"{unquoted_file_name}"))
152153
request.urlretrieve(url, fullpath)
153154
self._io.write_line(
154155
success(
155156
"Downloaded",
156-
f'"{file_name}" -> "{downloaded_file_name}"',
157+
f'"{unquoted_file_name}" -> "{downloaded_file_name}"',
157158
)
158159
)
159160
else:
@@ -163,7 +164,7 @@ def download_file(self, url: str) -> str:
163164
if self._io:
164165
self._io.write_line(error(f"invalid {url}"))
165166
else:
166-
return file_name, url
167+
return unquoted_file_name, url
167168

168169
def to_string(self, blocks: dict) -> str:
169170
return self.convert(blocks)

0 commit comments

Comments
 (0)