-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
84 lines (63 loc) · 1.92 KB
/
ui.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
# -*- coding: utf-8 -*-
""""
Author: Jan Harasym <[email protected]>
Website: dijit.sh
Creation Date: 2015-10-22
Last Modified: Thu 22 Oct 21:25:38 2015
Description:
Expected Use:
"""
from flask import Flask, request, redirect, url_for, render_template
import os
import json
import glob
from uuid import uuid4
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/upload", methods=["POST"])
def upload():
"""Handle the upload of a file."""
form = request.form
# Create a unique "session ID" for this particular batch of uploads.
upload_key = str(uuid4())
# Is the upload using Ajax, or a direct POST by the form?
is_ajax = False
if form.get("__ajax", None) == "true":
is_ajax = True
print "=== Form Data ==="
for key, value in form.items():
print key, "=>", value
for upload in request.files.getlist("file"):
filename = upload.filename.rsplit("/")[0]
destination = "/".join([target, filename])
print "Accept incoming file:", filename
print "Save it to:", destination
upload.save(destination)
if is_ajax:
return ajax_response(True, upload_key)
else:
return redirect(url_for("upload_complete", uuid=upload_key))
@app.route("/files/<uuid>")
def upload_complete(uuid):
"""The location we send them to at the end of the upload."""
# Get their files.
root = "uploadr/static/uploads/{}".format(uuid)
if not os.path.isdir(root):
return "Error: UUID not found!"
files = []
for file in glob.glob("{}/*.*".format(root)):
fname = file.split("/")[-1]
files.append(fname)
return render_template("files.html",
uuid=uuid,
files=files,
)
def ajax_response(status, msg):
status_code = "ok" if status else "error"
return json.dumps(dict(
status=status_code,
msg=msg,
))