Example #1
0
def photo_size(request,
               id,
               size,
               crop=False,
               quality=90,
               download=False,
               constrain=False):
    """
    Renders image and returns response
    Does not use template
    Saves resized image within cache system
    Returns 404 if if image rendering fails
    """

    if isinstance(quality, str) and quality.isdigit():
        quality = int(quality)

    cache_key = generate_image_cache_key(file=id,
                                         size=size,
                                         pre_key=PHOTO_PRE_KEY,
                                         crop=crop,
                                         unique_key=id,
                                         quality=quality,
                                         constrain=constrain)
    cached_image = cache.get(cache_key)
    if cached_image:
        return redirect('{0}{1}'.format(
            get_setting('site', 'global', 'siteurl'), cached_image))

    photo = get_object_or_404(Image, id=id)
    size = [int(s) for s in size.split('x')]
    size = aspect_ratio(photo.image_dimensions(), size, constrain)

    # check permissions
    if not has_perm(request.user, 'photos.view_image', photo):
        raise Http403

    attachment = ''
    if download:
        attachment = 'attachment;'

    if not photo.image or not default_storage.exists(photo.image.name):
        raise Http404

    # At this point, we didn't get the image from the cache.
    # Check if this particular thumbnail already exists on file system.
    # If it's there, no need to rebuild it from the original image!
    file_name = photo.image_filename()
    file_path = 'cached%s%s' % (request.path, file_name)
    if default_storage.exists(file_path):
        image = get_image_from_path(
            os.path.join(settings.MEDIA_ROOT, file_path))
    else:
        # gets resized image from cache or rebuild
        image = get_image(photo.image,
                          size,
                          PHOTO_PRE_KEY,
                          crop=crop,
                          quality=quality,
                          unique_key=str(photo.pk),
                          constrain=constrain)

    # if image not rendered; quit
    if not image:
        raise Http404

    response = HttpResponse(content_type='image/jpeg')
    response['Content-Disposition'] = '%s filename="%s"' % (
        attachment, photo.image_filename())
    image.convert('RGB').save(response, "JPEG", quality=quality)

    if photo.is_public_photo() and photo.is_public_photoset():
        if not default_storage.exists(file_path):
            default_storage.save(file_path, ContentFile(response.content))
        full_file_path = "%s%s" % (settings.MEDIA_URL, file_path)
        cache.set(cache_key, full_file_path)
        cache_group_key = "photos_cache_set.%s" % photo.pk
        cache_group_list = cache.get(cache_group_key)

        if cache_group_list is None:
            cache.set(cache_group_key, [cache_key])
        else:
            cache_group_list += [cache_key]
            cache.set(cache_group_key, cache_group_list)

    return response
Example #2
0
def photo_size(request, id, size, crop=False, quality=90, download=False, constrain=False):
    """
    Renders image and returns response
    Does not use template
    Saves resized image within cache system
    Returns 404 if if image rendering fails
    """

    if isinstance(quality, unicode) and quality.isdigit():
        quality = int(quality)

    cache_key = generate_image_cache_key(file=id, size=size, pre_key=PHOTO_PRE_KEY, crop=crop, unique_key=id, quality=quality, constrain=constrain)
    cached_image = cache.get(cache_key)
    if cached_image:
        return redirect('{0}{1}'.format(get_setting('site', 'global', 'siteurl'), cached_image))

    photo = get_object_or_404(Image, id=id)
    size = [int(s) for s in size.split('x')]
    size = aspect_ratio(photo.image_dimensions(), size, constrain)

    # check permissions
    if not has_perm(request.user, 'photos.view_image', photo):
        raise Http403

    attachment = ''
    if download:
        attachment = 'attachment;'

    if not photo.image or not default_storage.exists(photo.image.name):
        raise Http404
    
    # At this point, we didn't get the image from the cache.
    # Check if this particular thumbnail already exists on file system.
    # If it's there, no need to rebuild it from the original image!
    file_name = photo.image_filename()
    file_path = 'cached%s%s' % (request.path, file_name)
    if default_storage.exists(file_path):
        image = get_image_from_path(os.path.join(settings.MEDIA_ROOT, file_path))
    else:
        # gets resized image from cache or rebuild
        image = get_image(photo.image, size, PHOTO_PRE_KEY, crop=crop, quality=quality, unique_key=str(photo.pk), constrain=constrain)

    # if image not rendered; quit
    if not image:
        raise Http404

    response = HttpResponse(content_type='image/jpeg')
    response['Content-Disposition'] = '%s filename="%s"' % (attachment, photo.image_filename())
    image.convert('RGB').save(response, "JPEG", quality=quality)

    if photo.is_public_photo() and photo.is_public_photoset():
        if not default_storage.exists(file_path):
            default_storage.save(file_path, ContentFile(response.content))
        full_file_path = "%s%s" % (settings.MEDIA_URL, file_path)
        cache.set(cache_key, full_file_path)
        cache_group_key = "photos_cache_set.%s" % photo.pk
        cache_group_list = cache.get(cache_group_key)

        if cache_group_list is None:
            cache.set(cache_group_key, [cache_key])
        else:
            cache_group_list += [cache_key]
            cache.set(cache_group_key, cache_group_list)

    return response