-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
70 lines (58 loc) · 2.14 KB
/
api.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
from flask import Flask, request
from pathlib import Path
from io import BytesIO
import tarfile
import os
app = Flask(__name__)
key = os.environ['UPLOAD_API_KEY']
@app.route('/api/upload', methods=['POST'])
def upload():
auth_header = request.headers.get('Authorization')
if not auth_header or auth_header != f"Bearer {key}":
return {'message': "not authorized"}, 401
event = request.args.get('event')
if not (event and (event == 'pr' or event == 'commit')):
return {'message': "missing type"}, 400
# validate event id
event_id = request.args.get('event_id')
if not event_id:
return {'message': "missing event_id"}, 400
if event == 'pr' and not event_id.isdecimal():
return {'message': f'invalid event_id for pr {event_id}'}, 400
if event == 'commit' and len(event_id) != 7:
return {'message': f'invalid event_id for commit {event_id}'}, 400
# validate namespace
namespace = request.args.get('namespace')
base_path = '/'
if namespace:
# prevent users from being able to use the parent folder '..'
# to escape the upload www directory
sanitized = namespace.replace(r"\.+", ".")
# normalize namespace to construct base path
base_path = f'/{sanitized.strip("/")}/'
if (base_path.startswith('/pr/')
or base_path.startswith('/commit/')
or base_path.startswith('/api/')):
return {'message': "invalid namespace supplied"}, 400
tarball = BytesIO()
while True:
chunk = request.stream.read(4096)
if len(chunk) == 0:
tarball.seek(0)
break
tarball.write(chunk)
try:
build = tarfile.open(fileobj=tarball, mode="r:gz")
except tarfile.ReadError as e:
print(e)
return {'message': "not a tarfile"}, 400
target_path = f"{base_path}{event}/{event_id}"
disk_path = f"www{target_path}"
Path(disk_path).mkdir(parents=True, exist_ok=True)
build.extractall(disk_path)
return {
'path': target_path,
'url': f"https://staging.archit.us{target_path}"
}, 200
if __name__ == '__main__':
app.run(port=5454)