Browse files

Merge remote-tracking branch 'ksperling/master' into ksperling-merge

Conflicts:
	S3/S3.py
Minor conflict with econnell's "Upload file from stdin" patch, resolved manually.
  • Loading branch information...
2 parents 0451a17 + a845413 commit 3a41f477400a505f09f14f4db01e2fadfe68dcb9 @mdomsch committed Jul 14, 2012
Showing with 94 additions and 8 deletions.
  1. +29 −7 S3/S3.py
  2. +63 −0 magic
  3. +2 −1 s3cmd
View
36 S3/S3.py
@@ -29,18 +29,37 @@
from S3Uri import S3Uri
try:
- import magic
+ import magic, gzip
try:
## https://github.com/ahupp/python-magic
magic_ = magic.Magic(mime=True)
- def mime_magic(file):
+ def mime_magic_file(file):
return magic_.from_file(file)
- except (TypeError, AttributeError):
+ def mime_magic_buffer(buffer):
+ return magic_.from_buffer(buffer)
+ except TypeError:
+ ## http://pypi.python.org/pypi/filemagic
+ magic_ = magic.Magic(flags=magic.MAGIC_MIME)
+ def mime_magic_file(file):
+ return magic_.id_filename(file)
+ def mime_magic_buffer(buffer):
+ return magic_.id_buffer(buffer)
+ except AttributeError:
## Older python-magic versions
magic_ = magic.open(magic.MAGIC_MIME)
magic_.load()
- def mime_magic(file):
+ def mime_magic_file(file):
return magic_.file(file)
+ def mime_magic_buffer(buffer):
+ return magic_.buffer(buffer)
+
+ def mime_magic(file):
+ type = mime_magic_file(file)
+ if type != "application/x-gzip; charset=binary":
+ return (type, None)
+ else:
+ return (mime_magic_buffer(gzip.open(file).read(8192)), 'gzip')
+
except ImportError, e:
if str(e).find("magic") >= 0:
magic_message = "Module python-magic is not available."
@@ -53,7 +72,7 @@ def mime_magic(file):
if (not magic_warned):
warning(magic_message)
magic_warned = True
- return mimetypes.guess_type(file)[0]
+ return mimetypes.guess_type(file)
__all__ = []
class S3Request(object):
@@ -366,12 +385,15 @@ def object_put(self, filename, uri, extra_headers = None, extra_label = ""):
## MIME-type handling
content_type = self.config.mime_type
+ content_encoding = None
if filename != "-" and not content_type and self.config.guess_mime_type:
- content_type = mime_magic(filename)
+ (content_type, content_encoding) = mime_magic(filename)
if not content_type:
content_type = self.config.default_mime_type
- debug("Content-Type set to '%s'" % content_type)
+ debug("Content-Type set to '%s' (encoding %s)" % (content_type, content_encoding))
headers["content-type"] = content_type
+ if content_encoding is not None:
+ headers["content-encoding"] = content_encoding
## Other Amazon S3 attributes
if self.config.acl_public:
View
63 magic
@@ -0,0 +1,63 @@
+# Additional magic for common web file types
+
+0 string/b {\ " JSON data
+!:mime application/json
+0 string/b {\ } JSON data
+!:mime application/json
+0 string/b [ JSON data
+!:mime application/json
+
+0 search/4000 function
+>&0 search/32/b )\ { JavaScript program
+!:mime application/javascript
+
+0 search/4000 @media CSS stylesheet
+!:mime text/css
+0 search/4000 @import CSS stylesheet
+!:mime text/css
+0 search/4000 @namespace CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ background CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ border CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ bottom CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ color CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ cursor CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ direction CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ display CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ float CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ font CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ height CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ left CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ line- CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ margin CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ padding CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ position CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ right CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ text- CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ top CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ width CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ visibility CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ -moz- CSS stylesheet
+!:mime text/css
+0 search/4000/b {\ -webkit- CSS stylesheet
+!:mime text/css
View
3 s3cmd
@@ -32,6 +32,7 @@ from distutils.spawn import find_executable
def output(message):
sys.stdout.write(message + "\n")
+ sys.stdout.flush()
def check_args_type(args, type, verbose_type):
for arg in args:
@@ -1662,7 +1663,7 @@ def main():
optparser.add_option( "--no-access-logging", dest="log_target_prefix", action="store_false", help="Disable access logging (for [cfmodify] and [accesslog] commands)")
optparser.add_option( "--default-mime-type", dest="default_mime_type", action="store_true", help="Default MIME-type for stored objects. Application default is binary/octet-stream.")
- optparser.add_option( "--guess-mime-type", dest="guess_mime_type", action="store_true", help="Guess MIME-type of files by their extension or mime magic. Fall back to default MIME-Type as specified by --default-mime-type option")
+ optparser.add_option("-M", "--guess-mime-type", dest="guess_mime_type", action="store_true", help="Guess MIME-type of files by their extension or mime magic. Fall back to default MIME-Type as specified by --default-mime-type option")
optparser.add_option( "--no-guess-mime-type", dest="guess_mime_type", action="store_false", help="Don't guess MIME-type and use the default type instead.")
optparser.add_option("-m", "--mime-type", dest="mime_type", type="mimetype", metavar="MIME/TYPE", help="Force MIME-type. Override both --default-mime-type and --guess-mime-type.")

0 comments on commit 3a41f47

Please sign in to comment.