-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
38 lines (30 loc) · 1.16 KB
/
main.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
import webapp2
from google.appengine.api import urlfetch
from google.appengine.api import images
from google.appengine.ext import ndb
class Photo(ndb.Model):
image = ndb.BlobProperty()
class ImageHandler(webapp2.RequestHandler):
def get(self, width, height):
url = self.request.get('url')
key = '{}:{}:{}'.format(url, width, height)
height, width = [int(height), int(width)]
cached = Photo.get_by_id(key)
if cached is None:
img = urlfetch.fetch(url).content
output = images.resize(img, width=width, height=height)
cache = Photo(id = key, image = output)
cache.put()
else:
output = cached.image
self.response.headers['Content-Type'] = 'image/png'
self.response.headers['Cache-Control'] = 'max-age=3600'
self.response.write(output)
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('URL resize @ /img/w:<px>h:<px>url?')
app = webapp2.WSGIApplication([
('/', MainPage),
(r'/img/w:(\d+)h:(\d+)', ImageHandler),
], debug=True)