forked from felixhummel/flask-extjs-file-upload-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·53 lines (45 loc) · 1.56 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
50
51
52
53
#!/usr/bin/env python
# vim: set fileencoding=utf-8 filetype=python :
import os
from flask import Flask, request, redirect, url_for
from flask import send_from_directory
from flask import render_template
from werkzeug import secure_filename
import subprocess
ALLOWED_EXTENSIONS = set(['c','cpp'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.path.join(os.path.dirname(__file__), 'uploads')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
@app.route('/uploads', methods=['POST'])
def upload_file():
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
file_url = '/uploads/%s'%filename
process = subprocess.Popen(["/usr/bin/emcc", app.config['UPLOAD_FOLDER']+'/'+filename, '-o ' + filename+'.bin'], stdout=subprocess.PIPE)
return '''\
{
"success":true, // note this is Boolean, not string
"msg":"Consignment updated",
"file": "%s",
"file_url": "%s"
}\
'''%(filename, file_url)
return '''\
{
"success": false, // note this is Boolean, not string
"msg":"Consignment updated"
}\
'''
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)