-
Notifications
You must be signed in to change notification settings - Fork 4
/
thumbnail.py
49 lines (38 loc) · 1.51 KB
/
thumbnail.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
#!/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
from md5s3stash import md5_to_http_url
import os
assert 'BUCKET_BASE' in os.environ, "`BUCKET_BASE` must be set"
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,))