-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver_celery.py
83 lines (62 loc) · 2.38 KB
/
server_celery.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
import os
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename
import generate_thumbnail_celery as generate_thumbnail
from os.path import basename
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
import time
start_time = time.time()
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
base,ext=os.path.splitext(filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
generate_thumbnail.generate.delay(os.path.join(app.config['UPLOAD_FOLDER'], filename),os.path.join(app.config['UPLOAD_FOLDER'], base+"-face"+ext))
print "--- %s seconds ---" % str (time.time() - start_time)
return redirect("/")
return redirect(url_for('uploaded_file',
filename="facedetect-"+filename))
from os import listdir
from os.path import isfile, join
htmlpic=""
for f in sorted(listdir(UPLOAD_FOLDER)):
if isfile(join(UPLOAD_FOLDER,f)):
print f
htmlpic+="""
<a href='uploads/"""+f+"""'>
<img src='uploads/"""+f+"""'>
</a>
"""
return '''
<!doctype html>
<head>
</head>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''+htmlpic
from flask import send_from_directory
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
from werkzeug import SharedDataMiddleware
app.add_url_rule('/uploads/<filename>', 'uploaded_file',
build_only=True)
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/uploads': app.config['UPLOAD_FOLDER']
})
if __name__ == "__main__":
app.debug=True
app.run(host='0.0.0.0', port=5000)