-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
82 lines (56 loc) · 1.91 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os, gridfs, pika, json
from flask import Flask, request, send_file
from flask_pymongo import PyMongo
from auth import validate
from auth_svc import access
from storage import util
from bson.objectid import ObjectId
server = Flask(__name__)
mongo_video = PyMongo(server, uri="mongodb://host.minikube.internal:27017/videos")
mongo_mp3 = PyMongo(server, uri="mongodb://host.minikube.internal:27017/mp3s")
fs_videos = gridfs.GridFS(mongo_video.db)
fs_mp3s = gridfs.GridFS(mongo_mp3.db)
connection = pika.BlockingConnection(pika.ConnectionParameters("rabbitmq"))
channel = connection.channel()
@server.route("/login", methods=["POST"])
def login():
token, err = access.login(request)
if not err:
return token
else:
return err
@server.route("/upload", methods=["POST"])
def upload():
access, err = validate.token(request)
if err:
return err
access = json.loads(access)
if access["admin"]:
if len(request.files) > 1 or len(request.files) < 1:
return "exactly 1 file required", 400
for _, f in request.files.items():
err = util.upload(f, fs_videos, channel, access)
if err:
return err
return "success!", 200
else:
return "not authorized", 401
@server.route("/download", methods=["GET"])
def download():
access, err = validate.token(request)
if err:
return err
access = json.loads(access)
if access["admin"]:
fid_string = request.args.get("fid")
if not fid_string:
return "fid is required", 400
try:
out = fs_mp3s.get(ObjectId(fid_string))
return send_file(out, download_name=f"{fid_string}.mp3")
except Exception as err:
print(err)
return "internal server error", 500
return "not authorized", 401
if __name__ == "__main__":
server.run(host="0.0.0.0", port=8080)