Beispiel #1
0
def ajax_upload_media(request, upload_type):
    errors = []
    upload_hash = ''

    if 'upload_image' in request.FILES:
        upload_preview = request.FILES['upload_image']
        upload_preview.seek(0)

        is_icon = upload_type == 'icon'
        is_video = (upload_preview.content_type in amo.VIDEO_TYPES and
                    waffle.switch_is_active('video-upload'))

        # By pushing the type onto the instance hash, we can easily see what
        # to do with the file later.
        ext = upload_preview.content_type.replace('/', '-')
        upload_hash = '%s.%s' % (uuid.uuid4().hex, ext)
        loc = os.path.join(settings.TMP_PATH, upload_type, upload_hash)

        with storage.open(loc, 'wb') as fd:
            for chunk in upload_preview:
                fd.write(chunk)

        if is_video:
            if not video_library:
                errors.append(_('Video support not enabled.'))
            else:
                video = video_library(loc)
                video.get_meta()
                if not video.is_valid():
                    errors.extend(video.errors)

        else:
            check = amo.utils.ImageCheck(upload_preview)
            if (not check.is_image() or
                upload_preview.content_type not in amo.IMG_TYPES):
                if is_icon:
                    errors.append(_('Icons must be either PNG or JPG.'))
                else:
                    errors.append(_('Images must be either PNG or JPG.'))

            if check.is_animated():
                if is_icon:
                    errors.append(_('Icons cannot be animated.'))
                else:
                    errors.append(_('Images cannot be animated.'))

        max_size = (settings.MAX_ICON_UPLOAD_SIZE if is_icon else
                    settings.MAX_VIDEO_UPLOAD_SIZE if is_video else None)

        if max_size and upload_preview.size > max_size:
            if is_icon or is_video:
                errors.append(_('Please use files smaller than %dMB.') % (
                    max_size / 1024 / 1024 - 1))
    else:
        errors.append(_('There was an error uploading your preview.'))

    if errors:
        upload_hash = ''

    return {'upload_hash': upload_hash, 'errors': errors}
Beispiel #2
0
def ajax_upload_media(request, upload_type):
    errors = []
    upload_hash = ""

    if "upload_image" in request.FILES:
        upload_preview = request.FILES["upload_image"]
        upload_preview.seek(0)

        is_icon = upload_type == "icon"
        is_video = upload_preview.content_type in amo.VIDEO_TYPES and waffle.switch_is_active("video-upload")

        # By pushing the type onto the instance hash, we can easily see what
        # to do with the file later.
        ext = upload_preview.content_type.replace("/", "-")
        upload_hash = "%s.%s" % (uuid.uuid4().hex, ext)
        loc = os.path.join(settings.TMP_PATH, upload_type, upload_hash)

        with storage.open(loc, "wb") as fd:
            for chunk in upload_preview:
                fd.write(chunk)

        if is_video:
            if not video_library:
                errors.append(_("Video support not enabled."))
            else:
                video = video_library(loc)
                video.get_meta()
                if not video.is_valid():
                    errors.extend(video.errors)

        else:
            check = amo.utils.ImageCheck(upload_preview)
            if not check.is_image() or upload_preview.content_type not in amo.IMG_TYPES:
                if is_icon:
                    errors.append(_("Icons must be either PNG or JPG."))
                else:
                    errors.append(_("Images must be either PNG or JPG."))

            if check.is_animated():
                if is_icon:
                    errors.append(_("Icons cannot be animated."))
                else:
                    errors.append(_("Images cannot be animated."))

        max_size = settings.MAX_ICON_UPLOAD_SIZE if is_icon else settings.MAX_VIDEO_UPLOAD_SIZE if is_video else None

        if max_size and upload_preview.size > max_size:
            if is_icon or is_video:
                errors.append(_("Please use files smaller than %dMB.") % (max_size / 1024 / 1024 - 1))
    else:
        errors.append(_("There was an error uploading your preview."))

    if errors:
        upload_hash = ""

    return {"upload_hash": upload_hash, "errors": errors}
Beispiel #3
0
def check_upload(file_obj, upload_type, content_type):
    errors = []
    upload_hash = ''
    is_icon = upload_type == 'icon'
    is_video = (content_type in amo.VIDEO_TYPES and
                waffle.switch_is_active('video-upload'))

    # By pushing the type onto the instance hash, we can easily see what
    # to do with the file later.
    ext = content_type.replace('/', '-')
    upload_hash = '%s.%s' % (uuid.uuid4().hex, ext)
    loc = os.path.join(settings.TMP_PATH, upload_type, upload_hash)

    with storage.open(loc, 'wb') as fd:
        for chunk in file_obj:
            fd.write(chunk)

    if is_video:
        if not video_library:
            errors.append(_('Video support not enabled.'))
        else:
            video = video_library(loc)
            video.get_meta()
            if not video.is_valid():
                errors.extend(video.errors)

    else:
        check = amo.utils.ImageCheck(file_obj)
        if (not check.is_image() or
            content_type not in amo.IMG_TYPES):
            if is_icon:
                errors.append(_('Icons must be either PNG or JPG.'))
            else:
                errors.append(_('Images must be either PNG or JPG.'))

        if check.is_animated():
            if is_icon:
                errors.append(_('Icons cannot be animated.'))
            else:
                errors.append(_('Images cannot be animated.'))

    max_size = (settings.MAX_ICON_UPLOAD_SIZE if is_icon else
                settings.MAX_VIDEO_UPLOAD_SIZE if is_video else None)

    if max_size and file_obj.size > max_size:
        if is_icon or is_video:
            errors.append(_('Please use files smaller than %s.') %
                filesizeformat(max_size))

    return errors, upload_hash
Beispiel #4
0
def check_upload(file_obj, upload_type, content_type):
    errors = []
    upload_hash = ''
    is_icon = upload_type == 'icon'
    is_image_asset = upload_type == 'image'
    is_video = (content_type in amo.VIDEO_TYPES and
                waffle.switch_is_active('video-upload'))

    # By pushing the type onto the instance hash, we can easily see what
    # to do with the file later.
    ext = content_type.replace('/', '-')
    upload_hash = '%s.%s' % (uuid.uuid4().hex, ext)
    loc = os.path.join(settings.TMP_PATH, upload_type, upload_hash)

    with storage.open(loc, 'wb') as fd:
        for chunk in file_obj:
            fd.write(chunk)

    # A flag to prevent us from attempting to open the image with PIL.
    do_not_open = False

    if is_video:
        if not video_library:
            errors.append(_('Video support not enabled.'))
        else:
            video = video_library(loc)
            video.get_meta()
            if not video.is_valid():
                errors.extend(video.errors)

    else:
        check = amo.utils.ImageCheck(file_obj)
        if (not check.is_image() or
            content_type not in amo.IMG_TYPES):
            do_not_open = True
            if is_icon:
                errors.append(_('Icons must be either PNG or JPG.'))
            else:
                errors.append(_('Images must be either PNG or JPG.'))

        if check.is_animated():
            do_not_open = True
            if is_icon:
                errors.append(_('Icons cannot be animated.'))
            else:
                errors.append(_('Images cannot be animated.'))

    max_size = (settings.MAX_ICON_UPLOAD_SIZE if is_icon else
                settings.MAX_IMAGE_UPLOAD_SIZE if is_image_asset else
                settings.MAX_VIDEO_UPLOAD_SIZE if is_video else None)

    if max_size and file_obj.size > max_size:
        do_not_open = True
        if is_icon or is_video:
            errors.append(_('Please use files smaller than %s.') %
                filesizeformat(max_size))

    if is_icon and not do_not_open:
        file_obj.seek(0)
        try:
            im = Image.open(file_obj)
            im.verify()
        except IOError:
            errors.append(_('Icon could not be opened'))
        else:
            size_x, size_y = im.size
            # TODO: This should go away when we allow uploads for individual
            # icon sizes.
            if size_x < 128 or size_y < 128:
                errors.append(_('Icons must be at least 128px by 128px.'))

            if size_x != size_y:
                errors.append(_('Icons must be square.'))

    return errors, upload_hash
Beispiel #5
0
def check_upload(file_obj, upload_type, content_type):
    errors = []
    upload_hash = ''
    is_icon = upload_type == 'icon'
    is_preview = upload_type == 'preview'
    is_video = content_type in amo.VIDEO_TYPES

    if not any([is_icon, is_preview, is_video]):
        raise ValueError('Unknown upload type.')

    # By pushing the type onto the instance hash, we can easily see what
    # to do with the file later.
    ext = content_type.replace('/', '-')
    upload_hash = '%s.%s' % (uuid.uuid4().hex, ext)
    loc = os.path.join(settings.TMP_PATH, upload_type, upload_hash)

    with storage.open(loc, 'wb') as fd:
        for chunk in file_obj:
            fd.write(chunk)

    # A flag to prevent us from attempting to open the image with PIL.
    do_not_open = False

    if is_video:
        if not video_library:
            errors.append(_('Video support not enabled.'))
        else:
            video = video_library(loc)
            video.get_meta()
            if not video.is_valid():
                errors.extend(video.errors)

    else:
        check = amo.utils.ImageCheck(file_obj)
        if (not check.is_image() or content_type not in amo.IMG_TYPES):
            do_not_open = True
            if is_icon:
                errors.append(_('Icons must be either PNG or JPG.'))
            else:
                errors.append(_('Images must be either PNG or JPG.'))

        if check.is_animated():
            do_not_open = True
            if is_icon:
                errors.append(_('Icons cannot be animated.'))
            else:
                errors.append(_('Images cannot be animated.'))

    max_size = (settings.MAX_ICON_UPLOAD_SIZE
                if is_icon else settings.MAX_VIDEO_UPLOAD_SIZE if is_video else
                settings.MAX_IMAGE_UPLOAD_SIZE if is_preview else None)

    if max_size and file_obj.size > max_size:
        do_not_open = True
        if is_icon or is_video:
            errors.append(
                _('Please use files smaller than %s.') %
                filesizeformat(max_size))

    if (is_icon or is_preview) and not is_video and not do_not_open:
        file_obj.seek(0)
        try:
            im = Image.open(file_obj)
            im.verify()
        except IOError:
            if is_icon:
                errors.append(_('Icon could not be opened.'))
            elif is_preview:
                errors.append(_('Preview could not be opened.'))
        else:
            size_x, size_y = im.size
            if is_icon:
                # TODO: This should go away when we allow uploads for
                # individual icon sizes.
                if size_x < 128 or size_y < 128:
                    errors.append(_('Icons must be at least 128px by 128px.'))

                if size_x != size_y:
                    errors.append(_('Icons must be square.'))

            elif is_preview:
                if (size_x < APP_PREVIEW_MINIMUMS[0]
                        or size_y < APP_PREVIEW_MINIMUMS[1]) and (
                            size_x < APP_PREVIEW_MINIMUMS[1]
                            or size_y < APP_PREVIEW_MINIMUMS[0]):
                    errors.append(
                        # L10n: {0} and {1} are the height/width of the preview
                        # in px.
                        _('App previews must be at least {0}px by {1}px or '
                          '{1}px by {0}px.').format(*APP_PREVIEW_MINIMUMS))

    return errors, upload_hash
Beispiel #6
0
def check_upload(file_obj, upload_type, content_type):
    errors = []
    upload_hash = ''
    is_icon = upload_type == 'icon'
    is_preview = upload_type == 'preview'
    is_video = content_type in amo.VIDEO_TYPES

    if not any([is_icon, is_preview, is_video]):
        raise ValueError('Unknown upload type.')

    # By pushing the type onto the instance hash, we can easily see what
    # to do with the file later.
    ext = content_type.replace('/', '-')
    upload_hash = '%s.%s' % (uuid.uuid4().hex, ext)
    loc = os.path.join(settings.TMP_PATH, upload_type, upload_hash)

    with storage.open(loc, 'wb') as fd:
        for chunk in file_obj:
            fd.write(chunk)

    # A flag to prevent us from attempting to open the image with PIL.
    do_not_open = False

    if is_video:
        if not video_library:
            errors.append(_('Video support not enabled.'))
        else:
            video = video_library(loc)
            video.get_meta()
            if not video.is_valid():
                errors.extend(video.errors)

    else:
        check = amo.utils.ImageCheck(file_obj)
        if (not check.is_image() or
            content_type not in amo.IMG_TYPES):
            do_not_open = True
            if is_icon:
                errors.append(_('Icons must be either PNG or JPG.'))
            else:
                errors.append(_('Images must be either PNG or JPG.'))

        if check.is_animated():
            do_not_open = True
            if is_icon:
                errors.append(_('Icons cannot be animated.'))
            else:
                errors.append(_('Images cannot be animated.'))

    max_size = (settings.MAX_ICON_UPLOAD_SIZE if is_icon else
                settings.MAX_VIDEO_UPLOAD_SIZE if is_video else
                settings.MAX_IMAGE_UPLOAD_SIZE if is_preview else None)

    if max_size and file_obj.size > max_size:
        do_not_open = True
        if is_icon or is_video:
            errors.append(_('Please use files smaller than %s.') %
                filesizeformat(max_size))

    if (is_icon or is_preview) and not is_video and not do_not_open:
        file_obj.seek(0)
        try:
            im = Image.open(file_obj)
            im.verify()
        except IOError:
            if is_icon:
                errors.append(_('Icon could not be opened.'))
            elif is_preview:
                errors.append(_('Preview could not be opened.'))
        else:
            size_x, size_y = im.size
            if is_icon:
                # TODO: This should go away when we allow uploads for
                # individual icon sizes.
                if size_x < 128 or size_y < 128:
                    errors.append(_('Icons must be at least 128px by 128px.'))

                if size_x != size_y:
                    errors.append(_('Icons must be square.'))

            elif is_preview:
                if (size_x < APP_PREVIEW_MINIMUMS[0] or
                    size_y < APP_PREVIEW_MINIMUMS[1]) and (
                        size_x < APP_PREVIEW_MINIMUMS[1] or
                        size_y < APP_PREVIEW_MINIMUMS[0]):
                    errors.append(
                        # L10n: {0} and {1} are the height/width of the preview
                        # in px.
                        _('App previews must be at least {0}px by {1}px or '
                          '{1}px by {0}px.').format(*APP_PREVIEW_MINIMUMS))

    return errors, upload_hash
Beispiel #7
0
def check_upload(file_obj, upload_type, content_type):
    errors = []
    upload_hash = ''
    is_icon = upload_type == 'icon'
    is_image_asset = upload_type == 'image'
    is_video = (content_type in amo.VIDEO_TYPES
                and waffle.switch_is_active('video-upload'))

    # By pushing the type onto the instance hash, we can easily see what
    # to do with the file later.
    ext = content_type.replace('/', '-')
    upload_hash = '%s.%s' % (uuid.uuid4().hex, ext)
    loc = os.path.join(settings.TMP_PATH, upload_type, upload_hash)

    with storage.open(loc, 'wb') as fd:
        for chunk in file_obj:
            fd.write(chunk)

    # A flag to prevent us from attempting to open the image with PIL.
    do_not_open = False

    if is_video:
        if not video_library:
            errors.append(_('Video support not enabled.'))
        else:
            video = video_library(loc)
            video.get_meta()
            if not video.is_valid():
                errors.extend(video.errors)

    else:
        check = amo.utils.ImageCheck(file_obj)
        if (not check.is_image() or content_type not in amo.IMG_TYPES):
            do_not_open = True
            if is_icon:
                errors.append(_('Icons must be either PNG or JPG.'))
            else:
                errors.append(_('Images must be either PNG or JPG.'))

        if check.is_animated():
            do_not_open = True
            if is_icon:
                errors.append(_('Icons cannot be animated.'))
            else:
                errors.append(_('Images cannot be animated.'))

    max_size = (settings.MAX_ICON_UPLOAD_SIZE if is_icon else
                settings.MAX_IMAGE_UPLOAD_SIZE if is_image_asset else
                settings.MAX_VIDEO_UPLOAD_SIZE if is_video else None)

    if max_size and file_obj.size > max_size:
        do_not_open = True
        if is_icon or is_video:
            errors.append(
                _('Please use files smaller than %s.') %
                filesizeformat(max_size))

    if is_icon and not do_not_open:
        file_obj.seek(0)
        try:
            im = Image.open(file_obj)
            im.verify()
        except IOError:
            errors.append(_('Icon could not be opened'))
        else:
            size_x, size_y = im.size
            # TODO: This should go away when we allow uploads for individual
            # icon sizes.
            if size_x < 128 or size_y < 128:
                errors.append(_('Icons must be at least 128px by 128px.'))

            if size_x != size_y:
                errors.append(_('Icons must be square.'))

    return errors, upload_hash