Mercurial > public > sg101
comparison core/management/commands/ssl_images.py @ 882:9a3019f2c7dc
Base64 encode filename to make it shorter.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 27 Jan 2015 20:35:06 -0600 |
parents | 13f2d4393ec4 |
children | 9a15f7c27526 |
comparison
equal
deleted
inserted
replaced
881:13f2d4393ec4 | 882:9a3019f2c7dc |
---|---|
4 - Images with src = http://surfguitar101.com/something are rewritten to be | 4 - Images with src = http://surfguitar101.com/something are rewritten to be |
5 /something. | 5 /something. |
6 - Non SG101 images that use http: are downloaded, resized, and uploaded to | 6 - Non SG101 images that use http: are downloaded, resized, and uploaded to |
7 an S3 bucket. The src attribute is replaced with the new S3 URL. | 7 an S3 bucket. The src attribute is replaced with the new S3 URL. |
8 """ | 8 """ |
9 import base64 | |
9 import logging | 10 import logging |
10 from optparse import make_option | 11 from optparse import make_option |
11 import os.path | 12 import os.path |
12 import re | 13 import re |
13 import signal | 14 import signal |
137 logger.info('Resizing from %s to %s', image.size, PHOTO_MAX_SIZE) | 138 logger.info('Resizing from %s to %s', image.size, PHOTO_MAX_SIZE) |
138 image.thumbnail(PHOTO_MAX_SIZE, Image.ANTIALIAS) | 139 image.thumbnail(PHOTO_MAX_SIZE, Image.ANTIALIAS) |
139 image.save(img_path) | 140 image.save(img_path) |
140 | 141 |
141 | 142 |
143 def gen_key(): | |
144 """Return a random key.""" | |
145 return base64.b64encode(uuid.uuid4().bytes, '-_').rstrip('=') | |
146 | |
147 | |
142 def upload_image(img_path): | 148 def upload_image(img_path): |
143 """Upload image file located at img_path to our S3 bucket. | 149 """Upload image file located at img_path to our S3 bucket. |
144 | 150 |
145 Returns the URL of the image in the bucket or None if an error occurs. | 151 Returns the URL of the image in the bucket or None if an error occurs. |
146 """ | 152 """ |
147 logger.info("upload_image starting") | 153 logger.info("upload_image starting") |
148 # Make a unique name for the image in the bucket | 154 # Make a unique name for the image in the bucket |
149 unique_key = uuid.uuid4().hex | |
150 ext = os.path.splitext(img_path)[1] | 155 ext = os.path.splitext(img_path)[1] |
151 file_key = unique_key + ext | 156 file_key = gen_key() + ext |
152 try: | 157 try: |
153 return bucket.upload_from_filename(file_key, img_path, public=True) | 158 return bucket.upload_from_filename(file_key, img_path, public=True) |
154 except IOError as ex: | 159 except IOError as ex: |
155 logger.error("Error uploading file: %s", ex) | 160 logger.error("Error uploading file: %s", ex) |
156 return None | 161 return None |