Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thumbnails have MIME type application/octet-stream on CloudFiles #7

Closed
tuttle opened this issue Mar 8, 2010 · 7 comments
Closed

Thumbnails have MIME type application/octet-stream on CloudFiles #7

tuttle opened this issue Mar 8, 2010 · 7 comments

Comments

@tuttle
Copy link

tuttle commented Mar 8, 2010

Browsers can have problems with that.

Using latest django-storages from http://bitbucket.org/smileychris/django-storages/, latest python-cloudfiles from git://github.com/rackspace/python-cloudfiles.git, latest easy-thumbnails from git://github.com/SmileyChris/easy-thumbnails.git. As of 2010-03-08.

My proposed solution:

--- a/easy_thumbnails/files.py
+++ b/easy_thumbnails/files.py
@@ -1,23 +1,24 @@
 from PIL import Image
-from django.core.files.base import File, ContentFile
+from django.core.files.base import File
+from django.core.files.uploadedfile import SimpleUploadedFile
 from django.core.files.storage import get_storage_class, default_storage
 from django.db.models.fields.files import ImageFieldFile, FieldFile
 from django.utils.html import escape
 from django.utils.safestring import mark_safe
 from easy_thumbnails import engine, models, utils
 import datetime
 import os
-
+import mimetypes
 
 DEFAULT_THUMBNAIL_STORAGE = get_storage_class(
                                         utils.get_setting('DEFAULT_STORAGE'))()
 
 
 def get_thumbnailer(source, relative_name=None):
     """
     Get a thumbnailer for a source file.
     
     """
     if isinstance(source, Thumbnailer):
         return source
     elif isinstance(source, FieldFile):
@@ -181,27 +182,29 @@ class Thumbnailer(File):
     def generate_thumbnail(self, thumbnail_options):
         """
         Return a ``ThumbnailFile`` containing a thumbnail image.
         
         The thumbnail image is generated using the ``thumbnail_options``
         dictionary.
         
         """
         thumbnail_image = engine.process_image(self.image, thumbnail_options)
         quality = thumbnail_options.get('quality', self.thumbnail_quality)
         data = engine.save_image(thumbnail_image, quality=quality).read()
 
         filename = self.get_thumbnail_name(thumbnail_options)
-        thumbnail = ThumbnailFile(filename, ContentFile(data),
+        content_type = mimetypes.guess_type(filename)[0]
+        file = SimpleUploadedFile(filename, data, content_type)
+        thumbnail = ThumbnailFile(filename, file,
                                   storage=self.thumbnail_storage)
         thumbnail.image = thumbnail_image
@SmileyChris
Copy link
Owner

My storages changes have been pulled back into http://bitbucket.org/david/django-storages by the way

@SmileyChris
Copy link
Owner

I'm a bit confused. The filename is guessed by python-cloudfiles if the file doesn't have a content_type attribute (http://github.com/rackspace/python-cloudfiles/blob/master/cloudfiles/storage_object.py#L297) so isn't your patch really just duplicating this same logic?

@tuttle
Copy link
Author

tuttle commented Mar 9, 2010

Ok, I switched back to David's repo. How about to mark the info about the back pull and that your repo is in fact abandoned into its description?

TBH, I'm not sure whether my patch is duplicating. Just proposing one way of ensuring the resulting thumbnails have proper MIME type on CloudFiles. The important part in my patch is the computed content_type variable.

Sorry if I'm missing something. If I can be of any help with testing, just ping me.

@michelts
Copy link

Hi Chris,

The mimetype isn't guessed by cloudfiles, there is 2 methods to save the object: the write method, that guess the mimetpe as you noticed and the send method that just rely on the content type passed as argument.

I didn't understand exactly why, but the django storages make use of the send method. This forces us to set the mimetype before send the file.

This way, the patch isn't duplicating but anyway, I think the error is in rackspace cloudfiles. I think the send method should be able to guess the mimetype just like the write method.

@SmileyChris
Copy link
Owner

Thanks for your input michaelts by the way (I did read your comment but just never replied to it).

It really does sound like this is something that could be fixed in storages easily enough -- has anyone dropped a report with David?

I'd apply this ticket anyway, but using SimpleUploadedFile seems a bit of a warty workaround and I need to think more about any issues with doing it this way.

@michelts
Copy link

michelts commented Feb 7, 2011

Hi Chris,

I'm not using David's repo anymore. The author of the module is Richard Leland and he is keeping a new lib, the django-cumulus. It is the same thing but on a different place. I'm using it and we did some bug fixes and improvements that are not on the storages.

Anyway, django-cumulus is already taking care of the content-type issue (except for a little bug):

https://github.com/richleland/django-cumulus/issues/closed#issue/4

Now it would be redundant :)

@SmileyChris
Copy link
Owner

I'm going to close this issue, since it's not really a problem with easy-thumbnails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants