-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
52 lines (43 loc) · 1.74 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import urllib
from google.appengine.api import app_identity
from google.appengine.api import memcache
from google.appengine.api import images
from google.appengine.api import blobstore
import webapp2
import cloudstorage
import urllib
GCS_BUCKET = app_identity.get_default_gcs_bucket_name()
def create_file(filename, contents, mime_type):
filename = "/" + GCS_BUCKET + "/" + filename
# Create a GCS file with GCS client.
with cloudstorage.open(filename, 'w', content_type=mime_type) as f:
f.write(contents.read())
# Blobstore API requires extra /gs to distinguish against blobstore files.
blobstore_filename = '/gs' + filename
# This blob_key works with blobstore APIs that do not expect a
# corresponding BlobInfo in datastore.
return blobstore.create_gs_key(blobstore_filename)
def fetch_and_store(url):
res = urllib.urlopen(url)
mime = res.headers.get("content-type")
if not mime or mime.lower() not in ["image/jpeg", "image/png", "image/gif"]:
raise ValueError("Unsupported image type: %s" % (mime))
filename = urllib.quote_plus(url)
return create_file(filename, res, mime)
class MainHandler(webapp2.RequestHandler):
def get(self, size):
url = self.request.get("url")
key = memcache.get(url)
if not key:
key = fetch_and_store(url)
memcache.set(url, key)
try:
img_url = images.get_serving_url(key, secure_url=True, size=int(size))
except images.ObjectNotFoundError:
key = fetch_and_store(url)
memcache.set(url, key)
img_url = images.get_serving_url(key, secure_url=True)
self.redirect(img_url, permanent=True)
app = webapp2.WSGIApplication([
webapp2.Route("/<size>", MainHandler)
], debug=True)