From 883e9648d7205d5d37c39c61f12db41f4a57c01b Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Wed, 17 Apr 2024 16:57:07 +0200 Subject: [PATCH] speed up hash --- paperlibrary/library/library.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/paperlibrary/library/library.py b/paperlibrary/library/library.py index f9ec578..8c4ee5c 100644 --- a/paperlibrary/library/library.py +++ b/paperlibrary/library/library.py @@ -119,14 +119,18 @@ def download_file(api: PaperLibraryAPI, url: str, target_file: Path): def hash_file(file: Path, buffer_size=65536) -> str: - sha256 = hashlib.sha256() - with file.open("rb") as f: - while True: - data = f.read(buffer_size) - if not data: - break - sha256.update(data) - return sha256.hexdigest() + try: + with file.open("rb") as f: + return hashlib.file_digest(f, "sha256").hexdigest() + except AttributeError: + sha256 = hashlib.sha256() + with file.open("rb") as f: + while True: + data = f.read(buffer_size) + if not data: + break + sha256.update(data) + return sha256.hexdigest() def update_pdfs(api: PaperLibraryAPI, config: Config):