split upload function from the index, made uploading from index return a proper html page, added /upload api endpoint

This commit is contained in:
iou1name 2018-06-12 12:12:42 -04:00
parent 992587fac7
commit 73e48d2209
3 changed files with 29 additions and 9 deletions

View File

@ -363,20 +363,17 @@ def gallery(username):
upload_url=app.config.get("UPLOAD_URL")) upload_url=app.config.get("UPLOAD_URL"))
@app.route("/", methods=["POST", "GET"]) @app.route("/upload", methods=["POST"])
@login_required("login") @login_required("login")
def index(): def upload():
""" """
Saves the uploaded file and returns a URL pointing to it. Saves the uploaded files and returns URLs pointing to them.
""" """
if request.method == "GET":
return render_template("index.html")
username = session.get("username") username = session.get("username")
if not username: if not username:
username = request.form.get("user") username = request.form.get("user")
urls = [] urls = []
for file in request.files.getlist("file"): for file in request.files.getlist("files"):
fname = secure_filename(file.filename) fname = secure_filename(file.filename)
pre = get_rand_chars(8) pre = get_rand_chars(8)
fdir = app.config.get("UPLOAD_DIR") fdir = app.config.get("UPLOAD_DIR")
@ -409,7 +406,19 @@ def index():
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
urls.append(url) urls.append(url)
return "<br />".join(urls) return "\n".join(urls)
@app.route("/", methods=["POST", "GET"])
@login_required("login")
def index():
"""
Saves the uploaded file and returns a URL pointing to it.
"""
if request.method == "GET":
return render_template("index.html")
urls = upload().split("\n")
return render_template("result.html", urls=urls)
def get_rand_chars(n): def get_rand_chars(n):

View File

@ -11,7 +11,7 @@
<a href="{{ url_for('manage_uploads') }}">Manage Uploads</a><br /> <a href="{{ url_for('manage_uploads') }}">Manage Uploads</a><br />
<a href="{{ url_for('gallery', username=session.username) }}">Public Gallery</a><br /> <a href="{{ url_for('gallery', username=session.username) }}">Public Gallery</a><br />
<p>Select file to upload: <p>Select file to upload:
<p><input type="file" name="file" required multiple/><br /> <p><input type="file" name="files" 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">

11
templates/result.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Upload Successful</title>
</head>
<body>
{% for url in urls %}
<a href="{{ url }}">{{ url }}</a><br />
{% endfor %}
</body>
</html>