-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
154 lines (130 loc) · 5.97 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import psutil
# We'll render HTML templates and access data sent by POST
# using the request object from flask. Redirect and url_for
# will be used to redirect the user once the upload is done
# and send_from_directory will help us to send/show on the
# browser the file that the user just uploaded
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
from werkzeug import secure_filename
# Initialize the Flask application
app = Flask(__name__)
# This is the path to the upload directory
app.config['UPLOAD_FOLDER'] = '/upload/'
# These are the extension that we are accepting to be uploaded
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'vmdk', 'ova', 'log', 'mf', 'ovf', 'gz', 'zip', 'qcow2', 'iso', 'exe', 'rpm', 'tar'])
# For a given file, return whether it's an allowed type or not
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
# disk usage for upload folder
def diskusage(partition):
disk = psutil.disk_usage(partition)
# Divide from Bytes -> KB -> MB -> GB
free = round(disk.free/1024.0/1024.0/1024.0,1)
total = round(disk.total/1024.0/1024.0/1024.0,1)
return str(free) + 'GB free / ' + str(total) + 'GB total ( ' + str(disk.percent) + '% )'
# This route will show a form to perform an AJAX request
# jQuery is loaded to execute the request and update the
# value of the operation
@app.route('/')
def index():
return render_template('index.html', disk=diskusage(app.config['UPLOAD_FOLDER']))
@app.route('/<user>')
def index_user(user):
directory = '/upload/%s' % user
if not os.path.isdir(directory):
os.makedirs(directory)
return render_template('index.html', user=user, disk=diskusage(app.config['UPLOAD_FOLDER']))
# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
# Get the name of the uploaded files
uploaded_files = request.files.getlist("file[]")
filenames = []
for file in uploaded_files:
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
# Move the file form the temporal folder to the upload
# folder we setup
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Save the filename into a list, we'll use it later
filenames.append(filename)
# Redirect the user to the uploaded_file route, which
# will basicaly show on the browser the uploaded file
# Load an html page with a link to each uploaded file
return render_template('upload.html', filenames=filenames, disk=diskusage(app.config['UPLOAD_FOLDER']))
# Route that will process the file upload
@app.route('/<user>/upload', methods=['POST'])
def upload_user(user):
# Get the name of the uploaded files
uploaded_files = request.files.getlist("file[]")
filenames = []
for file in uploaded_files:
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
# Move the file form the temporal folder to the upload
# folder we setup
file.save(os.path.join(app.config['UPLOAD_FOLDER'] + "/" + user, filename))
# Save the filename into a list, we'll use it later
filenames.append(filename)
# Redirect the user to the uploaded_file route, which
# will basicaly show on the browser the uploaded file
# Load an html page with a link to each uploaded file
return render_template('upload.html', filenames=filenames, user=user, disk=diskusage(app.config['UPLOAD_FOLDER']))
# This route is expecting a parameter containing the name
# of a file. Then it will locate that file on the upload
# directory and show it on the browser, so if the user uploads
# an image, that image is going to be show after the upload
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
# This route is expecting a parameter containing the name
# of a file. Then it will locate that file on the upload
# directory and show it on the browser, so if the user uploads
# an image, that image is going to be show after the upload
@app.route('/<user>/uploads/<filename>')
def uploaded_file_user(filename,user):
return send_from_directory(app.config['UPLOAD_FOLDER'] + "/" + user,
filename)
@app.route('/uploads', defaults={'path': ''})
@app.route('/uploads/<path:path>')
def uploads(path):
BASE_DIR = '/upload'
# Joining the base and the requested path
abs_path = os.path.join(BASE_DIR, path)
# Return 404 if path doesn't exist
if not os.path.exists(abs_path):
return abort(404)
# Check if path is a file and serve
if os.path.isfile(abs_path):
return send_file(abs_path)
# Show directory contents
files = os.listdir(abs_path)
return render_template('files.html', files=files, disk=diskusage(app.config['UPLOAD_FOLDER']))
@app.route('/<user>/uploads')
def uploads_user(user):
BASE_DIR = '/upload/%s' % user
# Joining the base and the requested path
abs_path = os.path.join(BASE_DIR)
# Return 404 if path doesn't exist
if not os.path.exists(abs_path):
return abort(404)
# Check if path is a file and serve
if os.path.isfile(abs_path):
return send_file(abs_path)
# Show directory contents
files = os.listdir(abs_path)
return render_template('files.html', files=files, user=user, disk=diskusage(app.config['UPLOAD_FOLDER']))
if __name__ == '__main__':
app.run(
host="0.0.0.0",
port=int("8000"),
threaded=True,
debug=True
)