Browse code

Merge remote-tracking branch 'ejnoyes/master' into test-merge

Conflicts:
S3/S3.py

Fixed as two previous patches from ksperling (mime-type guessing) and
econnell (read from stdin) had touched exactly this same chunk.

Matt Domsch authored on 2012/07/31 13:04:24
Showing 3 changed files
... ...
@@ -40,6 +40,7 @@ class Config(object):
40 40
     proxy_port = 3128
41 41
     encrypt = False
42 42
     dry_run = False
43
+    add_encoding_exts = ""
43 44
     preserve_attrs = True
44 45
     preserve_attrs_list = [
45 46
         'uname',    # Verbose owner Name (e.g. 'root')
... ...
@@ -371,6 +371,21 @@ class S3(object):
371 371
 
372 372
         return response
373 373
 
374
+    def add_encoding(self, filename, content_type):
375
+        if content_type.find("charset=") != -1:
376
+           return False
377
+        exts = self.config.add_encoding_exts.split(',')
378
+        if exts[0]=='':
379
+            return False
380
+        parts = filename.rsplit('.',2)
381
+        if len(parts) < 2:
382
+            return False
383
+        ext = parts[1]
384
+        if ext in exts:
385
+            return True
386
+        else:
387
+            return False
388
+           
374 389
     def object_put(self, filename, uri, extra_headers = None, extra_label = ""):
375 390
         # TODO TODO
376 391
         # Make it consistent with stream-oriented object_get()
... ...
@@ -395,12 +410,17 @@ class S3(object):
395 395
 
396 396
         ## MIME-type handling
397 397
         content_type = self.config.mime_type
398
-        content_encoding = None
399 398
         if filename != "-" and not content_type and self.config.guess_mime_type:
400 399
             (content_type, content_encoding) = mime_magic(filename)
401 400
         if not content_type:
402 401
             content_type = self.config.default_mime_type
403
-        debug("Content-Type set to '%s' (encoding %s)" % (content_type, content_encoding))
402
+        if not content_encoding:
403
+            content_encoding = self.config.encoding.upper()
404
+    
405
+        ## add charset to content type
406
+        if self.add_encoding(filename, content_type) and content_encoding is not None:
407
+            content_type = content_type + "; charset=" + content_encoding
408
+       
404 409
         headers["content-type"] = content_type
405 410
         if content_encoding is not None:
406 411
             headers["content-encoding"] = content_encoding
... ...
@@ -1709,6 +1709,7 @@ def main():
1709 1709
     optparser.add_option(      "--add-header", dest="add_header", action="append", metavar="NAME:VALUE", help="Add a given HTTP header to the upload request. Can be used multiple times. For instance set 'Expires' or 'Cache-Control' headers (or both) using this options if you like.")
1710 1710
 
1711 1711
     optparser.add_option(      "--encoding", dest="encoding", metavar="ENCODING", help="Override autodetected terminal and filesystem encoding (character set). Autodetected: %s" % preferred_encoding)
1712
+    optparser.add_option(      "--add-encoding-exts", dest="add_encoding_exts", metavar="EXTENSIONs", help="Add encoding to these comma delimited extensions i.e. (css,js,html) when uploading to S3 )")
1712 1713
     optparser.add_option(      "--verbatim", dest="urlencoding_mode", action="store_const", const="verbatim", help="Use the S3 name as given on the command line. No pre-processing, encoding, etc. Use with caution!")
1713 1714
 
1714 1715
     optparser.add_option(      "--disable-multipart", dest="enable_multipart", action="store_false", help="Disable multipart upload on files bigger than --multipart-chunk-size-mb")