This repository was archived by the owner on Jan 28, 2025. It is now read-only.
forked from tingletech/md5s3stash
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththumbnail.py
More file actions
66 lines (53 loc) · 2 KB
/
thumbnail.py
File metadata and controls
66 lines (53 loc) · 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
#!/usr/bin/env python
""" thumbnail server for md5s3stash
extension to pilbox server http://agschwender.github.io/pilbox/#extension
"""
import tornado.gen
from pilbox.app import PilboxApplication, ImageHandler, main
import os
assert 'BUCKET_BASE' in os.environ, "`BUCKET_BASE` must be set"
def md5_to_http_url(md5, bucket_base, bucket_scheme='multibucket', s3_endpoint='s3.amazonaws.com'):
""" calculate the http URL given an md5 and an bucket_base """
if bucket_scheme == 'simple':
url = "http://{0}/{1}/{2}".format(
s3_endpoint,
bucket_base,
md5
)
elif bucket_scheme == 'multibucket':
url = "http://{1}.{2}.{0}/{3}".format(
s3_endpoint,
md5_to_bucket_shard(md5),
bucket_base,
md5
)
return url
class ThumbnailApplication(PilboxApplication):
def get_handlers(self):
# URL regex to handler mapping
return [
(r"^/([^/]+)/(\d+)x(\d+)/([a-fA-F\d]{32})$", ThumbnailImageHandler),
(r"^/([^/]+)/(\d+)x(\d+)/.*$", ThumbnailImageHandler)
]
# mode, w, h, md5
class ThumbnailImageHandler(ImageHandler):
def prepare(self):
self.args = self.request.arguments.copy()
self.settings['content_type_from_image'] = True
@tornado.gen.coroutine
def get(self, mode, w, h, md5='0d6cc125540194549459df758af868a8'):
url = md5_to_http_url(
md5,
os.environ['BUCKET_BASE'],
bucket_scheme=os.getenv('BUCKET_SCHEME', 'multibucket'),
s3_endpoint=os.getenv('S3_ENDPOINT'),
)
self.args.update(dict(w=w, h=h, url=url, mode=mode))
self.validate_request()
resp = yield self.fetch_image()
resp.headers["Cache-Control"] = "public, max-age=31536000"
self.render_image(resp)
def get_argument(self, name, default=None):
return self.args.get(name, default)
if __name__ == "__main__":
main(app=ThumbnailApplication(timeout=30,))