Exemplo n.º 1
0
def create_imageattachment(files, user, obj):
    """
    Given an uploaded file, a user and an object, it creates an ImageAttachment
    owned by `user` and attached to `obj`.
    """
    up_file = files.values()[0]
    check_file_size(up_file, settings.IMAGE_MAX_FILESIZE)

    (up_file, is_animated) = _image_to_png(up_file)

    image = ImageAttachment(content_object=obj, creator=user)
    image.file.save(up_file.name, File(up_file), save=True)

    # Compress and generate thumbnail off thread
    generate_thumbnail.delay(image, 'file', 'thumbnail')
    if not is_animated:
        compress_image.delay(image, 'file')

    (width, height) = _scale_dimensions(image.file.width, image.file.height)
    return {
        'name': up_file.name,
        'url': image.file.url,
        'thumbnail_url': image.thumbnail_if_set().url,
        'width': width,
        'height': height,
        'delete_url': image.get_delete_url()
    }
Exemplo n.º 2
0
def create_imageattachment(files, user, obj):
    """
    Given an uploaded file, a user and an object, it creates an ImageAttachment
    owned by `user` and attached to `obj`.
    """
    up_file = files.values()[0]
    check_file_size(up_file, settings.IMAGE_MAX_FILESIZE)

    up_file = _image_to_png(up_file)

    image = ImageAttachment(content_object=obj, creator=user)
    image.file.save(os.path.splitext(up_file.name)[0] + '.png',
                    File(up_file),
                    save=True)

    # Compress and generate thumbnail off thread
    generate_thumbnail.delay(image, 'file', 'thumbnail')
    compress_image.delay(image, 'file')

    (width, height) = _scale_dimensions(image.file.width, image.file.height)
    return {
        'name': up_file.name,
        'url': image.file.url,
        'thumbnail_url': image.thumbnail_if_set().url,
        'width': width,
        'height': height,
        'delete_url': image.get_delete_url()
    }
Exemplo n.º 3
0
def upload(request, media_type='image'):
    """Finalizes an uploaded draft."""
    drafts = _get_drafts(request.user)
    if media_type == 'image' and drafts['image']:
        # We're publishing an image draft!
        image_form = _init_media_form(ImageForm, request, drafts['image'][0])
        if image_form.is_valid():
            img = image_form.save(is_draft=None)
            generate_thumbnail.delay(img, 'file', 'thumbnail')
            # TODO: We can drop this when we start using Redis.
            invalidate = Image.objects.exclude(pk=img.pk)
            if invalidate.exists():
                Image.objects.invalidate(invalidate[0])
            return HttpResponseRedirect(img.get_absolute_url())
        else:
            return gallery(request, media_type='image')
    elif media_type == 'video' and drafts['video']:
        # We're publishing a video draft!
        video_form = _init_media_form(VideoForm, request, drafts['video'][0])
        if video_form.is_valid():
            vid = video_form.save(is_draft=None)
            if vid.thumbnail:
                generate_thumbnail.delay(vid, 'poster', 'poster',
                                         max_size=settings.WIKI_VIDEO_WIDTH)
                generate_thumbnail.delay(vid, 'thumbnail', 'thumbnail')
            # TODO: We can drop this when we start using Redis.
            invalidate = Video.objects.exclude(pk=vid.pk)
            if invalidate.exists():
                Video.objects.invalidate(invalidate[0])
            return HttpResponseRedirect(vid.get_absolute_url())
        else:
            return gallery(request, media_type='video')

    return HttpResponseBadRequest(u'Unrecognized POST request.')
Exemplo n.º 4
0
def create_video(files, user):
    """Given an uploaded file, a user, and other data, it creates a Video"""
    # Async uploads fallback to these defaults.
    title = get_draft_title(user)
    description = u'Autosaved draft.'
    # Use default locale to make sure a user can only have one draft
    locale = settings.WIKI_DEFAULT_LANGUAGE
    try:
        vid = Video.objects.get(title=title, locale=locale)
    except Video.DoesNotExist:
        vid = Video(title=title, creator=user, description=description,
                    locale=locale)
    for name in files:
        up_file = files[name]
        check_file_size(up_file, settings.VIDEO_MAX_FILESIZE)
        # name is in (webm, ogv, flv) sent from upload_video(), below
        getattr(vid, name).save(up_file.name, up_file, save=False)

        # Generate thumbnail off thread
        if 'thumbnail' == name:
            generate_thumbnail.delay(vid, 'thumbnail', 'poster',
                                     max_size=settings.WIKI_VIDEO_WIDTH)
            generate_thumbnail.delay(vid, 'thumbnail', 'thumbnail')

    vid.save()
    if 'thumbnail' in files:
        thumb = vid.thumbnail
        (width, height) = _scale_dimensions(thumb.width, thumb.height)
    else:
        width = settings.THUMBNAIL_PROGRESS_WIDTH
        height = settings.THUMBNAIL_PROGRESS_HEIGHT
    delete_url = reverse('gallery.delete_media', args=['video', vid.id])
    return {'name': up_file.name, 'url': vid.get_absolute_url(),
            'thumbnail_url': vid.thumbnail_url_if_set(),
            'width': width,
            'height': height,
            'delete_url': delete_url}
Exemplo n.º 5
0
def create_image(files, user):
    """Given an uploaded file, a user, and other data, it creates an Image"""
    up_file = files.values()[0]
    check_file_size(up_file, settings.IMAGE_MAX_FILESIZE)

    # Async uploads fallback to these defaults.
    title = get_draft_title(user)
    description = u'Autosaved draft.'
    # Use default locale to make sure a user can only have one draft
    locale = settings.WIKI_DEFAULT_LANGUAGE

    image = Image(title=title, creator=user, locale=locale,
                  description=description)
    image.file.save(up_file.name, File(up_file), save=True)

    # Generate thumbnail off thread
    generate_thumbnail.delay(image, 'file', 'thumbnail')

    (width, height) = _scale_dimensions(image.file.width, image.file.height)
    delete_url = reverse('gallery.delete_media', args=['image', image.id])
    return {'name': up_file.name, 'url': image.get_absolute_url(),
            'thumbnail_url': image.thumbnail_url_if_set(),
            'width': width, 'height': height,
            'delete_url': delete_url}
Exemplo n.º 6
0
def upload(request, media_type='image'):
    """Finalizes an uploaded draft."""
    drafts = _get_drafts(request.user)
    if media_type == 'image' and drafts['image']:
        # We're publishing an image draft!
        image_form = _init_media_form(ImageForm, request, drafts['image'][0])
        if image_form.is_valid():
            img = image_form.save(is_draft=None)
            generate_thumbnail.delay(img, 'file', 'thumbnail')
            compress_image.delay(img, 'file')
            # TODO: We can drop this when we start using Redis.
            invalidate = Image.objects.exclude(pk=img.pk)
            if invalidate.exists():
                Image.objects.invalidate(invalidate[0])
            # Rebuild KB
            schedule_rebuild_kb()
            return HttpResponseRedirect(img.get_absolute_url())
        else:
            return gallery(request, media_type='image')
    elif media_type == 'video' and drafts['video']:
        # We're publishing a video draft!
        video_form = _init_media_form(VideoForm, request, drafts['video'][0])
        if video_form.is_valid():
            vid = video_form.save(is_draft=None)
            if vid.thumbnail:
                generate_thumbnail.delay(vid,
                                         'poster',
                                         'poster',
                                         max_size=settings.WIKI_VIDEO_WIDTH)
                generate_thumbnail.delay(vid, 'thumbnail', 'thumbnail')
            # TODO: We can drop this when we start using Redis.
            invalidate = Video.objects.exclude(pk=vid.pk)
            if invalidate.exists():
                Video.objects.invalidate(invalidate[0])
            # Rebuild KB
            schedule_rebuild_kb()
            return HttpResponseRedirect(vid.get_absolute_url())
        else:
            return gallery(request, media_type='video')

    return HttpResponseBadRequest(u'Unrecognized POST request.')
Exemplo n.º 7
0
def upload(request, media_type='image'):
    """Finalizes an uploaded draft.

    We could probably use this same form to handle no-JS fallback, if
    we ever need to support that.

    """
    draft = _get_draft_info(request.user)
    if media_type == 'image' and draft['image']:
        # We're publishing an image draft!
        image_form = _init_media_form(ImageForm, request, draft['image'])
        if image_form.is_valid():
            img = image_form.save()
            generate_thumbnail.delay(img, 'file', 'thumbnail')
            # TODO: We can drop this when we start using Redis.
            invalidate = Image.objects.exclude(pk=img.pk)
            if invalidate.exists():
                Image.objects.invalidate(invalidate[0])
            return HttpResponseRedirect(img.get_absolute_url())
        else:
            return gallery(request, media_type='image')
    elif media_type == 'video' and draft['video']:
        # We're publishing a video draft!
        video_form = _init_media_form(VideoForm, request, draft['video'])
        if video_form.is_valid():
            vid = video_form.save()
            if vid.thumbnail:
                generate_thumbnail.delay(vid, 'poster', 'poster',
                                         max_size=settings.WIKI_VIDEO_WIDTH)
                generate_thumbnail.delay(vid, 'thumbnail', 'thumbnail')
            # TODO: We can drop this when we start using Redis.
            invalidate = Video.objects.exclude(pk=vid.pk)
            if invalidate.exists():
                Video.objects.invalidate(invalidate[0])
            return HttpResponseRedirect(vid.get_absolute_url())
        else:
            return gallery(request, media_type='video')

    return HttpResponseBadRequest(u'Unrecognized POST request.')
Exemplo n.º 8
0
def upload(request, media_type="image"):
    """Finalizes an uploaded draft."""
    drafts = _get_drafts(request.user)
    if media_type == "image" and drafts["image"]:
        # We're publishing an image draft!
        image_form = _init_media_form(ImageForm, request, drafts["image"][0])
        if image_form.is_valid():
            img = image_form.save(is_draft=None)
            generate_thumbnail.delay(img, "file", "thumbnail")
            compress_image.delay(img, "file")
            # TODO: We can drop this when we start using Redis.
            invalidate = Image.objects.exclude(pk=img.pk)
            if invalidate.exists():
                Image.objects.invalidate(invalidate[0])
            # Rebuild KB
            schedule_rebuild_kb()
            return HttpResponseRedirect(img.get_absolute_url())
        else:
            return gallery(request, media_type="image")
    elif media_type == "video" and drafts["video"]:
        # We're publishing a video draft!
        video_form = _init_media_form(VideoForm, request, drafts["video"][0])
        if video_form.is_valid():
            vid = video_form.save(is_draft=None)
            if vid.thumbnail:
                generate_thumbnail.delay(vid, "poster", "poster", max_size=settings.WIKI_VIDEO_WIDTH)
                generate_thumbnail.delay(vid, "thumbnail", "thumbnail")
            # TODO: We can drop this when we start using Redis.
            invalidate = Video.objects.exclude(pk=vid.pk)
            if invalidate.exists():
                Video.objects.invalidate(invalidate[0])
            # Rebuild KB
            schedule_rebuild_kb()
            return HttpResponseRedirect(vid.get_absolute_url())
        else:
            return gallery(request, media_type="video")

    return HttpResponseBadRequest(u"Unrecognized POST request.")