def upload_video(self, request, **kwargs): self.method_check(request, allowed=['post']) self.is_authenticated(request) try: obj = self._meta.queryset._clone().get(pk=kwargs['pk']) except self._meta.object_class.DoesNotExist: return http.HttpNotFound() bundle = self.build_bundle(obj=obj, request=request) self.authorized_update_detail(None, bundle) video = request.FILES.get('video', None) if not video: return self.error_response(request, {"error": "No file called video found"}, response_class=http.HttpBadRequest) video_thumbnail = request.FILES.get('video_thumbnail', None) if not video_thumbnail: return self.error_response(request, {"error": "No file called video_thumbnail found"}, response_class=http.HttpBadRequest) def save_video(obj, video): obj.video.save(video.name, video) obj.video_thumbnail.save(video_thumbnail.name, video_thumbnail) obj.save(update_fields=['video', 'video_thumbnail']) retry_cloudfiles(save_video, bundle.obj, video) bundle = self.full_dehydrate(bundle) def return_response(resource, request, bundle): return resource.create_response(request, bundle, response_class=http.HttpAccepted) # Retry cloudfiles in case we get a 404 because the videos haven't finished uploading yet return retry_cloudfiles(return_response, self, request, bundle)
def upload_picture(self, request, **kwargs): self.method_check(request, allowed=['post']) self.is_authenticated(request) try: obj = self._meta.queryset._clone().get(pk=kwargs['pk']) except self._meta.object_class.DoesNotExist: return http.HttpNotFound() bundle = self.build_bundle(obj=obj, request=request) self.authorized_update_detail(None, bundle) picture = request.FILES.get('picture', None) if not picture: return self.error_response(request, {"error": "No file called picture found"}, response_class=http.HttpBadRequest) def save_image(obj, picture): obj.original_photo.save(picture.name, picture) obj.save(update_fields=['original_photo']) retry_cloudfiles(save_image, bundle.obj, request.FILES['picture']) bundle = self.full_dehydrate(bundle) def return_response(resource, request, bundle): return resource.create_response(request, bundle, response_class=http.HttpAccepted) # Retry cloudfiles in case we get a 404 because the photos haven't finished uploading yet return retry_cloudfiles(return_response, self, request, bundle)
def get_profile_image(strategy, details, response, uid, user, social, is_new=False, *args, **kwargs): """Attempt to get a profile image for the User""" # If we don't have a user then just return if user is None: return # Save photo from FB if kwargs['backend'].name == "facebook": try: image_url = "https://graph.facebook.com/%s/picture?type=large" % uid result = urllib.urlretrieve(image_url) def save_image(user, uid, result): user.original_photo.save("%s.jpg" % uid, File(open(result[0]))) user.save(update_fields=['original_photo']) retry_cloudfiles(save_image, user, uid, result) except URLError: pass elif kwargs['backend'].name == "twitter" and social: try: # Get profile image to save if response['profile_image_url'] != '': image_result = urllib.urlretrieve(response['profile_image_url']) def save_image(user, uid, image_result): user.original_photo.save("%s.jpg" % uid, File(open(image_result[0]))) user.save(update_fields=['original_photo']) retry_cloudfiles(save_image, user, uid, image_result) except URLError: pass
def process_thumbnail(instance, original_file, sizes, crop=False): """ Makes a smart thumbnail """ file = StringIO.StringIO(original_file.read()) original_image = Image.open(file) # open the image using PIL # pull a few variables out of that full path filename = os.path.basename(original_file.name).rsplit('.', 1)[0] extension = os.path.basename(original_file.name).rsplit( '.', 1)[1] # the file extension # If there is no extension found try jpg if extension == '': extension = 'jpg' # use the file extension to determine if the image is valid before proceeding if extension not in ['jpg', 'jpeg', 'gif', 'png']: return False for size_name, size in sizes.iteritems(): im = original_image.copy() (x_size, y_size) = im.size original_ratio = float(x_size) / float(y_size) width = size['width'] height = size['height'] new_ratio = float(width / height) if new_ratio > original_ratio: im = im.resize((width, int(width / original_ratio)), Image.ANTIALIAS) if crop: clip_amount = int((int(width / original_ratio) - height) / 2) im = im.crop((0, clip_amount, width, height + clip_amount)) else: im = im.resize((int(height * original_ratio), height), Image.ANTIALIAS) if crop: clip_amount = int((int(height * original_ratio) - width) / 2) im = im.crop((clip_amount, 0, width + clip_amount, height)) name = "%s.jpg" % filename tempfile_io = StringIO.StringIO() if im.mode != "RGB": im = im.convert("RGB") im.save(tempfile_io, 'JPEG') temp_file = InMemoryUploadedFile(tempfile_io, None, name, 'image/jpeg', tempfile_io.len, None) def save_image(temp_file, instance, size_name, name): # Make sure we're at the beginning of the file for reading when saving temp_file.seek(0) getattr(instance, size_name).save(name, temp_file) retry_cloudfiles(save_image, temp_file, instance, size_name, name) return True
def process_thumbnail(instance, original_file, sizes, crop=False): """ Makes a smart thumbnail """ file = StringIO.StringIO(original_file.read()) original_image = Image.open(file) # open the image using PIL # pull a few variables out of that full path filename = os.path.basename(original_file.name).rsplit('.', 1)[0] extension = os.path.basename(original_file.name).rsplit('.', 1)[1] # the file extension # If there is no extension found try jpg if extension == '': extension = 'jpg' # use the file extension to determine if the image is valid before proceeding if extension not in ['jpg', 'jpeg', 'gif', 'png']: return False for size_name, size in sizes.iteritems(): im = original_image.copy() (x_size, y_size) = im.size original_ratio = float(x_size) / float(y_size) width = size['width'] height = size['height'] new_ratio = float(width / height) if new_ratio > original_ratio: im = im.resize((width, int(width / original_ratio)), Image.ANTIALIAS) if crop: clip_amount = int((int(width / original_ratio) - height) / 2) im = im.crop((0, clip_amount, width,height + clip_amount)) else: im = im.resize((int(height * original_ratio), height), Image.ANTIALIAS) if crop: clip_amount = int((int(height * original_ratio) - width) / 2) im = im.crop((clip_amount, 0, width + clip_amount, height)) name = "%s.jpg" % filename tempfile_io = StringIO.StringIO() if im.mode != "RGB": im = im.convert("RGB") im.save(tempfile_io, 'JPEG') temp_file = InMemoryUploadedFile(tempfile_io, None, name, 'image/jpeg', tempfile_io.len, None) def save_image(temp_file, instance, size_name, name): # Make sure we're at the beginning of the file for reading when saving temp_file.seek(0) getattr(instance, size_name).save(name, temp_file) retry_cloudfiles(save_image, temp_file, instance, size_name, name) return True
def get_profile_image(strategy, details, response, uid, user, social, is_new=False, *args, **kwargs): """Attempt to get a profile image for the User""" # If we don't have a user then just return if user is None: return # Save photo from FB if kwargs['backend'].name == "facebook": try: image_url = "https://graph.facebook.com/%s/picture?type=large" % uid result = urllib.urlretrieve(image_url) def save_image(user, uid, result): user.original_photo.save("%s.jpg" % uid, File(open(result[0]))) user.save(update_fields=['original_photo']) retry_cloudfiles(save_image, user, uid, result) except URLError: pass elif kwargs['backend'].name == "twitter" and social: try: # Get profile image to save if response['profile_image_url'] != '': image_result = urllib.urlretrieve( response['profile_image_url']) def save_image(user, uid, image_result): user.original_photo.save("%s.jpg" % uid, File(open(image_result[0]))) user.save(update_fields=['original_photo']) retry_cloudfiles(save_image, user, uid, image_result) except URLError: pass