allow uploading multiple files

This commit is contained in:
iou1name 2018-06-05 06:58:18 -04:00
parent 2815e9e029
commit 6ac585d9f5
2 changed files with 32 additions and 30 deletions

View File

@ -362,39 +362,41 @@ def index():
if request.method == "GET": if request.method == "GET":
return render_template("index.html") return render_template("index.html")
file = request.files.get('file') urls = []
fname = secure_filename(file.filename) for file in request.files.getlist("file"):
pre = get_rand_chars(8) fname = secure_filename(file.filename)
fdir = app.config.get("UPLOAD_DIR") pre = get_rand_chars(8)
fdir = app.config.get("UPLOAD_DIR")
if request.form.get("randname") == "on": if request.form.get("randname") == "on":
fname = pre + os.path.splitext(fname)[1] fname = pre + os.path.splitext(fname)[1]
else: else:
fname = pre + "_" + fname fname = pre + "_" + fname
if request.form.get("delflag") == "on": if request.form.get("delflag") == "on":
try: try:
delete_time = int(request.form.get("delnum")) delete_time = int(request.form.get("delnum"))
assert delete_time >= 1 and delete_time <= 59 assert delete_time >= 1 and delete_time <= 59
except (ValueError, AssertionError): except (ValueError, AssertionError):
return 'Invalid entry: "delnum=' + request.form.get("delnum") + '"' return 'Invalid entry: "delnum=' + request.form.get("delnum") + '"'
del_dict = {"minute": 60, "hour": 3600, "day": 3600*24, "week": 3600*24*7} del_dict = {"minute": 60, "hour": 3600, "day": 3600*24, "week": 3600*24*7}
try: try:
delete_time *= del_dict[request.form.get("deltype")] delete_time *= del_dict[request.form.get("deltype")]
except KeyError: except KeyError:
return 'Invalid entry: "deltype=' + request.form.get("deltype")+'"' return 'Invalid entry: "deltype=' + request.form.get("deltype")+'"'
delete_time = int(time.time()) + delete_time delete_time = int(time.time()) + delete_time
db_execute( db_execute(
"INSERT INTO UPLOADS (filename, uploaded_by, delete_date)" "INSERT INTO UPLOADS (filename, uploaded_by, delete_date)"
"VALUES (?,?,?)", (fname, username, delete_time)) "VALUES (?,?,?)", (fname, username, delete_time))
else: else:
db_execute("INSERT INTO uploads (filename, uploaded_by) VALUES (?,?)", db_execute("INSERT INTO uploads (filename, uploaded_by) VALUES (?,?)",
(fname, username)) (fname, username))
file.save(os.path.join(fdir, fname)) file.save(os.path.join(fdir, fname))
url = app.config.get("UPLOAD_URL") + fname url = app.config.get("UPLOAD_URL") + fname
return url urls.append(url)
return "<br />".join(urls)
def get_rand_chars(n): def get_rand_chars(n):

View File

@ -10,7 +10,7 @@
<a href="{{ url_for('logout') }}">Logout</a><br /> <a href="{{ url_for('logout') }}">Logout</a><br />
<a href="{{ url_for('manage_uploads') }}">Manage Uploads</a><br /> <a href="{{ url_for('manage_uploads') }}">Manage Uploads</a><br />
<p>Select file to upload: <p>Select file to upload:
<p><input type="file" name="file" required/><br /> <p><input type="file" name="file" required multiple/><br />
<input type="checkbox" name="randname"/> Generate random filename.<br /> <input type="checkbox" name="randname"/> Generate random filename.<br />
<input type="checkbox" name="delflag"/> Delete this file in <input type="number" name="delnum" min="1" max="59" value="1"/> <input type="checkbox" name="delflag"/> Delete this file in <input type="number" name="delnum" min="1" max="59" value="1"/>
<select name="deltype"> <select name="deltype">