diff options
Diffstat (limited to 'uploader/files')
-rw-r--r-- | uploader/files/__init__.py | 1 | ||||
-rw-r--r-- | uploader/files/functions.py | 11 | ||||
-rw-r--r-- | uploader/files/views.py | 31 |
3 files changed, 32 insertions, 11 deletions
diff --git a/uploader/files/__init__.py b/uploader/files/__init__.py index 60d2f3b..53c3176 100644 --- a/uploader/files/__init__.py +++ b/uploader/files/__init__.py @@ -1,3 +1,4 @@ +"""General files and chunks utilities.""" from .chunks import chunked_binary_read from .functions import (fullpath, save_file, diff --git a/uploader/files/functions.py b/uploader/files/functions.py index 5a3dece..7b9f06b 100644 --- a/uploader/files/functions.py +++ b/uploader/files/functions.py @@ -10,12 +10,15 @@ from werkzeug.datastructures import FileStorage from .chunks import chunked_binary_read -def save_file(fileobj: FileStorage, upload_dir: Path) -> Path: +def save_file(fileobj: FileStorage, upload_dir: Path, hashed: bool = True) -> Path: """Save the uploaded file and return the path.""" assert bool(fileobj), "Invalid file object!" - hashed_name = hashlib.sha512( - f"{fileobj.filename}::{datetime.now().isoformat()}".encode("utf8") - ).hexdigest() + hashed_name = ( + hashlib.sha512( + f"{fileobj.filename}::{datetime.now().isoformat()}".encode("utf8") + ).hexdigest() + if hashed else + fileobj.filename) filename = Path(secure_filename(hashed_name)) # type: ignore[arg-type] if not upload_dir.exists(): upload_dir.mkdir() diff --git a/uploader/files/views.py b/uploader/files/views.py index 7d39cee..ddf5350 100644 --- a/uploader/files/views.py +++ b/uploader/files/views.py @@ -8,6 +8,11 @@ from .chunks import chunk_name, chunks_directory files = Blueprint("files", __name__) +def target_file(fileid: str) -> Path: + """Compute the full path for the target file.""" + return Path(app.config["UPLOAD_FOLDER"], fileid) + + @files.route("/upload/resumable", methods=["GET"]) def resumable_upload_get(): """Used for checking whether **ALL** chunks have been uploaded.""" @@ -21,11 +26,22 @@ def resumable_upload_get(): "statuscode": 400 }), 400 + # If the complete target file exists, return 200 for all chunks. + _targetfile = target_file(fileid) + if _targetfile.exists(): + return jsonify({ + "uploaded-file": _targetfile.name, + "original-name": filename, + "chunk": chunk, + "message": "The complete file already exists.", + "statuscode": 200 + }), 200 + if Path(chunks_directory(fileid), chunk_name(filename, chunk)).exists(): return jsonify({ "chunk": chunk, - "message": f"Chunk exists.", + "message": f"Chunk {chunk} exists.", "statuscode": 200 }), 200 @@ -43,7 +59,7 @@ def __merge_chunks__(targetfile: Path, chunkpaths: tuple[Path, ...]) -> Path: with open(chunkfile, "rb") as _chunkdata: _target.write(_chunkdata.read()) - chunkfile.unlink() + chunkfile.unlink(missing_ok=True) return targetfile @@ -56,11 +72,12 @@ def resumable_upload_post(): "resumableFilename", default="", type=str) or "" _fileid = request.form.get( "resumableIdentifier", default="", type=str) or "" - _targetfile = Path(app.config["UPLOAD_FOLDER"], _fileid) + _targetfile = target_file(_fileid) if _targetfile.exists(): return jsonify({ "uploaded-file": _targetfile.name, + "original-name": _uploadfilename, "message": "File was uploaded successfully!", "statuscode": 200 }), 200 @@ -80,14 +97,14 @@ def resumable_upload_post(): chunks_directory(_fileid).rmdir() return jsonify({ "uploaded-file": _targetfile.name, + "original-name": _uploadfilename, "message": "File was uploaded successfully!", "statuscode": 200 }), 200 return jsonify({ - "message": "Some chunks were not uploaded!", - "error": "ChunksUploadError", - "error-description": "Some chunks were not uploaded!" - }) + "message": f"Chunk {int(_chunk)} uploaded successfully.", + "statuscode": 201 + }), 201 except Exception as exc:# pylint: disable=[broad-except] msg = "Error processing uploaded file chunks." app.logger.error(msg, exc_info=True, stack_info=True) |