Skip to content

Commit

Permalink
Merge pull request #320 from Sgiath/303-gcloud-mime-type
Browse files Browse the repository at this point in the history
[gcloud] Add support for MIME type
  • Loading branch information
scjody authored Jul 2, 2017
2 parents b5027d2 + d96107d commit a92e7a6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ setuptools*
__pycache__
.coverage
.cache
.idea

.idea/
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ By order of apparition, thanks:
* Eirik Martiniussen Sylliaas (Google Cloud Storage native support)
* Jody McIntyre (Google Cloud Storage native support)
* Stanislav Kaledin (Bug fixes in SFTPStorage)
* Filip Vavera (Google Cloud MIME types support)

Extra thanks to Marty for adding this in Django,
you can buy his very interesting book (Pro Django).
7 changes: 5 additions & 2 deletions storages/backends/gcloud.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mimetypes
from tempfile import SpooledTemporaryFile

from django.core.exceptions import ImproperlyConfigured
Expand All @@ -21,6 +22,7 @@
class GoogleCloudFile(File):
def __init__(self, name, mode, storage):
self.name = name
self.mime_type = mimetypes.guess_type(name)[0]
self._mode = mode
self._storage = storage
self.blob = storage.bucket.get_blob(name)
Expand Down Expand Up @@ -70,7 +72,7 @@ def close(self):
if self._file is not None:
if self._is_dirty:
self.file.seek(0)
self.blob.upload_from_file(self.file)
self.blob.upload_from_file(self.file, content_type=self.mime_type)
self._file.close()
self._file = None

Expand Down Expand Up @@ -154,7 +156,8 @@ def _save(self, name, content):
content.name = cleaned_name
encoded_name = self._encode_name(name)
file = GoogleCloudFile(encoded_name, 'rw', self)
file.blob.upload_from_file(content, size=content.size)
file.blob.upload_from_file(content, size=content.size,
content_type=file.mime_type)
return cleaned_name

def delete(self, name):
Expand Down
8 changes: 5 additions & 3 deletions tests/test_gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mock

import datetime
import mimetypes

from django.core.files.base import ContentFile
from django.test import TestCase
Expand Down Expand Up @@ -90,7 +91,8 @@ def test_open_write(self, MockBlob):
# File data is not actually written until close(), so do that.
f.close()

MockBlob().upload_from_file.assert_called_with(tmpfile)
MockBlob().upload_from_file.assert_called_with(
tmpfile, content_type=mimetypes.guess_type(self.filename)[0])

def test_save(self):
data = 'This is some test content.'
Expand All @@ -100,7 +102,7 @@ def test_save(self):

self.storage._client.get_bucket.assert_called_with(self.bucket_name)
self.storage._bucket.get_blob().upload_from_file.assert_called_with(
content, size=len(data))
content, size=len(data), content_type=mimetypes.guess_type(self.filename)[0])

def test_save2(self):
data = 'This is some test ủⓝï℅ⅆℇ content.'
Expand All @@ -111,7 +113,7 @@ def test_save2(self):

self.storage._client.get_bucket.assert_called_with(self.bucket_name)
self.storage._bucket.get_blob().upload_from_file.assert_called_with(
content, size=len(data))
content, size=len(data), content_type=mimetypes.guess_type(filename)[0])

def test_delete(self):
self.storage.delete(self.filename)
Expand Down

0 comments on commit a92e7a6

Please sign in to comment.