-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
65 lines (53 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
from flask import Flask, render_template, request, send_from_directory, redirect, url_for
import os
from datetime import datetime
import math
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'static/files'
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # 100MB max file size
app.config['FILES_PER_PAGE'] = 20 # Number of files to show per page
def get_file_info(filename):
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
stats = os.stat(filepath)
return {
'name': filename,
'size': stats.st_size,
'modified': datetime.fromtimestamp(stats.st_mtime),
}
# Ensure upload folder exists
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
@app.route('/')
def index():
page = request.args.get('page', 1, type=int)
files = []
# Get all files with their info
for filename in os.listdir(app.config['UPLOAD_FOLDER']):
files.append(get_file_info(filename))
# Sort files by modified date, newest first
files.sort(key=lambda x: x['modified'], reverse=True)
# Pagination
total_files = len(files)
total_pages = math.ceil(total_files / app.config['FILES_PER_PAGE'])
start_idx = (page - 1) * app.config['FILES_PER_PAGE']
end_idx = start_idx + app.config['FILES_PER_PAGE']
files_page = files[start_idx:end_idx]
return render_template('index.html',
files=files_page,
page=page,
total_pages=total_pages)
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
if file:
filename = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('index'))
@app.route('/files/<filename>')
def files(filename): # Changed from download_file to files
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')