-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
77 lines (58 loc) · 2.05 KB
/
app.py
File metadata and controls
77 lines (58 loc) · 2.05 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from flask import Flask, render_template, request, send_file
import os
from datetime import datetime
from util import *
app = Flask(__name__)
# Directory to save uploaded files
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Ensure the upload directory exists
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return {
"status": "failed",
"message": "No file part in the request"
}
file = request.files['file']
if file.filename == '':
return {
"status": "failed",
"message": "No selected file"
}
if file:
try:
# Save the file to the server
filename, file_ext = file.filename.rsplit(".", 1)
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
filecode = generate_filecode()
output_filename = f"{filename}_{timestamp}___{filecode}___.{file_ext}"
file_path = os.path.join(app.config['UPLOAD_FOLDER'], output_filename)
file.save(file_path)
return {
"status": "ok",
"message": f"File {filename} uploaded successfully!",
"filecode": filecode
}
except Exception as e:
return {
"status": "failed",
"message": f"Server error occurred. {e}"
}
@app.route('/download', methods=['POST'])
def download_file():
download_code = request.form.get('download-code')
file_path, filename = find_my_file(UPLOAD_FOLDER, download_code)
if (file_path is None) or (filename is None):
return {
"status": "failed",
"message": f"Invalid code: {download_code}"
}
return send_file(file_path, as_attachment=True, download_name=filename, mimetype="application/octet-stream")
if __name__ == "__main__":
app.run(debug=True)