From b73ef29f06498838c887d1683d12779c092a629b Mon Sep 17 00:00:00 2001 From: Daniel Markstedt Date: Fri, 24 Sep 2021 14:48:54 -0700 Subject: [PATCH] Use native unzip instead of python zipfile lib for better performance --- src/web/file_cmds.py | 20 +++++++++++--------- src/web/web.py | 8 ++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/web/file_cmds.py b/src/web/file_cmds.py index 5ced6335..d774e27b 100644 --- a/src/web/file_cmds.py +++ b/src/web/file_cmds.py @@ -1,5 +1,4 @@ import os -import subprocess import logging from ractl_cmds import ( @@ -63,20 +62,23 @@ def delete_file(file_name): def unzip_file(file_name): - from zipfile import ZipFile, is_zipfile + from subprocess import run - 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"} + unzip_proc = run( + ["unzip", "-d", base_dir, "-o", "-j", base_dir + file_name], capture_output=True + ) + if unzip_proc.returncode != 0: + logging.warning(f"Unzipping failed: {unzip_proc}") + return {"status": False, "msg": unzip_proc} + + return {"status": True, "msg": f"{file_name} unzipped"} def download_file_to_iso(scsi_id, url): import urllib.request import urllib.error as error import time + from subprocess import run file_name = url.split("/")[-1] tmp_ts = int(time.time()) @@ -94,7 +96,7 @@ def download_file_to_iso(scsi_id, url): return {"status": False, "msg": "Error loading the URL"} # iso_filename = make_cd(tmp_full_path, None, None) # not working yet - iso_proc = subprocess.run( + iso_proc = run( ["genisoimage", "-hfs", "-o", iso_filename, tmp_full_path], capture_output=True ) if iso_proc.returncode != 0: diff --git a/src/web/web.py b/src/web/web.py index 2d3a8034..bc84828f 100644 --- a/src/web/web.py +++ b/src/web/web.py @@ -522,14 +522,14 @@ def upload_file(): return make_response(("Transferred file corrupted!", 500)) else: log.info(f"File {file.filename} has been uploaded successfully") + if filename.lower().endswith(".zip"): + unzip_file(filename) else: log.debug(f"Chunk {current_chunk + 1} of {total_chunks} " f"for file {file.filename} completed.") - if unzip_file(filename): - return make_response(("File upload and unzip successful!", 200)) - else: - return make_response(("File upload successful!", 200)) + + return make_response(("File upload successful!", 200)) @app.route("/files/create", methods=["POST"])