Exemple #1
0
def image_handler(request, model_name, field_name, key, extension):
    extension = extension.upper()
    if extension not in VALID_EXTENSIONS:
        raise Http404("Unknown extension: %s" % extension)

    try:
        field_config = THUMBNAIL_OPTIONS[model_name][field_name]
    except KeyError:
        raise Http404("No configuration found for %s.%s" % (model_name, field_name))

    try:
        instance = field_config.get_instance(key)
    except ObjectDoesNotExist:
        raise Http404("%s with key '%s' not found" % (model_name, key))

    file_field = getattr(instance, field_name)

    config = request.GET.get('config', [])
    if config:
        try:
            config = decode_from_url(config, model_name, field_name, key, instance=instance)
        except:
            if settings.DEBUG:
                raise
            else:
                raise Http404("Could not decode configuration.")
    else:
        raise Http404("No image configuration provided.")

    if settings.THUMBNAIL_DEBUG and \
        request.GET.get('echo', 'no').lower() in ['true', 'yes']:
        return render_to_response("sorl_url/debug.html", config)

    options = dict(field_config.options)
    options.update(config.get('options', {}))
    options['format'] = VALID_EXTENSIONS[extension]

    backend = field_config.model_config.get_backend()
    thumbnail = backend.get_thumbnail(file_field, config['geometry'], **options)

    if getattr(settings, 'SORL_URL_SERVE_DIRECT', False):
        import mimetypes
        import time
        from django.http import CompatibleStreamingHttpResponse
        from django.utils.http import http_date

        mimetype, encoding = mimetypes.guess_type("prefix.%s" % extension)
        epoch_timestamp = time.time()
        mimetype = mimetype or 'application/octet-stream'
        data = thumbnail.read()

        response = CompatibleStreamingHttpResponse(data, content_type=mimetype)
        response["Last-Modified"] = http_date(epoch_timestamp)
        response["Content-Length"] = len(data)
        return response

    if getattr(settings, 'SORL_URL_PERMANENT_REDIRECT', False):
        redirect = HttpResponsePermanentRedirect
    else:
        redirect = HttpResponseRedirect

    return redirect(thumbnail.url)