-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
69 lines (57 loc) · 2.48 KB
/
server.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, request, redirect, url_for, send_file, flash, render_template
from werkzeug.utils import secure_filename
from clear_files import clear_files
from buttons import create_buttons
import os
# Function declarations
# This function takes a directory and a file as input and returns the file as an attachment for HTML
def send_from_directory(directory: str, filename: str):
file = send_file(os.path.join(directory, filename), as_attachment=True)
os.system(f'rm uploads/{filename}')
return file
# Create an 'app' variable and set the static_folder to 'static'
app = Flask(__name__, static_folder='static')
app.config['SECRET_KEY'] = 'admin'
# creating uploads folder if it doesn't exist
if not os.path.exists('uploads'):
os.mkdir(os.path.join(os.getcwd(), 'uploads'))
app.config['UPLOAD_FOLDER'] = 'uploads'
# This route and function creates the index.html template
@app.route('/')
def home():
return render_template("index.html")
# This route and function creates the download page and calls the create_buttons function for the HTML code
@app.route('/download', methods=['GET', 'POST'])
def download_page():
files = os.listdir(app.config['UPLOAD_FOLDER'])
if request.method == 'POST':
for file in files:
if request.form.get(f'{file}') == f'{file}':
return send_from_directory(app.config['UPLOAD_FOLDER'], f'{file}')
return create_buttons(files)
# This route and function creates the upload page and calls the clear file function
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
# If the post request is 'clear'
if request.form.get('clear') == 'Clear Files':
# Then call clear_files and remove all cleared files
clear_files()
return "<h1>Files cleared!</h1>"
else:
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('upload_file', filename=filename))
return render_template('upload.html')
# Run the app
if __name__ == '__main__':
# On the Wi-Fi network and port 8000
app.run(host='0.0.0.0', port=8000)