diff core/image_uploader.py @ 885:ee47122d6277

Base64 encode uploaded filenames to make them shorter.
author Brian Neal <bgneal@gmail.com>
date Tue, 27 Jan 2015 18:29:01 -0600
parents 234726f5a47a
children 51a2051588f5
line wrap: on
line diff
--- a/core/image_uploader.py	Fri Jan 23 18:56:02 2015 -0600
+++ b/core/image_uploader.py	Tue Jan 27 18:29:01 2015 -0600
@@ -3,6 +3,7 @@
 The image can be resized and a thumbnail can be generated and uploaded as well.
 
 """
+from base64 import b64encode
 import logging
 from io import BytesIO
 import os.path
@@ -18,6 +19,11 @@
 logger = logging.getLogger(__name__)
 
 
+def make_key():
+    """Generate a random key suitable for a filename"""
+    return b64encode(uuid.uuid4().bytes, '-_').rstrip('=')
+
+
 def upload(fp, bucket, metadata=None, new_size=None, thumb_size=None):
     """Upload an image file to a given S3Bucket.
 
@@ -54,7 +60,7 @@
     # to perform on it fail if this is the case. To get around these issues,
     # we make a copy of the file on the file system and operate on the copy.
     # First generate a unique name and temporary file path.
-    unique_key = uuid.uuid4().hex
+    unique_key = make_key()
     ext = os.path.splitext(fp.name)[1]
     temp_name = os.path.join(tempfile.gettempdir(), unique_key + ext)