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.
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
index 41daedc..10559c5 100644
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -425,7 +425,11 @@
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 0000000..e69de29
--- /dev/null
+++ b/tests/data/empty
diff --git a/tests/test_http.py b/tests/test_http.py
index 82369fc..6fb8305 100644
--- a/tests/test_http.py
+++ b/tests/test_http.py
@@ -131,6 +131,13 @@
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)