Use save() to upload files, to address file corruption issues with the previous solution. Also adds file ending validation which has been requested by users.

This commit is contained in:
Daniel Markstedt 2021-09-20 12:47:39 -07:00
parent d434dfe918
commit ac97d4bbaa
2 changed files with 37 additions and 29 deletions

View File

@ -170,22 +170,16 @@
<h2>Upload File</h2> <h2>Upload File</h2>
<p>Uploads file to <tt>{{base_dir}}</tt>. Max file size is set to {{max_file_size / 1024 /1024 }}MB</p> <p>Uploads file to <tt>{{base_dir}}</tt>. Max file size is set to {{max_file_size / 1024 /1024 }}MB</p>
<table style="border: none"> <table style="border: none">
<tr style="border: none"> <tr style="border: none">
<td style="border: none; vertical-align:top;"> <td style="border: none; vertical-align:top;">
<form id="uploadForm" action="/files/upload/" onchange="fileSelect(event)" method="post" enctype="multipart/form-data"> <form action="/files/upload" method="post" enctype="multipart/form-data">
<label for="file">File:</label> <label for="file">File:</label>
<input type="file" name="file"/> <input type="file" name="file" />
<input type="submit" value="Upload" /> <input type="submit" value="Upload" />
</form> </form>
</td> </td>
</tr> </tr>
</table> </table>
<script>
function fileSelect(e) {
document.getElementById("uploadForm").setAttribute('action', "/files/upload/" + e.target.files[0].name)
console.log(e.target.files[0].name);
}
</script>
<hr/> <hr/>

View File

@ -1,4 +1,13 @@
from flask import Flask, render_template, request, flash, url_for, redirect, send_file, send_from_directory from flask import (
Flask,
render_template,
request,
flash,
url_for,
redirect,
send_file,
send_from_directory,
)
from file_cmds import ( from file_cmds import (
list_files, list_files,
@ -465,29 +474,34 @@ def download_img():
return redirect(url_for("index")) return redirect(url_for("index"))
@app.route("/files/upload/<filename>", methods=["POST"]) @app.route("/files/upload", methods=["POST"])
def upload_file(filename): def upload_file():
if not filename: if 'file' not in request.files:
flash("No file provided.", "error") flash("No file part in request.", "error")
return redirect(url_for("index"))
f = request.files["file"]
if f.filename == "":
flash("No file selected.", "error")
return redirect(url_for("index")) return redirect(url_for("index"))
from werkzeug.utils import secure_filename
from os import path from os import path
file_path = path.join(app.config["UPLOAD_FOLDER"], filename) filename = secure_filename(f.filename)
if path.isfile(file_path): filepath = path.join(app.config["UPLOAD_FOLDER"], filename)
if not filename.lower().endswith(VALID_FILE_SUFFIX):
flash("Not a file format RaSCSI recognizes. Needs to be one of:", "error")
flash(f"{VALID_FILE_SUFFIX}", "error")
return redirect(url_for("index"))
if path.isfile(filepath):
flash(f"{filename} already exists.", "error") flash(f"{filename} already exists.", "error")
return redirect(url_for("index")) return redirect(url_for("index"))
try:
from io import DEFAULT_BUFFER_SIZE f.save(filepath)
binary_new_file = "bx" flash(f"File {filename} successfully uploaded to {base_dir} !")
with open(file_path, binary_new_file, buffering=DEFAULT_BUFFER_SIZE) as f: return redirect(url_for("index"))
chunk_size = DEFAULT_BUFFER_SIZE except error as e:
while True: flash(f"Failed to upload {filename}: {str(e)}")
chunk = request.stream.read(chunk_size) return redirect(url_for("index"))
if len(chunk) == 0:
break
f.write(chunk)
# TODO: display an informative success message
return redirect(url_for("index", filename=filename))
@app.route("/files/create", methods=["POST"]) @app.route("/files/create", methods=["POST"])