def _process_POST(self): f = request.files[self.IMAGE_TYPE] try: img = Image.open(f) except IOError: flash(_('You cannot upload this file as an icon/logo.'), 'error') return jsonify_data(content=None) if img.format.lower() not in {'jpeg', 'png', 'gif'}: flash(_('The file has an invalid format ({format})').format(format=img.format), 'error') return jsonify_data(content=None) if img.mode == 'CMYK': flash(_('The image you uploaded is using the CMYK colorspace and has been converted to RGB. ' 'Please check if the colors are correct and convert it manually if necessary.'), 'warning') img = img.convert('RGB') img = self._resize(img) image_bytes = BytesIO() img.save(image_bytes, 'PNG') image_bytes.seek(0) content = image_bytes.read() metadata = { 'hash': crc32(content), 'size': len(content), 'filename': os.path.splitext(secure_filename(f.filename, self.IMAGE_TYPE))[0] + '.png', 'content_type': 'image/png' } self._set_image(content, metadata) flash(self.SAVED_FLASH_MSG, 'success') logger.info("New {} '%s' uploaded by %s (%s)".format(self.IMAGE_TYPE), f.filename, session.user, self.category) return jsonify_data(content=get_image_data(self.IMAGE_TYPE, self.category))
def _process_POST(self): f = request.files[self.IMAGE_TYPE] try: img = Image.open(f) except IOError: flash(_('You cannot upload this file as an icon/logo.'), 'error') return jsonify_data(content=None) if img.format.lower() not in {'jpeg', 'png', 'gif'}: flash(_('The file has an invalid format ({format})').format(format=img.format), 'error') return jsonify_data(content=None) if img.mode == 'CMYK': flash(_('The image you uploaded is using the CMYK colorspace and has been converted to RGB. ' 'Please check if the colors are correct and convert it manually if necessary.'), 'warning') img = img.convert('RGB') img = self._resize(img) image_bytes = BytesIO() img.save(image_bytes, 'PNG') image_bytes.seek(0) content = image_bytes.read() metadata = { 'hash': crc32(content), 'size': len(content), 'filename': os.path.splitext(secure_filename(f.filename, self.IMAGE_TYPE))[0] + '.png', 'content_type': 'image/png' } self._set_image(content, metadata) flash(self.SAVED_FLASH_MSG, 'success') logger.info("New {} '%s' uploaded by %s (%s)".format(self.IMAGE_TYPE), f.filename, session.user, self.category) return jsonify_data(content=get_image_data(self.IMAGE_TYPE, self.category))