-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-server.py
More file actions
67 lines (53 loc) · 2.24 KB
/
Copy pathhttp-server.py
File metadata and controls
67 lines (53 loc) · 2.24 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
from flask import Flask, request, redirect, url_for, send_from_directory
import os
app = Flask(__name__)
# Directory where uploaded files will be saved
UPLOAD_FOLDER = 'uploads'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/')
def index():
# Display the upload form and list of uploaded files
files = []
for root, dirs, filenames in os.walk(app.config['UPLOAD_FOLDER']):
for filename in filenames:
# Collect relative paths of all files
relative_path = os.path.relpath(os.path.join(root, filename), app.config['UPLOAD_FOLDER'])
files.append(relative_path)
return '''
<h1>Upload a Folder</h1>
<form method="POST" enctype="multipart/form-data" action="/upload" multiple>
<input type="file" name="files" webkitdirectory multiple>
<input type="submit" value="Upload">
</form>
<h2>Uploaded Files</h2>
<ul>
''' + ''.join([f'<li><a href="/download/{file}">{file}</a></li>' for file in files]) + '''
</ul>
'''
@app.route('/upload', methods=['POST'])
def upload_files():
if 'files' not in request.files:
return 'No files part'
files = request.files.getlist('files')
if not files:
return 'No selected files'
for file in files:
# Print the original filename to debug path structure
# print(f"Received file: {file.filename}")
if file.filename:
# Debug: Check if filename includes folder structure
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
# Print path to confirm the subfolder structure is maintained
# print(f"Saving to: {file_path}")
# Ensure the directory exists for the file
os.makedirs(os.path.dirname(file_path), exist_ok=True)
# Save file to the designated path
file.save(file_path)
return redirect(url_for('index'))
@app.route('/download/<path:filename>')
def download_file(filename):
# Serve the requested file for download, preserving nested directory structure
return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)