-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (52 loc) · 2.61 KB
/
main.py
File metadata and controls
58 lines (52 loc) · 2.61 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
from flask import Flask, request, send_file, current_app
import uuid
import os
app = Flask(__name__)
content_types = {
".jpeg": "image/jpeg", ".jpg": "image/jpeg", ".png": "image/png", ".gif": "image/gif", ".webp": "image/webp",
".bmp": "image/bmp", ".svg": "image/svg+xml", ".ico": "image/x-icon",
".mp4": "video/mp4", ".webm": "video/webm", ".mkv": "video/x-matroska",
".mov": "video/quicktime", ".ogv": "video/ogg", ".flv": "video/x-flv",
".mp3": "audio/mpeg", ".aac": "audio/aac", ".wav": "audio/wav", ".flac": "audio/flac",
".ogg": "audio/ogg", ".opus": "audio/opus", ".m4a": "audio/mp4",
".pdf": "application/pdf", ".zip": "application/zip", ".php": "application/x-php",
".html": "text/html", ".css": "text/css", ".js": "application/javascript",
".json": "application/json", ".xml": "application/xml", ".txt": "text/plain", ".csv": "text/csv",
".woff": "font/woff", ".woff2": "font/woff2", ".rar": "application/vnd.rar", ".torrent": "application/x-bittorrent",
}
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization,Cache-Control')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
return response
@app.route('/peer/upload', methods=['POST'])
def peerupload():
file = request.files['file']
if file is None:
return "", 200
uploadpath = os.path.join(current_app.root_path, 'static', 'peerdata')
os.makedirs(uploadpath, exist_ok=True)
filename = str(uuid.uuid4()) + os.path.splitext(file.filename)[1]
filepath = os.path.join(uploadpath, filename)
try:
file.save(filepath)
baseurl = request.host_url
if request.headers.get('X-Forwarded-Proto') == 'https' or request.headers.get('X-Forwarded-Scheme') == 'https' or request.headers.get('X-Scheme') == 'https':
baseurl = baseurl.replace('http://', 'https://')
return f"{baseurl.rstrip('/')}/peer/{filename}", 200
except Exception:
return "", 200
@app.route('/peer/<path:filename>')
def peerload(filename):
filepath = os.path.join(current_app.root_path, 'static', 'peerdata', filename)
if not os.path.exists(filepath):
return "", 404
extension = os.path.splitext(filename)[1]
content_type = content_types.get(extension.lower(), 'application/octet-stream')
return send_file(filepath, mimetype=content_type)
@app.route('/')
def peerindex():
return send_file(os.path.join('static', 'peerdata', 'index.html'))
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False)