diff options
author | Nam T. Nguyen <namnguyen@google.com> | 2015-12-01 10:18:56 -0800 |
---|---|---|
committer | Nam T. Nguyen <namnguyen@google.com> | 2015-12-01 10:18:56 -0800 |
commit | dc13631485efcb8b9f533b9a55d4f98c50a3d37b (patch) | |
tree | 382a68c6a63bd19614b4d8d38632ed65b7850280 | |
parent | dbabf825f6a5224a4bdcebd2390b21f7797f651d (diff) | |
download | google-api-python-client-dc13631485efcb8b9f533b9a55d4f98c50a3d37b.tar.gz |
Default to octet-stream if mimetype detection fails.
The function mimetypes.guess_type returns (None, None) if it fails to
find a matching extension. In that case, we should just treat the media
as application/octet-stream.
-rw-r--r-- | googleapiclient/http.py | 6 | ||||
-rw-r--r-- | tests/data/empty | 0 | ||||
-rw-r--r-- | tests/test_http.py | 7 |
3 files changed, 12 insertions, 1 deletions
diff --git a/googleapiclient/http.py b/googleapiclient/http.py index 41daedcdf..10559c5e5 100644 --- a/googleapiclient/http.py +++ b/googleapiclient/http.py @@ -425,7 +425,11 @@ class MediaFileUpload(MediaIoBaseUpload): self._filename = filename fd = open(self._filename, 'rb') if mimetype is None: - (mimetype, encoding) = mimetypes.guess_type(filename) + # No mimetype provided, make a guess. + mimetype, _ = mimetypes.guess_type(filename) + if mimetype is None: + # Guess failed, use octet-stream. + mimetype = 'application/octet-stream' super(MediaFileUpload, self).__init__(fd, mimetype, chunksize=chunksize, resumable=resumable) diff --git a/tests/data/empty b/tests/data/empty new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/data/empty diff --git a/tests/test_http.py b/tests/test_http.py index 82369fcc4..6fb830556 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -131,6 +131,13 @@ class TestUserAgent(unittest.TestCase): class TestMediaUpload(unittest.TestCase): + def test_media_file_upload_mimetype_detection(self): + upload = MediaFileUpload(datafile('small.png')) + self.assertEqual('image/png', upload.mimetype()) + + upload = MediaFileUpload(datafile('empty')) + self.assertEqual('application/octet-stream', upload.mimetype()) + def test_media_file_upload_to_from_json(self): upload = MediaFileUpload( datafile('small.png'), chunksize=500, resumable=True) |