-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
70 lines (59 loc) · 2.09 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
from flask import Flask, render_template, jsonify, send_from_directory, request
from werkzeug.utils import secure_filename
import os
import uuid
app = Flask(__name__)
UPLOAD_FOLDER='upload'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
basedir = os.path.abspath(os.path.dirname(__file__))
ALLOWED_EXTENSIONS = set(['txt','png','jpg','gif','pdf','epub'])
IGNORED_FILES = set(['.gitignore'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS
def init_dir():
file_dir=os.path.join(basedir,app.config['UPLOAD_FOLDER'])
if not os.path.exists(file_dir):
os.makedirs(file_dir)
return file_dir
@app.route('/')
def index():
return render_template('index.html', title='Duokan')
@app.route('/files')
def load():
file_dir = init_dir()
files_saved = [f for f in os.listdir(file_dir) if os.path.isfile(os.path.join(file_dir, f)) and f not in IGNORED_FILES ]
files = []
number = 0
for f in files_saved:
size = os.path.getsize(os.path.join(app.config['UPLOAD_FOLDER'], f))
number += 1
files.append({
"id": number,
"size": size,
"name": f,
"path": ""
})
return jsonify(files)
@app.route('/files/<string:filename>')
def download(filename):
return send_from_directory(os.path.join(app.config['UPLOAD_FOLDER']), filename=filename, as_attachment=True)
@app.route('/files', methods=['POST'])
def upload():
file_dir = init_dir()
file = request.files['newfile']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(file_dir, filename))
return jsonify({"result":"OK"})
else:
return jsonify({"result":"error"})
@app.route("/files/<string:filename>", methods=['DELETE'])
def delete(filename):
file_dir = init_dir()
file_path = os.path.join(file_dir, filename)
if os.path.exists(file_path):
try:
os.remove(file_path)
return jsonify({"result":"OK"})
except:
return jsonify({"result":"error"})