From 87640a7c2cb360841e3fdc18cd2d490e08a59999 Mon Sep 17 00:00:00 2001 From: Daniel Markstedt Date: Mon, 25 Oct 2021 18:58:16 -0700 Subject: [PATCH] More verbose unzipping; don't overwrite existing files (#378) --- src/web/file_cmds.py | 15 +++++++++------ src/web/web.py | 7 ++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/web/file_cmds.py b/src/web/file_cmds.py index 82de6849..c7720977 100644 --- a/src/web/file_cmds.py +++ b/src/web/file_cmds.py @@ -138,21 +138,24 @@ def delete_file(file_path): def unzip_file(file_name): """ - Takes str file_name - Returns dict with boolean status and str msg + Takes (str) file_name + Returns dict with (boolean) status and (list of str) msg """ from subprocess import run server_info = get_server_info() unzip_proc = run( - ["unzip", "-d", server_info["image_dir"], "-o", "-j", \ + ["unzip", "-d", server_info["image_dir"], "-n", "-j", \ f"{server_info['image_dir']}/{file_name}"], capture_output=True ) if unzip_proc.returncode != 0: - logging.warning(f"Unzipping failed: {unzip_proc}") - return {"status": False, "msg": str(unzip_proc)} + stderr = unzip_proc.stderr.decode("utf-8") + logging.warning(f"Unzipping failed: {stderr}") + return {"status": False, "msg": stderr} - return {"status": True, "msg": f"Unzipped {file_name} to {server_info['image_dir']}"} + from re import findall + unzipped = findall("inflating:(.+)\n", unzip_proc.stdout.decode("utf-8")) + return {"status": True, "msg": unzipped} def download_file_to_iso(scsi_id, url): diff --git a/src/web/web.py b/src/web/web.py index e2e1df92..77cd0e25 100644 --- a/src/web/web.py +++ b/src/web/web.py @@ -640,7 +640,12 @@ def unzip(): process = unzip_file(image) if process["status"]: - flash(process["msg"]) + if len(process["msg"]) < 1: + flash("Aborted unzip: File(s) with the same name already exists.", "error") + return redirect(url_for("index")) + flash("Unzipped the following files:") + for m in process["msg"]: + flash(m) return redirect(url_for("index")) else: flash("Failed to unzip " + image, "error")