def save_thumb(self, id, thumb, **kwargs): """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_media_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.media.Media.id` which is important if a new media has just been created. """ if id == 'new': media = create_media_stub() else: media = fetch_row(Media, id) try: # Create thumbs img = Image.open(thumb.file) if id == 'new': DBSession.add(media) DBSession.flush() # TODO: Allow other formats? for key, xy in config['thumb_sizes'][media._thumb_dir].iteritems(): thumb_path = helpers.thumb_path(media, 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(media, '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: success = False message = 'Unsupported image type' except Exception, e: success = False message = e.message
def save_album_art(self, id, album_art, **kwargs): """Save album art uploaded with :class:`~mediacore.forms.media.AlbumArtForm`. :param id: Media ID. If ``"new"`` a new Media stub is created with :func:`~mediacore.model.media.create_media_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.media.Media.id` which is important if a new media has just been created. """ if id == 'new': media = create_media_stub() else: media = fetch_row(Media, id, incl_trash=True) im_path = os.path.join(config.image_dir, 'media/%s%s.%s') try: # Create thumbnails im = Image.open(album_art.file) if id == 'new': DBSession.add(media) DBSession.flush() # TODO: Allow other formats? for key, dimensions in config.album_art_sizes.iteritems(): file_path = im_path % (media.id, key, 'jpg') im.resize(dimensions, 1).save(file_path) # Backup the original image just for kicks orig_type = os.path.splitext(album_art.filename)[1].lower()[1:] backup_file = open(im_path % (media.id, 'orig', orig_type), 'w') copyfileobj(album_art.file, backup_file) album_art.file.close() backup_file.close() success = True message = None except IOError: success = False message = 'Unsupported image type' except Exception, e: success = False message = e.message
def add_file(self, id, file=None, url=None, **kwargs): """Save action for the :class:`~mediacore.forms.admin.media.AddFileForm`. Creates a new :class:`~mediacore.model.media.MediaFile` from the uploaded file or the local or remote URL. :param id: Media ID. If ``"new"`` a new Media stub is created with :func:`~mediacore.model.media.create_media_stub`. :type id: :class:`int` or ``"new"`` :param file: The uploaded file :type file: :class:`cgi.FieldStorage` or ``None`` :param url: A URL to a recognizable audio or video file :type url: :class:`unicode` or ``None`` :rtype: JSON dict :returns: success bool message Error message, if unsuccessful media_id The :attr:`~mediacore.model.media.Media.id` which is important if new media has just been created. file_id The :attr:`~mediacore.model.media.MediaFile.id` for the newly created file. edit_form The rendered XHTML :class:`~mediacore.forms.admin.media.EditFileForm` for this file. status_form The rendered XHTML :class:`~mediacore.forms.admin.media.UpdateStatusForm` """ if id == 'new': media = create_media_stub() else: media = fetch_row(Media, id) data = {'success': False} if file is not None: # Create a media object, add it to the video, and store the file permanently. try: media_file = _add_new_media_file(media, file.filename, file.file) data['success'] = True except Invalid, e: data['message'] = unicode(e)
def add_file(self, id, file=None, url=None, **kwargs): """Save action for the :class:`~mediacore.forms.media.AddFileForm`. Creates a new :class:`~mediacore.model.media.MediaFile` from the uploaded file or the local or remote URL. :param id: Media ID. If ``"new"`` a new Media stub is created with :func:`~mediacore.model.media.create_media_stub`. :type id: :class:`int` or ``"new"`` :param file: The uploaded file :type file: :class:`cgi.FieldStorage` or ``None`` :param url: A URL to a recognizable audio or video file :type url: :class:`unicode` or ``None`` :rtype: JSON dict :returns: success bool message Error message, if unsuccessful media_id The :attr:`~mediacore.model.media.Media.id` which is important if new media has just been created. file_id The :attr:`~mediacore.model.media.MediaFile.id` for the newly created file. edit_form The rendered XHTML :class:`~mediacore.forms.media.EditFileForm` for this file. status_form The rendered XHTML :class:`~mediacore.forms.media.UpdateStatusForm` """ if id == 'new': media = create_media_stub() else: media = fetch_row(Media, id, incl_trash=True) try: if file is not None: # Create a media object, add it to the video, and store the file permanently. media_file = _add_new_media_file(media, file.filename, file.file) elif url: media_file = MediaFile() # Parse the URL checking for known embeddables like YouTube for type, info in config.embeddable_filetypes.iteritems(): match = re.match(info['pattern'], url) if match: media_file.type = type media_file.url = match.group('id') media_file.enable_feed = False break else: # Check for types we can play ourselves type = os.path.splitext(url)[1].lower()[1:] for playable_types in config.playable_types.itervalues(): if type in playable_types: media_file.type = type media_file.url = url break else: raise Exception, 'Unsupported URL %s' % url else: raise Exception, 'Given no action to perform.' media.files.append(media_file) media.update_type() media.update_status() DBSession.add(media) DBSession.flush() # Render some widgets so the XHTML can be injected into the page edit_form_xhtml = unicode(edit_file_form.display( action=url_for(action='edit_file', id=media.id), file=media_file)) status_form_xhtml = unicode(update_status_form.display( action=url_for(action='update_status', id=media.id), media=media)) return dict( success = True, media_id = media.id, file_id = media_file.id, edit_form = edit_form_xhtml, status_form = status_form_xhtml, ) except Exception, e: return dict( success = False, message = e.message, )
def add_file(self, id, file=None, url=None, **kwargs): """Save action for the :class:`~mediacore.forms.admin.media.AddFileForm`. Creates a new :class:`~mediacore.model.media.MediaFile` from the uploaded file or the local or remote URL. :param id: Media ID. If ``"new"`` a new Media stub is created with :func:`~mediacore.model.media.create_media_stub`. :type id: :class:`int` or ``"new"`` :param file: The uploaded file :type file: :class:`cgi.FieldStorage` or ``None`` :param url: A URL to a recognizable audio or video file :type url: :class:`unicode` or ``None`` :rtype: JSON dict :returns: success bool message Error message, if unsuccessful media_id The :attr:`~mediacore.model.media.Media.id` which is important if new media has just been created. file_id The :attr:`~mediacore.model.media.MediaFile.id` for the newly created file. edit_form The rendered XHTML :class:`~mediacore.forms.admin.media.EditFileForm` for this file. status_form The rendered XHTML :class:`~mediacore.forms.admin.media.UpdateStatusForm` """ if id == 'new': media = create_media_stub() else: media = fetch_row(Media, id) data = dict(success=False) if file is not None: # Create a media object, add it to the video, and store the file permanently. media_file = _add_new_media_file(media, file.filename, file.file) data['success'] = True elif url: media_file = MediaFile() # Parse the URL checking for known embeddables like YouTube for type, info in external_embedded_containers.iteritems(): match = info['pattern'].match(url) if match: media_file.type = guess_media_type(type) media_file.container = type media_file.embed = match.group('id') media_file.display_name = type.capitalize() + ' ID: ' + media_file.embed data['success'] = True break else: # Check for types we can play ourselves try: ext = os.path.splitext(url)[1].lower()[1:] container = guess_container_format(ext) except KeyError: container = None for conts in playable_containers.itervalues(): if container in conts: media_file.type = guess_media_type(container) media_file.container = container media_file.url = url media_file.display_name = os.path.basename(url) data['success'] = True break else: data['message'] = 'Unsupported URL' else: data['message'] = 'No action to perform.' if data['success']: media.files.append(media_file) media.update_type() media.update_status() DBSession.add(media) DBSession.flush() if id == 'new': helpers.create_default_thumbs_for(media) # Render some widgets so the XHTML can be injected into the page edit_form_xhtml = unicode(edit_file_form.display( action=url_for(action='edit_file', id=media.id), file=media_file)) status_form_xhtml = unicode(update_status_form.display( action=url_for(action='update_status', id=media.id), media=media)) data.update(dict( media_id = media.id, file_id = media_file.id, file_type = media_file.type, edit_form = edit_form_xhtml, status_form = status_form_xhtml, )) return data