-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
61 lines (43 loc) · 1.67 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
import os
from flask import Flask, json, request
from werkzeug.utils import secure_filename
app=Flask(__name__)
app.secret_key = "secret key"
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
path = os.getcwd()
DOWNLOAD_FOLDER = os.path.join(path, 'downloads/newUploads/')
# Make directory for downloads if it doesn't exist
if not os.path.isdir(DOWNLOAD_FOLDER):
os.makedirs(DOWNLOAD_FOLDER)
print(f'{DOWNLOAD_FOLDER} created')
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER
# Allowed extensions
ALLOWED_EXTENSIONS = set(['mp4', 'xml'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def hello():
return 'Hello World! API is running'
@app.route('/uploads', methods=['PUT'])
def upload_file():
if request.method == 'PUT':
if 'files' not in request.files:
return "No files found", 400
files = request.files.getlist('files')
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['DOWNLOAD_FOLDER'], filename))
print(f'{file.filename} DOWNLOADED')
return json.dumps({"success": True}), 201
@app.route('/delete/<path:filename>', methods=['DELETE'])
def delete_file(filename):
try:
url = os.path.join(app.config['DOWNLOAD_FOLDER'], filename)
os.remove(url)
print(f'{filename} DELETED')
return json.dumps({"success": True}), 200
except FileNotFoundError as e:
return e.args, 204
if __name__ == '__main__':
app.run(host="localhost", port=8081, threaded = True)