def set_profile_image(self, file_path, file_name, content_type):
"""
Takes a local path, name and content-type, which are parameters passed in by
nginx upload module. Converts to RGB, resizes to thumbnail and uploads to S3.
Returns False if some conditions aren't met, such as error making thumbnail
or content type is one we don't support.
"""
valid_content_types = ('image/gif', 'image/jpeg', 'image/jpg', 'image/png',)
if content_type not in valid_content_types:
return False
destination = cStringIO.StringIO()
if not transform_to_square_thumbnail(file_path, 100*2, destination):
return False
bucket = S3Bucket()
k = Key(bucket)
k.key = "account/%s/profile.jpg" % (self.id)
k.set_metadata('Content-Type', 'image/jpeg')
k.set_metadata('Cache-Control', 'max-age=86400')
k.set_contents_from_string(destination.getvalue())
k.set_acl('public-read')
self.profile_image = 1
self.save()
return True
评论列表
文章目录