Skip to content

Commit 39be2a4

Browse files
committed
Updates to issue purge requests to CDN
Particularly for saving shake and profile images. These have long TTLs like other assets served via CDN, but unlike post images, they can be updated and so have to be purged when they are updated. Note that we don't really have a size limitation for uploaded profile, shake images. They are forcibly resized to 200x200 (or left alone if smaller than this). The site places no restriction on the resulting image size, since a 200x200 JPG should be pretty small. Fixes #450 #447 #345 maybe others
1 parent 1ab32c8 commit 39be2a4

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

handlers/account.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import datetime
22
from urllib.parse import urlencode
33

4-
import tornado.httpclient
54
import tornado.web
65
from tornado.escape import xhtml_escape
76
from tornado.options import options
@@ -17,6 +16,9 @@
1716
import stripe
1817
from tasks.migration import migrate_for_user
1918

19+
import logging
20+
logger = logging.getLogger("mltshp")
21+
2022

2123
class VerifyEmailHandler(BaseHandler):
2224
def get(self, verification_key):
@@ -321,6 +323,23 @@ def post(self):
321323
photo_path, photo_name, photo_content_type,
322324
skip_s3=self.get_argument('skip_s3', None))
323325

326+
# issue PURGE command for profile URL
327+
if not self.get_argument('skip_s3', False) and \
328+
profile_image_saved and \
329+
options.use_cdn:
330+
try:
331+
urls = [s for s in [
332+
user.profile_image_url(include_protocol=True),
333+
user.profile_image_url(include_protocol=True).replace("/s3", ""),
334+
] if "default-icon" not in s]
335+
336+
for url in urls:
337+
response = requests.request("PURGE", url)
338+
if response.status_code != 200:
339+
logger.warning("PURGE failed: %s" % response.text)
340+
except Exception as e:
341+
logger.error("PURGE exception: %s" % str(e))
342+
324343
if user.is_paid or not uses_a_banned_phrase(full_name):
325344
user.full_name = full_name
326345
if user.is_paid or not uses_a_banned_phrase(about):

handlers/shake.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
from tornado import escape
33
import torndb
44
from tornado.options import options
5+
import requests
56

67
from .base import BaseHandler, require_membership
78
from models import Shake, User, Notification, ShakeManager, MigrationState
89
from lib.utilities import base36decode
910

11+
import logging
12+
logger = logging.getLogger("mltshp")
13+
1014

1115
def _invitations(shake, current_user):
1216
invitation = None
@@ -234,6 +238,24 @@ def post(self, shake_name=None):
234238
if file_name and not skip_s3:
235239
shake_to_update.set_page_image(file_path, sha1_value)
236240

241+
# issue PURGE command for shake image URLs
242+
if not skip_s3 and \
243+
options.use_cdn:
244+
try:
245+
urls = [s for s in [
246+
shake_to_update.page_image(),
247+
shake_to_update.page_image().replace("/s3", ""),
248+
shake_to_update.thumbnail_url(),
249+
shake_to_update.thumbnail_url().replace("/s3", ""),
250+
] if "default-icon" not in s]
251+
252+
for url in urls:
253+
response = requests.request("PURGE", url)
254+
if response.status_code != 200:
255+
logger.warning("PURGE failed: %s" % response.text)
256+
except Exception as e:
257+
logger.error("PURGE exception: %s" % str(e))
258+
237259
shake_to_update.save()
238260

239261
if json:

0 commit comments

Comments
 (0)