forked from m-murad/compressor-head
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (82 loc) · 3.17 KB
/
main.py
File metadata and controls
97 lines (82 loc) · 3.17 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
#
# Copyright 2017 Murad.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from google.appengine.api import images
from google.appengine.api import memcache
from google.appengine.api import urlfetch
import logging
import webapp2
class HomeHandler(webapp2.RequestHandler):
def get(self):
self.response.write('This is CompressorHead.')
def set_output_format(requested_format):
if requested_format.upper() == 'JPEG':
output_format = images.JPEG
requested_format = 'jpeg'
elif requested_format.upper() == 'PNG':
output_format = images.PNG
requested_format = 'png'
elif requested_format.upper() == 'WEBP':
output_format = images.WEBP
requested_format = 'webp'
else: # Default format is JPEG.
output_format = images.JPEG
requested_format = 'jpeg'
return output_format, requested_format
class ImageHandler(webapp2.RequestHandler):
def get(self):
# Getting the URL parameters.
image_url = self.request.get('image_url')
try:
height = int(self.request.get('height'))
except:
height = 0
try:
width = int(self.request.get('width'))
except:
width = 0
requested_format = str(self.request.get('format')).strip().lower()
output_format, requested_format = set_output_format(requested_format)
memcache_store_key = '{},{},{},{}'.format(image_url, height, width, requested_format)
output = memcache.get(memcache_store_key)
if output is not None:
logging.info('Image found in cache.')
self.response.headers['Content-Type'] = 'image/' + requested_format
self.response.write(output)
return
image = images.Image(urlfetch.fetch(image_url).content)
if height == 0 and width == 0:
# No resizing done
pass
elif height == 0 or width == 0:
image.resize(width=width, height=height)
else:
image.resize(width=width, height=height, allow_stretch=True)
output = image.execute_transforms(output_encoding=output_format)
try:
added = memcache.add(memcache_store_key, output)
if added:
logging.info('Image added to cache.')
if not added:
logging.info('Image couldn\'t be cached.')
except ValueError:
logging.error('memcache.add() failed - image larger than 1MB')
self.response.headers['Content-Type'] = 'image/' + requested_format
self.response.write(output)
app = webapp2.WSGIApplication([
('/', HomeHandler),
('/image/', ImageHandler),
], debug=True)