-
Notifications
You must be signed in to change notification settings - Fork 9
/
frontend.py
65 lines (54 loc) · 1.81 KB
/
frontend.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
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Frontend for the image host. This does the actual serving of the images
for use on others sites and within the admin
"""
import os
import datetime
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from models import Image
class GenericServer(webapp.RequestHandler):
"""
Image server designed to handle serving png images from
different object properties
"""
property = 'image'
def get(self):
# key is provided in the query string
img = self.request.get("id")
try:
# it might be an invalid key so we better check
image = db.get(img)
except db.BadKeyError:
# if it is then return a 404
self.error(404)
return
if image and image.image:
# we have an image so prepare the response
# with the relevant headers
self.response.headers['Content-Type'] = "image/png"
# and then write our the image data direct to the response
self.response.out.write(eval("image.%s" % self.property))
else:
# we should probably return an image with the correct header
# here instead of the default html 404
self.error(404)
class ImageServer(GenericServer):
"Serve the main image"
property = 'image'
class ThumbServer(GenericServer):
"Serve the thumbnail image"
property = 'thumb'
class OriginalServer(GenericServer):
"Serve the original uploaded image. Currently unused."
property = 'original'
application = webapp.WSGIApplication([
('/i/img', ImageServer),
('/i/thumb', ThumbServer),
], debug=True)
def main():
"Run the application"
run_wsgi_app(application)
if __name__ == '__main__':
main()