diff options
author | Frederick Muriuki Muriithi | 2024-12-20 12:34:17 -0600 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-12-20 12:34:17 -0600 |
commit | bb85bfe307231a3082681ff3ececb331a56b0bdc (patch) | |
tree | 4a82858261c388b21b84449a77c67de85f707437 /uploader/files/chunks.py | |
parent | 959fb4bb4ec50af4bc28a4de1455d25ab82b10e8 (diff) | |
download | gn-uploader-bb85bfe307231a3082681ff3ececb331a56b0bdc.tar.gz |
Separate generic functions from chunking functions
Diffstat (limited to 'uploader/files/chunks.py')
-rw-r--r-- | uploader/files/chunks.py | 32 |
1 files changed, 32 insertions, 0 deletions
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}") |