diff options
author | Frederick Muriuki Muriithi | 2024-10-17 10:33:51 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-10-17 11:32:28 -0500 |
commit | bacb21f34e1912c657d93a4fb1474664300b7ba7 (patch) | |
tree | eda7f4731e6e429654d9a75b2a1210e106926b52 /uploader | |
parent | 8745a5db5c91d49befd76a0e6ea8f06e7a878180 (diff) | |
download | gn-uploader-bacb21f34e1912c657d93a4fb1474664300b7ba7.tar.gz |
Implement sha256 hashing over file contents.
Diffstat (limited to 'uploader')
-rw-r--r-- | uploader/files.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/uploader/files.py b/uploader/files.py index b163612..d37a53e 100644 --- a/uploader/files.py +++ b/uploader/files.py @@ -1,7 +1,9 @@ """Utilities to deal with uploaded files.""" import hashlib from pathlib import Path +from typing import Iterator from datetime import datetime + from flask import current_app from werkzeug.utils import secure_filename @@ -21,6 +23,27 @@ def save_file(fileobj: FileStorage, upload_dir: Path) -> Path: fileobj.save(filepath) return filepath + def fullpath(filename: str): """Get a file's full path. This makes use of `flask.current_app`.""" return Path(current_app.config["UPLOAD_FOLDER"], filename).absolute() + + +def chunked_binary_read(filepath: Path, chunksize: int = 2048) -> Iterator: + """Read a file in binary mode in chunks.""" + with open(filepath, "rb") as inputfile: + while True: + data = inputfile.read(chunksize) + if data != b"": + yield data + continue + break + + +def sha256_digest_over_file(filepath: Path) -> str: + """Compute the sha256 digest over a file's contents.""" + filehash = hashlib.sha256() + for chunk in chunked_binary_read(filepath): + filehash.update(chunk) + + return filehash.hexdigest() |