Skip to content

Commit

Permalink
feat: Make downloaded filenames consistent between runs (#38)
Browse files Browse the repository at this point in the history
The downloaded filenames change between multiple runs which make it hard
to track changes.
This will make them consistent by prefixing a the hashed url-path to the
filename. Using just the filename would not work if a notion page
contains e.g. two different images with the same filename.

Co-authored-by: Felix <fkunzweiler@xps-felix.fritz.box>
  • Loading branch information
felixthebeard and Felix committed Oct 14, 2022
1 parent 49a423a commit 30e8f0b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions notion2md/convertor/block.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import concurrent.futures
import hashlib
import os
import urllib.request as request
import uuid
Expand Down Expand Up @@ -137,12 +138,16 @@ def download_file(self, url: str) -> str:
file_name = os.path.basename(urlparse(url).path)
if self._config.download:
if file_name:
name, extentsion = os.path.splitext(file_name)
name, extension = os.path.splitext(file_name)

if not extentsion:
if not extension:
return file_name, url

downloaded_file_name = str(uuid.uuid4())[:8] + extentsion
url_hash = hashlib.blake2s(
urlparse(url).path.encode()
).hexdigest()[:8]
downloaded_file_name = f"{url_hash}_{file_name}"

fullpath = os.path.join(
self._config.tmp_path, downloaded_file_name
)
Expand Down

0 comments on commit 30e8f0b

Please sign in to comment.