-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
297 lines (250 loc) · 10.3 KB
/
index.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# TODO:
# * Tell people somewhere on the front page that they can e-mail photos
# * Report the KeyError bug with memcache (tsurp.com/P)
# * Properly determine content-type
# * Use GAE's image.resize() to reduce network traffic
import random
import logging
import wsgiref.handlers
import os
import traceback
import email
import mimetypes
from google.appengine.api import users, memcache, mail, datastore_errors
from google.appengine.ext import webapp, db
from google.appengine.ext.webapp import template
from google.appengine.runtime import apiproxy_errors
import urllib
from url56 import from_url56, to_url56, InvalidURLError
from imagestore import Image, ImageStore
ADMIN_ADDRESS = '[email protected]'
CONTACT_ADDRESS = '[email protected]'
ALLOWED_EXTENSIONS = ('.jpg', '.jpeg', '.jpe', '.bmp',
'.png', '.gif', '.tif', '.tiff')
IMAGE_URL = 'http://tsurp.com/%s'
# T-mobile images
BAD_FILES = ('dottedline350.gif', 'dottedline600.gif',
'tmobilelogo.gif', 'tmobilespace.gif')
class Index(webapp.RequestHandler):
def get(self):
try:
self.response.headers['Content-Type'] = 'text/html'
write_template(self.response, 'index.html', None)
except Exception, e:
self.error(500)
logging.error('%s: %s' % (self.__class__.__name__,
traceback.print_exc()))
class ImageFile(webapp.RequestHandler):
def get(self, image_id):
try:
image = memcache.get(image_id)
if image is None:
image = Image.get_by_id(from_url56(image_id))
try:
memcache.add(image_id, image)
except ValueError:
# too big for memcache
pass
if image and image.image:
self.response.headers['Content-Type'] = "image/%s" % str(image.ext)
self.response.out.write(image.image)
else:
self.error(404)
except InvalidURLError, e:
self.error(404)
except datastore_errors.BadKeyError, e:
self.error(404)
except Exception, e:
self.error(404)
logging.error('%s: %s' % (self.__class__.__name__,
traceback.print_exc()))
class ImagePage(webapp.RequestHandler):
def get(self, image_id):
try:
image = memcache.get(image_id)
if image is None:
image = Image.get_by_id(from_url56(image_id))
try:
memcache.add(image_id, image)
except ValueError:
# too big for memcache
pass
if not image:
self.error(404)
return
extra_header_values = {
'title': 'tsurp: ' + image.title,
}
values = {
'image_url': '/img/' + image_id,
'this_url': self.request.url
}
write_template(self.response, 'image.html', values,
extra_header_values)
except InvalidURLError, e:
self.error(404)
except datastore_errors.BadKeyError, e:
self.error(404)
except Exception, e:
self.error(404)
logging.error('%s: %s' % (self.__class__.__name__,
traceback.print_exc()))
class UserImages(webapp.RequestHandler):
def get(self, user_nickname):
try:
extra_header_values = {
'title': 'tsurp: %s''s images' % user_nickname
}
user_nickname = urllib.unquote(user_nickname)
images = db.GqlQuery("SELECT * FROM Image WHERE author_nickname = :1", user_nickname)
if not images or not isinstance(images, db.GqlQuery):
self.error(404)
return
images_table = "<table id='images_table'>"
rowcount = 0
rows = "<tr>"
for img in images:
# 5 images per row
if (rowcount % 5) == 0 and rowcount > 0:
rows += "</tr><tr>"
image_url = "/%s" % to_url56(img.key().id())
image_src_url = "/img%s" % image_url
rows += "<td>"
rows += "<a href='%s'>" % image_url
rows += "<img src='%s' /></a>" % image_src_url
rows += "</td>"
rowcount += 1
images_table += rows
images_table += "</tr></table>"
values = {
'images_table': images_table,
'username': user_nickname
}
write_template(self.response, 'userimages.html', values,
extra_header_values)
except Exception, e:
self.error(404)
logging.error('%s: %s' % (self.__class__.__name__,
traceback.print_exc()))
class UploadImageFromWeb(webapp.RequestHandler):
def post(self):
try:
if not isinstance(self.request.get('img'), str):
self.redirect('/')
image = db.Blob(self.request.get('img'))
ext = self.request.POST.get('img').filename.split('.')[-1]
author = users.get_current_user()
title = self.request.get('title')
image_id = ImageStore.add(image, ext=ext,
author=author, title=title)
self.redirect('/' + image_id)
except Exception, e:
self.error(500)
logging.error('%s: %s' % (self.__class__.__name__,
traceback.print_exc()))
class UploadImageFromMail(webapp.RequestHandler):
def post(self):
try:
sender = self.request.GET.get("from", "")
if not sender or sender == '':
self.error(500)
return
recipient = self.request.GET.get("to", "")
if recipient == ADMIN_ADDRESS:
mail.send_mail(sender=CONTACT_ADDRESS,
to=ADMIN_ADDRESS,
subject="mail to [email protected] from %s" % sender,
body=self.request.body)
return
message = email.message_from_string(self.request.body)
for part in message.walk():
if part.get_content_maintype() == 'multipart':
continue
filename = part.get_filename()
if filename and filename not in BAD_FILES:
ext = mimetypes.guess_extension(part.get_content_type())
if ext in ALLOWED_EXTENSIONS:
image = db.Blob(part.get_payload(decode=True))
ext = ext.replace('.', '')
# turn .jpe's into jpgs, there's no content/type: jpe
if ext == 'jpe': ext = 'jpg'
image_id = ImageStore.add(image, ext=ext,
author=users.User(sender))
url = IMAGE_URL % image_id
mail.send_mail(sender=CONTACT_ADDRESS,
to=sender,
subject="tsurp: %s" % url,
body=url)
logging.debug('sent image url %s to %s' % (url, sender))
else:
logging.debug('invalid ext for %s: %s' % (filename,
ext))
except apiproxy_errors.OverQuotaError, e:
self.error(500)
logging.debug('lost message for %s, over quota: %s.' % (sender, e))
# We should add to a queue in the datastore here
except Exception, e:
self.error(500)
logging.error('%s: %s' % (self.__class__.__name__,
traceback.print_exc()))
def get_login_listitems():
user = users.get_current_user()
if user:
items = "<li><a href=""/user/%s"">my images</a></li>" % user.nickname()
items += "<li><a href=""%s"">logout</a>" % users.create_logout_url("/")
return items
else:
items = "<li><a href=""%s"">login (google acct)</a></li>" % users.create_login_url("/")
items += "<li><a href=""%s"">register</a>" % users.create_login_url("/")
return items
def write_template(response, file, values,
extra_header_values=None, extra_footer_values=None):
header_values = {
'title': 'tsurp'
}
login_listitems = get_login_listitems()
footer_values = {
'login_listitems': login_listitems
}
path = os.path.join(os.path.dirname(__file__), file)
if extra_header_values:
header_values.update(extra_header_values)
response.out.write(template.render('header.html', header_values))
response.out.write(template.render(path, values))
if extra_footer_values:
footer_values.update(extra_footer_values)
response.out.write(template.render('footer.html', footer_values))
def real_main():
application = webapp.WSGIApplication(
[('/', Index),
('/user/(.*)', UserImages),
('/u/img/web', UploadImageFromWeb),
('/u/img/mail', UploadImageFromMail),
('/img/(.*)', ImageFile),
('/(.*)', ImagePage)
],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
def profile_main():
# This is the main function for profiling
# We've renamed our original main() above to real_main()
try:
import cProfile, pstats, StringIO
prof = cProfile.Profile()
prof = prof.runctx("real_main()", globals(), locals())
stream = StringIO.StringIO()
stats = pstats.Stats(prof, stream=stream)
stats.sort_stats("time") # Or cumulative
stats.print_stats(80) # 80 = how many to print
# The rest is optional.
# stats.print_callees()
# stats.print_callers()
logging.info("Profile data:\n%s", stream.getvalue())
except:
return
if __name__ == "__main__":
# Profile one percent of requests
if random.randint(1, 100) == 1:
profile_main()
else:
real_main()