# HG changeset patch # User Brian Neal # Date 1422404941 21600 # Node ID ee47122d6277e3d7f8d9f46c68bc446872658e85 # Parent 4dd7e1b5efe8862c28b2b73d72a3abb868f8705d Base64 encode uploaded filenames to make them shorter. diff -r 4dd7e1b5efe8 -r ee47122d6277 core/image_uploader.py --- 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)