This repository has been archived by the owner on Apr 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
lambda.py
executable file
·86 lines (63 loc) · 2.33 KB
/
lambda.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
import os
from base64 import b64encode
import boto3
page_template = "<!DOCTYPE html><html><body><ul>{}</ul></body></html>"
list_template = '<li><a href="{path}">{name}</a>'
S3_BUCKET = os.environ.get("S3_BUCKET")
FILE_TYPES = {"egg", "whl", "gz"}
s3 = boto3.resource("s3")
def is_allowed_type(path):
return os.path.splitext(path)[1][1:] in FILE_TYPES
def get_objects():
bucket = s3.Bucket(S3_BUCKET)
return {obj.key: obj for obj in bucket.objects.all()}
def parse_path(path):
package, *filename = path.split("/")
return package, "".join(filename)
def handler(event, context):
objects = get_objects()
path = event.get("path", "/")[1:]
package, filename = parse_path(path)
if package:
if filename:
# download file
if path in objects and is_allowed_type(path):
summary = objects[path]
obj = s3.Object(summary.bucket_name, summary.key)
local_path = f"/tmp/{filename}"
obj.download_file(local_path)
with open(local_path, "rb") as file:
data = file.read()
data = b64encode(data)
return {
"isBase64Encoded": True,
"headers": {
"Content-Disposition": f"attachment; filename={filename}",
"Content-Type": "application/zip, application/octet-stream",
},
"body": data.decode(),
}
else:
raise Exception("Invalid file requested.")
# list package files
buffer = len(package) + 1
items = {}
for key in objects:
print(package, key)
filename = key[buffer:] if key.startswith(package) else key
print(filename)
if "/" not in filename and is_allowed_type(filename):
items[filename] = filename
else:
# list packages
items = {
key: key for key in objects if key.endswith("/") and "/" not in key[:-1]
}
listing = sorted(
list_template.format(path=key, name=value) for key, value in items.items()
)
return {
"isBase64Encoded": False,
"headers": {"Content-Type": "text/html"},
"body": page_template.format("\n".join(listing)),
}