-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
81 lines (64 loc) · 2.2 KB
/
server.py
File metadata and controls
81 lines (64 loc) · 2.2 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
#!/usr/bin/python3
import lib.utils as utils
import logging
import graphene
from flask import Flask, send_file, after_this_request
from flask_graphql import GraphQLView
from models.api import DemucsServiceAPI
from pathlib import Path
from flask_cors import CORS
from typing import Optional
class DemucsInternalException(Exception):
def __init__(self, msg):
super().__init__(msg)
app = Flask(__name__)
CORS(app)
schema = graphene.Schema(query=DemucsServiceAPI)
@app.errorhandler(FileNotFoundError)
def file_not_found(err):
# app.logger.exception(err)
return "The file you are trying to access is not longer available", 404
@app.errorhandler(DemucsInternalException)
def internal_error(err):
# app.logger.exception(err)
return "The file you are trying to access is not longer available", 500
@app.route('/download/<token>')
def gen_download(token):
logging.info(f"Received download request for: {token}")
download_file: Optional[Path] = utils.get_download_file(token)
if download_file:
try:
# if download_file is not None, then this token
# was found on the DB
utils.disable_download(token)
failed = False
@after_this_request
def remove_file(response):
if not failed:
try:
utils.remove_download_file(download_file)
except Exception as e:
logging.error(f"Unable to remove the download: {e}")
return response
return send_file(
download_file,
as_attachment=True,
attachment_filename=download_file.name,
mimetype='application/zip'
)
except Exception as e:
failed = True
logging.error(f"An error ocurred while handling your request {e}")
raise DemucsInternalException(e) from e
else:
raise FileNotFoundError("The download request is not longer available")
app.add_url_rule(
'/graphql',
view_func=GraphQLView.as_view(
'graphql',
schema=schema,
graphiql=True
)
)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)