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

Always attempt to set the content-type #406

Merged
merged 1 commit into from
Dec 8, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions S3/S3.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,26 @@ def _expiration_set(self, uri):
request = self.create_request("BUCKET_CREATE", bucket = bucket, headers = headers, extra="?lifecycle")
return (request, body)

def content_type(self, filename):
content_type = self.config.mime_type
content_charset = None

if filename != "-" and not content_type and self.config.guess_mime_type:
if self.config.use_mime_magic:
(content_type, content_charset) = mime_magic(filename)
else:
(content_type, content_charset) = mimetypes.guess_type(filename)
if not content_type:
content_type = self.config.default_mime_type
if not content_charset:
content_charset = self.config.encoding.upper()

## add charset to content type
if self.add_encoding(filename, content_type) and content_charset is not None:
content_type = content_type + "; charset=" + content_charset

return content_type

def add_encoding(self, filename, content_type):
if content_type.find("charset=") != -1:
return False
Expand Down Expand Up @@ -480,23 +500,7 @@ def object_put(self, filename, uri, extra_headers = None, extra_label = ""):
headers["x-amz-server-side-encryption"] = "AES256"

## MIME-type handling
content_type = self.config.mime_type
content_charset = None
if filename != "-" and not content_type and self.config.guess_mime_type:
if self.config.use_mime_magic:
(content_type, content_charset) = mime_magic(filename)
else:
(content_type, content_charset) = mimetypes.guess_type(filename)
if not content_type:
content_type = self.config.default_mime_type
if not content_charset:
content_charset = self.config.encoding.upper()

## add charset to content type
if self.add_encoding(filename, content_type) and content_charset is not None:
content_type = content_type + "; charset=" + content_charset

headers["content-type"] = content_type
headers["content-type"] = self.content_type(filename)

## Other Amazon S3 attributes
if self.config.acl_public:
Expand Down Expand Up @@ -624,6 +628,10 @@ def object_copy(self, src_uri, dst_uri, extra_headers = None):
if extra_headers:
headers['x-amz-metadata-directive'] = "REPLACE"
headers.update(extra_headers)

filename = os.path.basename(str(src_uri))
headers["content-type"] = self.content_type(filename)

request = self.create_request("OBJECT_PUT", uri = dst_uri, headers = headers)
response = self.send_request(request)
return response
Expand Down