echamberlain avatar echamberlain committed 3602d34

S3 Content-Type fix.

Boto overwrites the Header Content-Type setting with its own guess. Changed _save to add the Content-Type to the metadata.
The metadata is added to the Header after boto overwrites the Content-Type in the Header.

In the case of uploaded files, the file's content_type is ignored and instead mimetypes.guess_type is used instead.
This fix checks for content.content_type and if present, uses it instead of mimetypes.guess_type.

We needed this fix to properly set the Content-Type when uploading caf files without an extension.

Comments (0)

Files changed (1)

storages/backends/s3boto.py

         cleaned_name = self._clean_name(name)
         name = self._normalize_name(cleaned_name)
         headers = self.headers
-        content_type = mimetypes.guess_type(name)[0] or Key.DefaultContentType            
+        content_type = getattr(content,'content_type', mimetypes.guess_type(name)[0] or Key.DefaultContentType)
 
         if self.gzip and content_type in self.gzip_content_types:
             content = self._compress_content(content)
             headers.update({'Content-Encoding': 'gzip'})
 
-        headers.update({
-            'Content-Type': content_type,
-        })
-        
         content.name = cleaned_name
         k = self.bucket.get_key(name)
         if not k:
             k = self.bucket.new_key(name)
+
+        k.set_metadata('Content-Type',content_type)
         k.set_contents_from_file(content, headers=headers, policy=self.acl, 
                                  reduced_redundancy=self.reduced_redundancy)
         return cleaned_name
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.