-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_server.py
More file actions
106 lines (82 loc) · 3.31 KB
/
Copy pathfile_server.py
File metadata and controls
106 lines (82 loc) · 3.31 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
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
import os
import datetime
import statistics
from flask import Flask, request, jsonify, send_file
app = Flask(__name__)
FILES_FOLDER = 'files_folder'
if not os.path.exists(FILES_FOLDER):
os.makedirs(FILES_FOLDER)
MAX_FILE_SIZE_BYTES = 100 * 1024 * 1024
app.config['MAX_CONTENT_LENGTH'] = MAX_FILE_SIZE_BYTES
@app.route('/file', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'})
if file.content_length > MAX_FILE_SIZE_BYTES:
return jsonify({'error': f'File size exceeds the maximum allowed limit of {MAX_FILE_SIZE_BYTES} bytes'})
filename = os.path.join(FILES_FOLDER, file.filename)
file.save(filename)
file_info = {
'name': file.filename,
'type': file.content_type,
'size_bytes': os.path.getsize(filename),
'datetime_uploaded': datetime.datetime.now().isoformat()
}
return jsonify({'message': f'File {file.filename} uploaded successfully', 'file_info': file_info})
@app.route('/file/<filename>', methods=['GET'])
def download_file(filename):
file_path = os.path.join(FILES_FOLDER, filename)
if os.path.isfile(file_path):
return send_file(file_path, as_attachment=True)
else:
return jsonify({'error': 'File not found'})
@app.route('/file/<filename>', methods=['DELETE'])
def delete_file(filename):
file_path = os.path.join(FILES_FOLDER, filename)
if os.path.isfile(file_path):
os.remove(file_path)
return jsonify({'message': f'File {filename} deleted successfully'})
else:
return jsonify({'error': 'File not found'})
@app.route('/file', methods=['GET'])
def list_files():
files = os.listdir(FILES_FOLDER)
filter_type = request.args.get('type')
if filter_type:
files = [f for f in files if f.endswith(filter_type)]
sort_by_size = request.args.get('sort_by_size')
if sort_by_size == 'asc':
files.sort(key=lambda f: os.path.getsize(os.path.join(FILES_FOLDER, f)))
elif sort_by_size == 'desc':
files.sort(key=lambda f: os.path.getsize(os.path.join(FILES_FOLDER, f)), reverse=True)
file_info = []
for filename in files:
file_path = os.path.join(FILES_FOLDER, filename)
file_info.append({
'name': filename,
'type': os.path.splitext(filename)[1],
'size_bytes': os.path.getsize(file_path),
'datetime_uploaded': datetime.datetime.fromtimestamp(
os.path.getctime(file_path)
).isoformat()
})
return jsonify({'files': file_info})
@app.route('/file/statistics', methods=['GET'])
def file_statistics():
files = os.listdir(FILES_FOLDER)
sizes = [os.path.getsize(os.path.join(FILES_FOLDER, f)) for f in files]
num_files = len(files)
total_size_bytes = sum(sizes)
avg_size_bytes = total_size_bytes / num_files
median_size_bytes = statistics.median(sizes)
return jsonify({
'num_of_files': num_files,
'total_size_bytes': total_size_bytes,
'avg_size_bytes': int(avg_size_bytes),
'median_size_bytes': int(median_size_bytes)
})
if __name__ == '__main__':
app.run(debug=True)