comparison user_photos/images.py @ 697:67f8d49a9377

Cleaned up the code a bit. Separated the S3 stuff out into its own class. This class maybe should be in core. Still want to do some kind of context manager around the temporary file we are creating to ensure it gets deleted.
author Brian Neal <bgneal@gmail.com>
date Sun, 08 Sep 2013 21:02:58 -0500
parents b2a8fde3173a
children
comparison
equal deleted inserted replaced
696:b2a8fde3173a 697:67f8d49a9377
9 import os.path 9 import os.path
10 import uuid 10 import uuid
11 11
12 from django.conf import settings 12 from django.conf import settings
13 from PIL import Image 13 from PIL import Image
14 from boto.s3.connection import S3Connection
15 from boto.s3.key import Key
16 14
17 15
18 logger = logging.getLogger(__name__) 16 logger = logging.getLogger(__name__)
19 17
20 18
21 def process_file(f, user): 19 def process_file(f, user, bucket):
22 """Perform processing on the given uploaded image file: 20 """Perform processing on the given uploaded image file:
23 21
24 * The image is resized if necessary 22 * The image is resized if necessary
25 * A thumbnail version is created 23 * A thumbnail version is created
26 * The image and thumbnail are uploaded to an S3 bucket 24 * The image and thumbnail are uploaded to an S3 bucket
49 image.thumbnail(settings.USER_PHOTOS_THUMB_SIZE, Image.ANTIALIAS) 47 image.thumbnail(settings.USER_PHOTOS_THUMB_SIZE, Image.ANTIALIAS)
50 thumb = BytesIO() 48 thumb = BytesIO()
51 image.save(thumb, format=image.format) 49 image.save(thumb, format=image.format)
52 50
53 # Upload both images to S3 51 # Upload both images to S3
54 logger.debug('Getting connection / bucket') 52 logging.debug('Uploading image')
55 conn = S3Connection(settings.USER_PHOTOS_ACCESS_KEY,
56 settings.USER_PHOTOS_SECRET_KEY)
57 bucket = conn.get_bucket(settings.USER_PHOTOS_BUCKET, validate=False)
58
59 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') 53 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
60 54 metadata = {'user': user.username, 'date': now}
61 logging.debug('Uploading image') 55 file_key = unique_key + ext
62 k1 = Key(bucket) 56 bucket.upload_from_filename(file_key, filename, metadata)
63 k1.key = unique_key + ext
64 k1.set_metadata('user', user.username)
65 k1.set_metadata('date', now)
66 k1.set_contents_from_filename(filename)
67 57
68 logging.debug('Uploading thumbnail') 58 logging.debug('Uploading thumbnail')
69 k2 = Key(bucket) 59 thumb_key = '{}t{}'.format(unique_key, ext)
70 k2.key = '{}t{}'.format(unique_key, ext) 60 bucket.upload_from_string(thumb_key, thumb.getvalue())
71 k2.set_metadata('user', user.username)
72 k2.set_metadata('date', now)
73 k2.set_contents_from_string(thumb.getvalue())
74
75 logging.debug('Making public')
76 k1.make_public()
77 k2.make_public()
78 61
79 os.remove(filename) 62 os.remove(filename)
80 63
81 logger.info('Completed processing image file for {}: {}'.format(user.username, f.name)) 64 logger.info('Completed processing image file for {}: {}'.format(user.username, f.name))
82 65
83 url_base = '{}/{}/'.format(settings.USER_PHOTOS_BASE_URL, 66 url_base = '{}/{}/'.format(settings.USER_PHOTOS_BASE_URL,
84 settings.USER_PHOTOS_BUCKET) 67 settings.USER_PHOTOS_BUCKET)
85 68
86 return (url_base + k1.key, url_base + k2.key) 69 return (url_base + file_key, url_base + thumb_key)