Improve error handling of unzip method; clean up unused code

This commit is contained in:
Daniel Markstedt 2021-09-23 12:18:17 -07:00
parent 9bebb0b0b5
commit 404a2e6b2b
4 changed files with 11 additions and 34 deletions

View File

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

View File

@ -12,4 +12,3 @@ zope.event==4.5.0
zope.interface==5.1.2
protobuf==3.17.3
pydrop==0.0.6
zipfile

View File

@ -91,10 +91,10 @@
</tr>
{% for file in files %}
<tr>
<td>{{file["name"].replace(base_dir, '')}}</td>
<td>{{file["name"]}}</td>
<td style="text-align:center">
<form action="/files/download" method="post">
<input type="hidden" name="image" value="{{file["name"].replace(base_dir, '')}}">
<input type="hidden" name="image" value="{{file["name"]}}">
<input type="submit" value="{{file["size_mb"]}} MB &#8595;" />
</form>
</td>
@ -107,20 +107,12 @@
<option value="{{id}}">{{id}}</option>
{% endfor %}
</select>
{% if not file["name"].lower().endswith(archive_file_suffix) %}
<input type="submit" value="Attach" />
{% endif %}
</form>
<form action="/files/delete" method="post" onsubmit="return confirm('Delete file?')">
<input type="hidden" name="image" value="{{file["name"].replace(base_dir, '')}}">
<input type="hidden" name="image" value="{{file["name"]}}">
<input type="submit" value="Delete" />
</form>
{% if file["name"].lower().endswith(archive_file_suffix) %}
<form action="/files/unzip" method="post">
<input type="hidden" name="image" value="{{file["name"].replace(base_dir, '')}}">
<input type="submit" value="Unzip" />
</form>
{% endif %}
</td>
</tr>
{% endfor %}

View File

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