Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix encoded filename is too long case #59

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions notion2md/convertor/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
Loading