diff options
-rw-r--r-- | uploader/files/__init__.py | 2 | ||||
-rw-r--r-- | uploader/files/chunks.py | 32 | ||||
-rw-r--r-- | uploader/files/functions.py | 14 |
3 files changed, 35 insertions, 13 deletions
diff --git a/uploader/files/__init__.py b/uploader/files/__init__.py index 4a62409..60d2f3b 100644 --- a/uploader/files/__init__.py +++ b/uploader/files/__init__.py @@ -1,4 +1,4 @@ +from .chunks import chunked_binary_read from .functions import (fullpath, save_file, - chunked_binary_read, sha256_digest_over_file) diff --git a/uploader/files/chunks.py b/uploader/files/chunks.py new file mode 100644 index 0000000..c4360b5 --- /dev/null +++ b/uploader/files/chunks.py @@ -0,0 +1,32 @@ +"""Functions dealing with chunking of files.""" +from pathlib import Path +from typing import Iterator + +from flask import current_app as app +from werkzeug.utils import secure_filename + + +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 chunk_name(uploadfilename: str, chunkno: int) -> str: + """Generate chunk name from original filename and chunk number""" + if uploadfilename == "": + raise ValueError("Name cannot be empty!") + if chunkno < 1: + raise ValueError("Chunk number must be greater than zero") + return f"{secure_filename(uploadfilename)}_part_{chunkno:05d}" + + +def chunks_directory(uniqueidentifier: str) -> Path: + """Compute the directory where chunks are temporarily stored.""" + if uniqueidentifier == "": + raise ValueError("Unique identifier cannot be empty!") + return Path(app.config["UPLOAD_FOLDER"], f"tempdir_{uniqueidentifier}") diff --git a/uploader/files/functions.py b/uploader/files/functions.py index d37a53e..5a3dece 100644 --- a/uploader/files/functions.py +++ b/uploader/files/functions.py @@ -1,7 +1,6 @@ """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 @@ -9,6 +8,8 @@ from flask import current_app from werkzeug.utils import secure_filename from werkzeug.datastructures import FileStorage +from .chunks import chunked_binary_read + def save_file(fileobj: FileStorage, upload_dir: Path) -> Path: """Save the uploaded file and return the path.""" assert bool(fileobj), "Invalid file object!" @@ -29,17 +30,6 @@ def fullpath(filename: str): 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() |