melinath / django-daguerre

On-the-fly image manipulation for Django.
http://django-daguerre.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
85 stars 15 forks source link

error with S3 #65

Closed brechmos closed 9 years ago

brechmos commented 9 years ago

I have all my images stored in S3 using boto etc. I am trying out methods of thumbnailing my images automatically and hence am trying daguerre.

The link to the s3 image starts with http:/com..s3.amazonaws.... For some reason it is missing a '/' in the http://.

I confirmed the two slashes are there by going to the django admin section. It does show two.

I looked through the code but don't see where it might be dropping a slash (or not adding two).

melinath commented 9 years ago

Great to hear you're trying out Daguerre! We have used daguerre with s3 in the past, so this should be fixable. Off the cuff, it sounds like it could be an issue with your MEDIA_URL setting. What's that set to?

melinath commented 9 years ago

Daguerre uses Django's internal mechanisms to resolve the ultimate URL for an adjusted image from the adjusted image's file path.

https://github.com/littleweaver/django-daguerre/blob/master/daguerre/helpers.py#L187 https://github.com/django/django/blob/master/django/db/models/fields/files.py#L64

So this is probably not a bug in Daguerre.

brechmos commented 9 years ago

MEDIA_URL='/'

I am using django-storages to store the images in S3. That part works for me. I'll take a look around the code and see if I can track it down. I'll try a URL not in S3 as well and see if that works. Thanks for the ideas.

brechmos commented 9 years ago

Well... interesting. I just tried a static different way of doing it and maybe I am testing it incorrectly. I tried:

{% load daguerre %}
<img src="{% adjust 'http://www.wired.com/wp-content/uploads/2014/07/brain1.jpg' 'crop' width=200 %}" >

and it gave me the same error with the missing slash:

SuspiciousOperation: Attempted access to 'http:/www.wired.com/wp-content/uploads/2014/07/brain1.jpg' denied.

melinath commented 9 years ago

Yeah, you're testing incorrectly. Daguerre does not currently support arbitrary URLs on the internet. Try passing in the storage path of the image or the image file field.

For example, given the following model:

class MyModel(models.Model):
    image = models.ImageField()

All you need to do is:

{% load daguerre %}
<img src="{% adjust mymodelinstance.image 'crop' width=200 %}" >

This will work with any storage.

harrislapiroff commented 9 years ago

If you're really interested in resizing an image from a URL using Daguerre currently, you can get the view to handle the lifting for that. For instance, you could write a view like this:

import urllib2
from django.shortcuts import render_to_response
from django.core.files.storage import get_storage_class

def test_view(request):
    storage_class = get_storage_class()
    storage = storage_class()
    image = storage.save("image.jpg", urllib2.urlopen("http://www.wired.com/wp-content/uploads/2014/07/brain1.jpg"))
    return render_to_response("template.html", {'image': image})

And then in template.html:

{% load daguerre %}
<img src="{% adjust image 'crop' width=200 height=200 %}" />

This is just an illustration of how you could accomplish what you're trying to do with Daguerre. This technique isn't exactly advisable since it would result in resaving and adjusting the image anew every time you request it. It's probably ideal to keep track of images by associating them with models as @melinath advised above. Alternatively you could hash the URL into a file name and check for the existence of that file before downloading the image again.

I'm going to close this ticket, since it's not a bug in Daguerre, but I'll open a new one where we can discuss whether we want the {% adjust %} tag to handle arbitrary URLs.