-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_server.py
61 lines (50 loc) · 1.77 KB
/
web_server.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
# -*- coding: utf-8 -*-
"""
:author: Grey Li <[email protected]>
:copyright: (c) 2017 by Grey Li.
:license: MIT, see LICENSE for more details.
"""
import os
import hashlib
import random
from flask import Flask, render_template, request
from flask_dropzone import Dropzone
basedir = os.path.abspath(os.path.dirname(__file__))
os.makedirs(os.path.join(basedir, 'uploads'), exist_ok=True)
app = Flask(__name__)
app.config.update(
UPLOADED_PATH=os.path.join(basedir, 'static', 'uploads'),
DONE_PATH=os.path.join(basedir, 'static', 'done'),
# Flask-Dropzone config:
DROPZONE_ALLOWED_FILE_TYPE='image',
DROPZONE_MAX_FILE_SIZE=5,
DROPZONE_MAX_FILES=1,
)
dropzone = Dropzone(app)
os.makedirs(app.config['UPLOADED_PATH'], exist_ok=True)
os.makedirs(app.config['DONE_PATH'], exist_ok=True)
@app.route('/', methods=['POST', 'GET'])
def upload():
if request.method == 'POST':
f = request.files.get('file')
data = f.read()
sha256 = hashlib.sha256(data).hexdigest()
_, ext = os.path.splitext(f.filename)
fp = open(os.path.join(app.config['UPLOADED_PATH'], "%s%s" % (sha256, ext)), 'wb')
fp.write(data)
fp.close()
return sha256
return render_template('index.html')
@app.route('/jobs', methods=['POST', 'GET'])
def jobs():
if request.method == 'POST':
f = request.files.get('file')
f.save(os.path.join(app.config['DONE_PATH'], f.filename)) # sha256.jpg
return f.filename
done = list(map(lambda x: x.split('.')[0], os.listdir(app.config['DONE_PATH'])))
jobs = list(filter(lambda x: x.split('.')[0] not in done, os.listdir(app.config['UPLOADED_PATH'])))
if not jobs:
return "empty"
return random.choice(jobs)
if __name__ == '__main__':
app.run(debug=True)