-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (44 loc) · 1.72 KB
/
Copy pathapp.py
File metadata and controls
59 lines (44 loc) · 1.72 KB
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
# -*- coding: utf-8 -*-
import os
import time
import hashlib
from flask import Flask, render_template, redirect, url_for, request
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileRequired, FileAllowed
from wtforms import SubmitField
app = Flask(__name__)
app.config['SECRET_KEY'] = 'I have a dream'
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd() + '/static'
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
patch_request_class(app) # set maximum file size, default is 16MB
class UploadForm(FlaskForm):
photo = FileField(validators=[FileAllowed(photos, u'Image Only!'), FileRequired(u'Choose a file!')])
submit = SubmitField(u'Upload')
@app.route('/', methods=['GET', 'POST'])
def upload_file():
form = UploadForm()
if form.validate_on_submit():
for filename in request.files.getlist('photo'):
name = hashlib.md5('admin' + str(time.time())).hexdigest()[:15]
photos.save(filename, name=name + '.')
success = True
else:
success = False
return render_template('index.html', form=form, success=success)
@app.route('/manage')
def manage_file():
files_list = os.listdir(app.config['UPLOADED_PHOTOS_DEST'])
return render_template('manage.html', files_list=files_list)
@app.route('/open/<filename>')
def open_file(filename):
file_url = photos.url(filename)
return render_template('browser.html', file_url=file_url)
@app.route('/delete/<filename>')
def delete_file(filename):
file_path = photos.path(filename)
os.remove(file_path)
return redirect(url_for('manage_file'))
if __name__ == '__main__':
app.run(debug=True)