From 404a2e6b2b7f049aa7de16e8e5d9a9ee40762470 Mon Sep 17 00:00:00 2001 From: Daniel Markstedt Date: Thu, 23 Sep 2021 12:18:17 -0700 Subject: [PATCH] Improve error handling of unzip method; clean up unused code --- src/web/file_cmds.py | 11 +++++++---- src/web/requirements.txt | 1 - src/web/templates/index.html | 14 +++----------- src/web/web.py | 19 +------------------ 4 files changed, 11 insertions(+), 34 deletions(-) diff --git a/src/web/file_cmds.py b/src/web/file_cmds.py index d5a9bbc9..5ced6335 100644 --- a/src/web/file_cmds.py +++ b/src/web/file_cmds.py @@ -63,11 +63,14 @@ def delete_file(file_name): def unzip_file(file_name): - import zipfile + from zipfile import ZipFile, is_zipfile - with zipfile.ZipFile(base_dir + file_name, "r") as zip_ref: - zip_ref.extractall(base_dir) - return True + if is_zipfile(file_name): + with zipfile.ZipFile(base_dir + file_name, "r") as zip_ref: + zip_ref.extractall(base_dir) + return {"status": True, "msg": f"{file_name} unzipped"} + else: + return {"status": False, "msg": f"{file_name} is not a zip file"} def download_file_to_iso(scsi_id, url): diff --git a/src/web/requirements.txt b/src/web/requirements.txt index bd0bdec8..e853db3d 100644 --- a/src/web/requirements.txt +++ b/src/web/requirements.txt @@ -12,4 +12,3 @@ zope.event==4.5.0 zope.interface==5.1.2 protobuf==3.17.3 pydrop==0.0.6 -zipfile \ No newline at end of file diff --git a/src/web/templates/index.html b/src/web/templates/index.html index eb093ebc..4664bcae 100644 --- a/src/web/templates/index.html +++ b/src/web/templates/index.html @@ -91,10 +91,10 @@ {% for file in files %} - {{file["name"].replace(base_dir, '')}} + {{file["name"]}}
- +
@@ -107,20 +107,12 @@ {% endfor %} - {% if not file["name"].lower().endswith(archive_file_suffix) %} - {% endif %}
- +
- {% if file["name"].lower().endswith(archive_file_suffix) %} -
- - -
- {% endif %} {% endfor %} diff --git a/src/web/web.py b/src/web/web.py index a4a8a84e..67f0f874 100644 --- a/src/web/web.py +++ b/src/web/web.py @@ -527,11 +527,7 @@ def upload_file(): log.debug(f"Chunk {current_chunk + 1} of {total_chunks} " f"for file {file.filename} completed.") - from zipfile import ZipFile, is_zipfile - if is_zipfile(save_path): - with ZipFile(save_path, 'r') as z: - z.extractall(path=app.config["UPLOAD_FOLDER"]) - delete_file(filename) + if unzip_file(filename): return make_response(("File upload and unzip successful!", 200)) else: return make_response(("File upload successful!", 200)) @@ -594,19 +590,6 @@ def delete(): return redirect(url_for("index")) - -@app.route("/files/unzip", methods=["POST"]) -def unzip(): - image = request.form.get("image") - - if unzip_file(image): - flash("Unzipped file " + image) - return redirect(url_for("index")) - else: - flash("Failed to unzip " + image, "error") - return redirect(url_for("index")) - - if __name__ == "__main__": app.secret_key = "rascsi_is_awesome_insecure_secret_key" app.config["SESSION_TYPE"] = "filesystem"