コード例 #1
0
ファイル: upload.py プロジェクト: 86me/mediacore
    def _save_media_obj(self, name, email, title, description, tags, file, url):
        # create our media object as a status-less placeholder initially
        media_obj = Media()
        media_obj.author = Author(name, email)
        media_obj.title = title
        media_obj.slug = get_available_slug(Media, title)
        media_obj.description = description
        media_obj.notes = fetch_setting('wording_additional_notes')
        media_obj.set_tags(tags)

        # Create a media object, add it to the media_obj, and store the file permanently.
        if file is not None:
            media_file = _add_new_media_file(media_obj, file.filename, file.file)
        else:
            # FIXME: For some reason the media.type isn't ever set to video
            #        during this request. On subsequent requests, when
            #        media_obj.update_type() is called, it is set properly.
            #        This isn't too serious an issue right now because
            #        it is called the first time a moderator goes to review
            #        the new media_obj.
            media_file = MediaFile()
            url = unicode(url)
            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
                    break
            else:
                # Trigger a validation error on the whole form.
                raise formencode.Invalid('Please specify a URL or upload a file below.', None, None)
            media_obj.files.append(media_file)

        # Add the final changes.
        media_obj.update_type()
        media_obj.update_status()
        DBSession.add(media_obj)
        DBSession.flush()

        create_default_thumbs_for(media_obj)

        return media_obj
コード例 #2
0
ファイル: media.py プロジェクト: 86me/mediacore
    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