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

Detect Content-Type when content_type property is null #407

Merged
merged 3 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions storages/backends/s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ def _save(self, name, content):
name = self._normalize_name(cleaned_name)
headers = self.headers.copy()
_type, encoding = mimetypes.guess_type(name)
content_type = getattr(content, 'content_type',
_type or self.key_class.DefaultContentType)
content_type = getattr(content, 'content_type', None)
content_type = content_type or _type or self.key_class.DefaultContentType

# setting the content_type in the key object is not enough.
headers.update({'Content-Type': content_type})
Expand Down
4 changes: 2 additions & 2 deletions storages/backends/s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ def _save(self, name, content):
name = self._normalize_name(cleaned_name)
parameters = self.object_parameters.copy()
_type, encoding = mimetypes.guess_type(name)
content_type = getattr(content, 'content_type',
_type or self.default_content_type)
content_type = getattr(content, 'content_type', None)
content_type = content_type or _type or self.default_content_type

# setting the content_type in the key object is not enough.
parameters.update({'ContentType': content_type})
Expand Down
20 changes: 20 additions & 0 deletions tests/test_s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ def test_storage_save(self):
rewind=True
)

def test_content_type(self):
"""
Test saving a file with a None content type.
"""
name = 'test_image.jpg'
content = ContentFile('data')
content.content_type = None
self.storage.save(name, content)
self.storage.bucket.get_key.assert_called_once_with(name)

key = self.storage.bucket.get_key.return_value
key.set_metadata.assert_called_with('Content-Type', 'image/jpeg')
key.set_contents_from_file.assert_called_with(
content,
headers={'Content-Type': 'image/jpeg'},
policy=self.storage.default_acl,
reduced_redundancy=self.storage.reduced_redundancy,
rewind=True
)

def test_storage_save_gzipped(self):
"""
Test saving a gzipped file
Expand Down
19 changes: 19 additions & 0 deletions tests/test_s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ def test_storage_save(self):
}
)

def test_content_type(self):
"""
Test saving a file with a None content type.
"""
name = 'test_image.jpg'
content = ContentFile('data')
content.content_type = None
self.storage.save(name, content)
self.storage.bucket.Object.assert_called_once_with(name)

obj = self.storage.bucket.Object.return_value
obj.upload_fileobj.assert_called_with(
content.file,
ExtraArgs={
'ContentType': 'image/jpeg',
'ACL': self.storage.default_acl,
}
)

def test_storage_save_gzipped(self):
"""
Test saving a gzipped file
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ deps =
django110: Django>=1.10, <1.11
django111: Django>=1.11, <2.0
py27: mock
# enum is needed by paramiko, but wasn't added to py3 until py34
py33: enum34
boto3>=1.2.3
boto>=2.32.0
dropbox>=8.0.0
Expand Down