aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--uploader/files.py23
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()