Skip to content

Commit

Permalink
Hash saved files if no block id is provided (#185)
Browse files Browse the repository at this point in the history
* Hash saved files if no block id is provided

* Add a note in `Client.download_file`
  • Loading branch information
sHermanGriffiths authored Jul 3, 2024
1 parent aac93cb commit e3eabf4
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions n2y/notion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import importlib.util
import json
from os import makedirs, path
Expand Down Expand Up @@ -461,22 +462,29 @@ def _parse_response(self, response, stream=False):
raise HTTPResponseError(error.response)
return response.json() if not stream else response.content

def download_file(self, url, page, block_id):
def download_file(self, url, page, block_id=None):
"""
Download a file from a given URL into the MEDIA_ROOT.
Preserve the file extension from the URL, but use the
id of the block followed by an md5 hash.
"""
# block_id will be None if the file is not attached to a block
# (as is the case for page properties) or one would rather tie
# the file name to it's content than it's location in Notion.

url_path = path.basename(urlparse(url).path)
_, extension = path.splitext(url_path)
content = self._get_url(url, stream=True)
return self.save_file(content, page, extension, block_id)

def save_file(self, content, page, extension, block_id):
block_id_chars = strip_hyphens(block_id)
def save_file(self, content, page, extension, block_id=None):
if block_id is None:
id_chars = hashlib.md5(content).hexdigest()
else:
id_chars = strip_hyphens(block_id)
page_title = sanitize_filename(page.title.to_plain_text())
relative_filepath = f"{page_title}-{block_id_chars[:11]}{extension}"
relative_filepath = f"{page_title}-{id_chars[:11]}{extension}"
full_filepath = path.join(self.media_root, relative_filepath)
makedirs(self.media_root, exist_ok=True)
with open(full_filepath, "wb") as temp_file:
Expand Down

0 comments on commit e3eabf4

Please sign in to comment.