Mercurial > public > sg101
comparison core/images/upload.py @ 971:4f265f61874b
Hotlink image form is functioning.
The user can now submit a URL via a form and the URL will be downloaded
and uploaded to a S3 bucket if it is an image.
Tests to follow.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 22 Sep 2015 20:23:50 -0500 |
parents | core/image_uploader.py@51a2051588f5 |
children | 7fc6c42b2f5b |
comparison
equal
deleted
inserted
replaced
970:bd594bcba5eb | 971:4f265f61874b |
---|---|
1 """This module contains a function to upload an image file to a S3Bucket. | |
2 | |
3 The image can be resized and a thumbnail can be generated and uploaded as well. | |
4 | |
5 """ | |
6 from base64 import b64encode | |
7 import logging | |
8 from io import BytesIO | |
9 import os.path | |
10 import uuid | |
11 | |
12 from PIL import Image | |
13 | |
14 from .utils import orient_image | |
15 | |
16 | |
17 logger = logging.getLogger(__name__) | |
18 | |
19 | |
20 def make_key(): | |
21 """Generate a random key suitable for a filename""" | |
22 return b64encode(uuid.uuid4().bytes, '-_').rstrip('=') | |
23 | |
24 | |
25 def upload(filename, bucket, metadata=None, new_size=None, thumb_size=None): | |
26 """Upload an image file to a given S3Bucket. | |
27 | |
28 The image can optionally be resized and a thumbnail can be generated and | |
29 uploaded as well. | |
30 | |
31 Parameters: | |
32 filename - The path to the file to process. The filename should have an | |
33 extension, and this is used for the uploaded image & thumbnail | |
34 names. | |
35 bucket - A core.s3.S3Bucket instance to upload to. | |
36 metadata - If not None, must be a dictionary of metadata to apply to the | |
37 uploaded file and thumbnail. | |
38 new_size - If not None, the image will be resized to the dimensions | |
39 specified by new_size, which must be a (width, height) tuple. | |
40 thumb_size - If not None, a thumbnail image will be created with the | |
41 dimensions specified by thumb_size, which must be a (width, | |
42 height) tuple. The thumbnail will use the same metadata, if | |
43 present, as the image. The thumbnail filename will be the | |
44 same basename as the image with a 't' appended. The | |
45 extension will be the same as the original image. | |
46 | |
47 A tuple is returned: (image_url, thumb_url) where thumb_url will be None if | |
48 a thumbnail was not requested. | |
49 """ | |
50 | |
51 logger.info('Processing image file: %s', filename) | |
52 | |
53 unique_key = make_key() | |
54 ext = os.path.splitext(filename)[1] | |
55 | |
56 # Re-orient if necessary | |
57 image = Image.open(filename) | |
58 changed, image = orient_image(image) | |
59 if changed: | |
60 image.save(filename) | |
61 | |
62 # Resize image if necessary | |
63 if new_size: | |
64 image = Image.open(filename) | |
65 if image.size > new_size: | |
66 logger.debug('Resizing from {} to {}'.format(image.size, new_size)) | |
67 image.thumbnail(new_size, Image.ANTIALIAS) | |
68 image.save(filename) | |
69 | |
70 # Create thumbnail if necessary | |
71 thumb = None | |
72 if thumb_size: | |
73 logger.debug('Creating thumbnail {}'.format(thumb_size)) | |
74 image = Image.open(filename) | |
75 image.thumbnail(thumb_size, Image.ANTIALIAS) | |
76 thumb = BytesIO() | |
77 image.save(thumb, format=image.format) | |
78 | |
79 # Upload images to S3 | |
80 file_key = unique_key + ext | |
81 logging.debug('Uploading image') | |
82 image_url = bucket.upload_from_filename(file_key, filename, metadata) | |
83 | |
84 thumb_url = None | |
85 if thumb: | |
86 logging.debug('Uploading thumbnail') | |
87 thumb_key = '{}t{}'.format(unique_key, ext) | |
88 thumb_url = bucket.upload_from_string(thumb_key, | |
89 thumb.getvalue(), | |
90 metadata) | |
91 | |
92 logger.info('Completed processing image file: %s', filename) | |
93 | |
94 return (image_url, thumb_url) |