def save_thumb(self, id, thumb, **values): """Save a thumbnail uploaded with :class:`~mediacore.forms.admin.ThumbForm`. :param id: Media ID. If ``"new"`` a new Media stub is created with :func:`~mediacore.model.media.create_podcast_stub`. :type id: ``int`` or ``"new"`` :param file: The uploaded file :type file: :class:`cgi.FieldStorage` or ``None`` :rtype: JSON dict :returns: success bool message Error message, if unsuccessful id The :attr:`~mediacore.model.podcasts.Podcast.id` which is important if a new podcast has just been created. """ if id == 'new': podcast = create_podcast_stub() else: podcast = fetch_row(Podcast, id) try: # Create jpeg thumbs img = Image.open(thumb.file) if id == 'new': DBSession.add(podcast) DBSession.flush() # TODO: Allow other formats? for key, xy in config['thumb_sizes'][podcast._thumb_dir].iteritems(): thumb_path = helpers.thumb_path(podcast, key) thumb_img = helpers.resize_thumb(img, xy) thumb_img.save(thumb_path) # Backup the original image just for kicks backup_type = os.path.splitext(thumb.filename)[1].lower()[1:] backup_path = helpers.thumb_path(podcast, 'orig', ext=backup_type) backup_file = open(backup_path, 'w+b') thumb.file.seek(0) shutil.copyfileobj(thumb.file, backup_file) thumb.file.close() backup_file.close() success = True message = None except IOError, e: success = False message = 'Unsupported image type'