changeset 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 f12751259f66
files core/management/commands/ssl_images.py
diffstat 1 files changed, 7 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/core/management/commands/ssl_images.py	Thu Jan 15 21:05:05 2015 -0600
+++ b/core/management/commands/ssl_images.py	Tue Jan 27 20:35:06 2015 -0600
@@ -6,6 +6,7 @@
     - Non SG101 images that use http: are downloaded, resized, and uploaded to
       an S3 bucket. The src attribute is replaced with the new S3 URL.
 """
+import base64
 import logging
 from optparse import make_option
 import os.path
@@ -139,6 +140,11 @@
         image.save(img_path)
 
 
+def gen_key():
+    """Return a random key."""
+    return base64.b64encode(uuid.uuid4().bytes, '-_').rstrip('=')
+
+
 def upload_image(img_path):
     """Upload image file located at img_path to our S3 bucket.
 
@@ -146,9 +152,8 @@
     """
     logger.info("upload_image starting")
     # Make a unique name for the image in the bucket
-    unique_key = uuid.uuid4().hex
     ext = os.path.splitext(img_path)[1]
-    file_key = unique_key + ext
+    file_key = gen_key() + ext
     try:
         return bucket.upload_from_filename(file_key, img_path, public=True)
     except IOError as ex: