def clean_avatar(self): avatar = self.cleaned_data.get("avatar") # Validate file size. kilobytes = len(avatar) / 1024 if kilobytes > settings.MAX_AVATAR_FILE_SIZE: raise ValidationError("File too large. It must be under {}KB.".format(settings.MAX_AVATAR_FILE_SIZE)) # Validate file extension. if hasattr(avatar, "temporary_file_path"): f = avatar.temporary_file_path() elif hasattr(avatar, "read"): f = BytesIO(avatar.read()) else: f = BytesIO(avatar["content"]) try: img = Image.open(f) except Exception: # Python Imaging Library doesn't recognize it as an image raise ValidationError("Unsupported image type. Please upload bmp, png or jpeg.") if img.format not in ("BMP", "PNG", "JPEG"): raise ValidationError("Unsupported image type. Please upload bmp, png or jpeg.") # Resize avatar with the thumbnail helper. This will crop the avatar to # a 1:1 aspect ratio without stretching the image. return SimpleUploadedFile( name="t_{}.{}".format(str(uuid.uuid4()), img.format), content=create_thumbnail(avatar, img.format.lower(), 200, 200), )
def scrape(self, url): logger.info("Started downloading '%s'", url) response = requests.get(url, headers={'User-Agent': self.USER_AGENT}) file_type, extension = response.headers.get('content-type').split('/') downloaded_file = SimpleUploadedFile( name='w_{}.{}'.format(str(uuid.uuid4()), extension), content=response.content, content_type=response.headers.get('content-type') ) form = WallpaperForm(files={'file': downloaded_file}) if not form.is_valid(): logger.info("Image '%s' did not validate: '%s'", url, form.errors) return wallpaper = form.save() logger.info("Saved image '%s' with hash '%s'", url, wallpaper.hash) thumb_file = SimpleUploadedFile( name='t_{}.{}'.format(str(uuid.uuid4()), wallpaper.extension), content=create_thumbnail(wallpaper.file, wallpaper.extension), content_type=response.headers.get('content-type') ) Thumbnail(wallpaper=wallpaper, file=thumb_file).save() time.sleep(random.randint(1, 5))
def import_file(self, folder_path, file_name): full_path = folder_path + file_name extension = file_name.split('.')[-1] f = open(full_path, "rb") if extension == 'jpg': extension = 'jpeg' form = WallpaperForm(files={'file': SimpleUploadedFile( name='tmp.{extension}'.format(extension=extension), content=f.read(), content_type=extension )}) if form.is_valid(): logger.info('Saving image \'{}\''.format(file_name)) # Wallpaper wallpaper = form.save(commit=False) wallpaper.size = form.cleaned_data['file'].size wallpaper.hash = file_hash(form.cleaned_data['file']) form.cleaned_data['file'].file.seek(0) wallpaper.width, wallpaper.height = Image.open(form.cleaned_data['file'].file).size wallpaper.extension = extension wallpaper.file.save( 'w_{hash}.{extension}'.format(hash=wallpaper.hash, extension=wallpaper.extension), form.cleaned_data['file'] ) wallpaper.save() # Thumbnail form.cleaned_data['file'].file.seek(0) thumb_file = create_thumbnail(form.cleaned_data['file'].file, extension) thumb_width, thumb_height = Image.open(BytesIO(thumb_file)).size thumbnail = Thumbnail( size=sys.getsizeof(thumb_file), wallpaper=wallpaper, hash=file_hash(BytesIO(thumb_file)), width=thumb_width, height=thumb_height, extension=extension ) thumbnail.file.save( 't_{hash}.{extension}'.format(hash=thumbnail.hash, extension=extension), ContentFile(thumb_file) ) thumbnail.save() else: logger.info('Image \'{}\' did not validate: \'{}\''.format(file_name, form.errors)) os.remove(full_path)
def _regenerate_thumbnails(self, wallpaper): wallpaper.thumbnails.all().delete() thumbnail_file = create_thumbnail( image_file=wallpaper.file, file_extension=wallpaper.extension ) thumb_file = SimpleUploadedFile( name='t_{}.{}'.format(str(uuid.uuid4()), wallpaper.extension), content=thumbnail_file, ) Thumbnail(wallpaper=wallpaper, file=thumb_file).save() thumb_file.close()
def post(self, request, *args, **kwargs): formset = self.WallpaperFormSet(request.POST, request.FILES) if not formset.is_valid(): context = self.get_context_data(**kwargs) context['formset'] = formset return self.render_to_response(context) for form in formset: if not form.cleaned_data.get('file'): continue uploaded_file = form.cleaned_data['file'] file_type, extension = uploaded_file.content_type.split('/') wallpaper = form.save(commit=False) wallpaper.uploaded_by = request.user wallpaper.file.name = 'w_{}.{}'.format(str(uuid.uuid4()), extension) wallpaper.save() thumb_file = SimpleUploadedFile( name='t_{}.{}'.format(str(uuid.uuid4()), wallpaper.extension), content=create_thumbnail(wallpaper.file, wallpaper.extension), content_type=uploaded_file.content_type ) Thumbnail(wallpaper=wallpaper, file=thumb_file).save() messages.add_message( request, messages.SUCCESS, "Your wallpapers have succesfully been uploaded. They will get " + "visible under 'uploaded' and to other users after they have been" + " accepted." ) return redirect( reverse_lazy('walldb:user:details', kwargs={'id': request.user.pk}) )