-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
90 lines (70 loc) · 2.72 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
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
85
86
87
88
89
90
from flask import (
Flask,
request,
render_template,
send_from_directory,
url_for,
jsonify
)
from werkzeug import secure_filename
import os
import time
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
from logging import Formatter, FileHandler
handler = FileHandler(os.path.join(basedir, 'log.txt'), encoding='utf8')
handler.setFormatter(
Formatter("[%(asctime)s] %(levelname)-8s %(message)s", "%Y-%m-%d %H:%M:%S")
)
app.logger.addHandler(handler)
app.config['UPLOAD_FOLDER'] = os.path.join(basedir, 'upload/')
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.context_processor
def override_url_for():
return dict(url_for=dated_url_for)
def dated_url_for(endpoint, **values):
if endpoint == 'js_static':
filename = values.get('filename', None)
if filename:
file_path = os.path.join(app.root_path,
'static/js', filename)
values['q'] = int(os.stat(file_path).st_mtime)
elif endpoint == 'css_static':
filename = values.get('filename', None)
if filename:
file_path = os.path.join(app.root_path,
'static/css', filename)
values['q'] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
@app.route('/css/<path:filename>')
def css_static(filename):
return send_from_directory(app.root_path + '/static/css/', filename)
@app.route('/img/<path:filename>')
def img_static(filename):
return send_from_directory(app.root_path + '/static/img/', filename)
@app.route('/js/<path:filename>')
def js_static(filename):
return send_from_directory(app.root_path + '/static/js/', filename)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/uploadajax', methods=['POST'])
def upldfile():
if request.method == 'POST':
files = request.files['file']
if files and allowed_file(files.filename):
filename = secure_filename(files.filename)
app.logger.info('FileName: ' + filename)
updir = app.config['UPLOAD_FOLDER']
time.sleep(3)
files.save(os.path.join(updir, filename))
file_size = os.path.getsize(os.path.join(updir, filename))
return jsonify(name=filename, size=file_size)
@app.route('/download/<path:filename>', methods=['GET', 'POST'])
def download(filename):
return send_from_directory(directory=app.config['UPLOAD_FOLDER'], filename=filename)
if __name__ == '__main__':
app.run(debug=True, port=5001)