From 0eb9e50d18916e68d5f17a280f9ea8c96d2b94b0 Mon Sep 17 00:00:00 2001 From: Daniel Markstedt Date: Mon, 13 Dec 2021 18:50:36 -0800 Subject: [PATCH] Rename image file in web interface (#521) * Introduce rename_image method * Introduce rename endpoint * Better prompt text * Fix bug * Make renaming properties files work * Have current file name prefilled * Add login_required decorator * Remove unused code --- src/web/file_cmds.py | 18 ++++++++++++++++++ src/web/templates/index.html | 7 ++++++- src/web/web.py | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/web/file_cmds.py b/src/web/file_cmds.py index 35948a67..fbdac092 100644 --- a/src/web/file_cmds.py +++ b/src/web/file_cmds.py @@ -146,6 +146,24 @@ def delete_image(file_name): return {"status": result.status, "msg": result.msg} +def rename_image(file_name, new_file_name): + """ + Takes (str) file_name, (str) new_file_name + Sends a RENAME_IMAGE command to the server + Returns (dict) with (bool) status and (str) msg + """ + command = proto.PbCommand() + command.operation = proto.PbOperation.RENAME_IMAGE + + command.params["from"] = file_name + command.params["to"] = new_file_name + + data = send_pb_command(command.SerializeToString()) + result = proto.PbResult() + result.ParseFromString(data) + return {"status": result.status, "msg": result.msg} + + def delete_file(file_path): """ Takes (str) file_path with the full path to the file to delete diff --git a/src/web/templates/index.html b/src/web/templates/index.html index 116f618a..93cb0fce 100644 --- a/src/web/templates/index.html +++ b/src/web/templates/index.html @@ -277,8 +277,13 @@ {% endif %} +
+ + + +
- +
{% endif %} diff --git a/src/web/web.py b/src/web/web.py index 9dc852eb..a6dd9ee1 100644 --- a/src/web/web.py +++ b/src/web/web.py @@ -26,7 +26,9 @@ from file_cmds import ( create_new_image, download_file_to_iso, delete_image, + rename_image, delete_file, + rename_file, unzip_file, download_to_dir, write_config, @@ -869,7 +871,7 @@ def delete(): """ Deletes a specified file in the images dir """ - file_name = request.form.get("image") + file_name = request.form.get("file_name") process = delete_image(file_name) if process["status"]: @@ -892,6 +894,37 @@ def delete(): return redirect(url_for("index")) +@APP.route("/files/rename", methods=["POST"]) +@login_required +def rename(): + """ + Renames a specified file in the images dir + """ + file_name = request.form.get("file_name") + new_file_name = request.form.get("new_file_name") + + process = rename_image(file_name, new_file_name) + if process["status"]: + flash(f"Image file renamed to: {new_file_name}") + else: + flash(process["msg"], "error") + return redirect(url_for("index")) + + # Rename the drive properties file, if it exists + prop_file_path = f"{CFG_DIR}/{file_name}.{PROPERTIES_SUFFIX}" + new_prop_file_path = f"{CFG_DIR}/{new_file_name}.{PROPERTIES_SUFFIX}" + if Path(prop_file_path).is_file(): + process = rename_file(prop_file_path, new_prop_file_path) + if process["status"]: + flash(process["msg"]) + return redirect(url_for("index")) + + flash(process["msg"], "error") + return redirect(url_for("index")) + + return redirect(url_for("index")) + + @APP.route("/files/unzip", methods=["POST"]) @login_required def unzip():