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
This commit is contained in:
Daniel Markstedt 2021-12-13 18:50:36 -08:00 committed by GitHub
parent 986116f8d9
commit 0eb9e50d18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 2 deletions

View File

@ -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

View File

@ -277,8 +277,13 @@
<input type="submit" value="Attach">
{% endif %}
</form>
<form action="/files/rename" method="post" onsubmit="var new_file_name = prompt('Enter new file name for \'{{ file["name"] }}\'', '{{ file['name'] }}'); if (new_file_name === null) event.preventDefault(); document.getElementById('new_file_name_{{ loop.index }}').value = new_file_name;">
<input name="file_name" type="hidden" value="{{ file['name'] }}">
<input name="new_file_name" id="new_file_name_{{ loop.index }}" type="hidden" value="">
<input type="submit" value="Rename">
</form>
<form action="/files/delete" method="post" onsubmit="return confirm('Delete file \'{{ file["name"] }}\'?')">
<input name="image" type="hidden" value="{{ file['name'] }}">
<input name="file_name" type="hidden" value="{{ file['name'] }}">
<input type="submit" value="Delete">
</form>
{% endif %}

View File

@ -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():