forked from horazont/xmpp-http-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxhu.py
174 lines (152 loc) · 5.11 KB
/
xhu.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
########################################################################
# File name: xhu.py
# This file is part of: xmpp-http-upload
#
# LICENSE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.
#
########################################################################
import atexit
import boto3
import contextlib
import errno
import flask
import fnmatch
import json
import hashlib
import hmac
import pathlib
import typing
import os
import sys
import time
from apscheduler.schedulers.background import BackgroundScheduler
from botocore.exceptions import ClientError
from datetime import datetime, timezone, timedelta
app = flask.Flask("xmpp-http-upload")
app.config.from_envvar("XMPP_HTTP_UPLOAD_CONFIG")
application = app
if app.config.get("S3_ENDPOINT_URL"):
s3 = boto3.resource("s3", endpoint_url=app.config["S3_ENDPOINT_URL"])
else:
s3 = boto3.resource("s3")
bucket = s3.Bucket(app.config["DATA_BUCKET"])
def remove_old_uploads():
for s3object in bucket.objects.all():
delta = (datetime.now(timezone.utc) - s3object.last_modified)
if (delta > timedelta(days = app.config["EXPIRE_DAYS"])):
s3object.delete()
if (int(app.config.get("EXPIRE_DAYS", 0)) > 0):
scheduler = BackgroundScheduler()
scheduler.add_job(func=remove_old_uploads, trigger="interval", hours=1)
scheduler.start()
atexit.register(lambda: scheduler.shutdown())
if app.config['ENABLE_CORS']:
from flask_cors import CORS
CORS(app)
@app.route("/")
def index():
return flask.Response(
"Welcome to XMPP HTTP Upload. State your business.",
mimetype="text/plain",
)
@app.route("/<path:path>", methods=["PUT"])
def put_file(path):
verification_key = flask.request.args.get("v", "")
length = int(flask.request.headers.get("Content-Length", 0))
hmac_input = "{} {}".format(path, length).encode("utf-8")
key = app.config["SECRET_KEY"]
mac = hmac.new(key, hmac_input, hashlib.sha256)
digest = mac.hexdigest()
if not hmac.compare_digest(digest, verification_key):
return flask.Response(
"Invalid verification key",
403,
mimetype="text/plain",
)
content_type = flask.request.headers.get(
"Content-Type",
"application/octet-stream",
)
try:
s3object = bucket.Object(path)
s3object.load()
except ClientError as e:
if e.response["Error"]["Code"] == "404":
s3object.upload_fileobj(flask.request.stream,
ExtraArgs={'ContentType': content_type})
return flask.Response(
"Created",
201,
mimetype="text/plain",
)
else:
raise
else:
return flask.Response(
"Conflict",
409,
mimetype="text/plain",
)
def generate_headers(response_headers):
content_type = response_headers["Content-Type"]
for mimetype_glob in app.config.get("NON_ATTACHMENT_MIME_TYPES", []):
if fnmatch.fnmatch(content_type, mimetype_glob):
break
else:
response_headers["Content-Disposition"] = "attachment"
response_headers["X-Content-Type-Options"] = "nosniff"
response_headers["X-Frame-Options"] = "DENY"
response_headers["Content-Security-Policy"] = "default-src 'none'; frame-ancestors 'none'; sandbox"
@app.route("/<path:path>", methods=["HEAD"])
def head_file(path):
try:
s3object = bucket.Object(path)
s3object.load()
except ClientError as e:
if e.response["Error"]["Code"] == "404":
return flask.Response(
"Not Found",
404,
mimetype="text/plain",
)
else:
raise
response = flask.Response()
response.headers["Content-Length"] = s3object.content_length
response.headers["Content-Type"] = s3object.content_type
generate_headers(response.headers)
return response
@app.route("/<path:path>", methods=["GET"])
def get_file(path):
try:
s3object = bucket.Object(path)
s3object.load()
except ClientError as e:
if e.response["Error"]["Code"] == "404":
return flask.Response(
"Not Found",
404,
mimetype="text/plain",
)
else:
raise
response = flask.make_response(flask.send_file(
s3object.get()['Body'], mimetype = s3object.content_type
))
response.headers["Content-Length"] = s3object.content_length
generate_headers(response.headers)
return response