]>
Commit | Line | Data |
---|---|---|
2cd9af74 MT |
1 | #!/usr/bin/python |
2 | ||
3 | import logging | |
4 | import tornado.web | |
5 | ||
6 | from handlers_base import * | |
7 | ||
8 | class AccountsAvatarHandler(BaseHandler): | |
9 | def get(self, who): | |
10 | # Get the desired size of the avatar file | |
11 | size = self.get_argument("size", 0) | |
12 | ||
13 | try: | |
14 | size = int(size) | |
15 | except (TypeError, ValueError): | |
16 | size = None | |
17 | ||
18 | # Cache handle | |
19 | cache_handle = "accounts-avatar-%s-%s" % (who, size or 0) | |
20 | avatar = self.memcached.get(cache_handle) | |
21 | ||
22 | if not avatar: | |
23 | logging.debug("Querying for avatar of %s" % who) | |
24 | ||
25 | account = self.backend.accounts.get_by_uid(who) | |
26 | if not account: | |
27 | raise tornado.web.HTTPError(404) | |
28 | ||
29 | avatar = account.get_avatar(size) | |
30 | ||
31 | # Save the avatar to cache for 6 hours | |
32 | if avatar: | |
33 | self.memcached.set(cache_handle, avatar, 6 * 3600) | |
34 | ||
35 | # Otherwise redirect to gravatar | |
36 | else: | |
37 | avatar = account.get_gravatar_url(size) | |
38 | ||
4da2cb99 | 39 | if avatar.startswith("http://") or avatar.startswith("https://"): |
2cd9af74 MT |
40 | return self.redirect(avatar) |
41 | ||
42 | self.set_header("Cache-Control", "public,max-age=300") | |
43 | self.set_header("Content-Disposition", "inline; filename=\"%s.jpg\"" % who) | |
44 | self.set_header("Content-Type", "image/jpeg") | |
45 | ||
46 | self.finish(avatar) |