Browse code

Add support for setting Content-Encoding header where it can be guessed, in particular Content-Encoding: gzip is handled.

Karsten Sperling authored on 2012/04/04 09:26:46
Showing 1 changed files
... ...
@@ -29,24 +29,37 @@ from MultiPart import MultiPartUpload
29 29
 from S3Uri import S3Uri
30 30
 
31 31
 try:
32
-    import magic
32
+    import magic, gzip
33 33
     try:
34 34
         ## https://github.com/ahupp/python-magic
35 35
         magic_ = magic.Magic(mime=True)
36
-        def mime_magic(file):
36
+        def mime_magic_file(file):
37 37
             return magic_.from_file(file)
38
+        def mime_magic_buffer(buffer):
39
+            return magic_.from_buffer(buffer)
38 40
     except (TypeError, AttributeError):
39 41
         ## Older python-magic versions
40 42
         magic_ = magic.open(magic.MAGIC_MIME)
41 43
         magic_.load()
42
-        def mime_magic(file):
44
+        def mime_magic_file(file):
43 45
             return magic_.file(file)
46
+        def mime_magic_buffer(buffer):
47
+            return magic_.buffer(buffer)
44 48
 except ImportError, e:
45 49
     if str(e).find("magic") >= 0:
46 50
         magic_message = "Module python-magic is not available."
47 51
     else:
48 52
         magic_message = "Module python-magic can't be used (%s)." % e.message
49 53
     magic_message += " Guessing MIME types based on file extensions."
54
+    
55
+    def mime_magic(file):
56
+        type = mime_magic_file(file)
57
+        if type != "application/x-gzip; charset=binary":
58
+            return (type, None)
59
+        else:
60
+            return (mime_magic_buffer(gzip.open(file).read(8192)), 'gzip')
61
+            
62
+except ImportError:
50 63
     magic_warned = False
51 64
     def mime_magic(file):
52 65
         global magic_warned
... ...
@@ -359,12 +372,15 @@ class S3(object):
359 359
 
360 360
         ## MIME-type handling
361 361
         content_type = self.config.mime_type
362
+        content_encoding = None
362 363
         if not content_type and self.config.guess_mime_type:
363
-            content_type = mime_magic(filename)
364
+            (content_type, content_encoding) = mime_magic(filename)
364 365
         if not content_type:
365 366
             content_type = self.config.default_mime_type
366
-        debug("Content-Type set to '%s'" % content_type)
367
+        debug("Content-Type set to '%s' (encoding %s)" % (content_type, content_encoding))
367 368
         headers["content-type"] = content_type
369
+        if content_encoding is not None:
370
+            headers["content-encoding"] = content_encoding
368 371
 
369 372
         ## Other Amazon S3 attributes
370 373
         if self.config.acl_public: