-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
49 lines (37 loc) · 1.42 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import hashlib
import json
import os
from flask import Flask, request, send_file
app = Flask(__name__)
app.config["UPLOAD_FOLDER"] = os.getenv("UPLOAD_FOLDER", "/usr/share/nginx/html")
if not os.path.exists(app.config["UPLOAD_FOLDER"]):
os.makedirs(app.config["UPLOAD_FOLDER"])
@app.route("/")
def index():
return "You shouldn't be here."
@app.route("/save", methods=["POST"])
def save():
if "file" not in request.files:
return "no file uploaded", 400
file = request.files["file"]
if file.filename == "":
return "no filename found", 400
filename = hashlib.sha256(file.read()).hexdigest()
file.seek(0)
if "filename" in request.form:
name, ext = json.loads(request.form["filename"])
filename = "%s.%s.%s" % (name, filename, ext)
else:
if "prefix" in request.form:
filename = "%s_%s" % (request.form["prefix"], filename)
if "suffix" in request.form:
filename = "%s_%s" % (filename, request.form["suffix"])
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
return filename
# This route should be used for debugging filestore locally.
@app.route("/static/<string:path>")
def serve(path):
return send_file(os.path.join(app.config["UPLOAD_FOLDER"], path))
if __name__ == "__main__":
port = int(os.getenv("FILESTORE_PORT", "7910"))
app.run(use_debugger=True, use_reloader=True, port=port, host="0.0.0.0")