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

Undo recent addition of a filename hashing system in the core Client class functionality #194

Merged
merged 1 commit into from
Aug 29, 2024
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
17 changes: 5 additions & 12 deletions n2y/notion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import hashlib
import importlib.util
import json
from os import makedirs, path
Expand Down Expand Up @@ -461,27 +460,21 @@ 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=None):
def download_file(self, url, page, block_id):
"""
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.
Preserve the file extension from the URL, but use the page title
followed by a segment of the id of the block as the file name.
"""
# 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=None):
if block_id is None:
id_chars = hashlib.md5(content).hexdigest()
else:
id_chars = strip_hyphens(block_id)
def save_file(self, content, page, extension, block_id):
id_chars = strip_hyphens(block_id)
page_title = sanitize_filename(page.title.to_plain_text())
relative_filepath = f"{page_title}-{id_chars[:11]}{extension}"
full_filepath = path.join(self.media_root, relative_filepath)
Expand Down
Loading