def set_page_image(self, file_path=None, sha1_value=None): thumb_cstr = cStringIO.StringIO() image_cstr = cStringIO.StringIO() if not file_path or not sha1_value: return False #generate smaller versions if not transform_to_square_thumbnail(file_path, 48, thumb_cstr): return False if not transform_to_square_thumbnail(file_path, 284, image_cstr): return False bucket = S3Bucket() try: #save thumbnail k = Key(bucket) k.key = "account/%s/shake_%s_small.jpg" % (self.user_id, self.name) k.set_contents_from_string(thumb_cstr.getvalue()) k.set_acl('public-read') #save small k = Key(bucket) k.key = "account/%s/shake_%s.jpg" % (self.user_id, self.name) k.set_contents_from_string(image_cstr.getvalue()) k.set_acl('public-read') self.image = 1 self.save() except Exception as e: return False return True
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