Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc13631

Browse files
committed
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.
1 parent dbabf82 commit dc13631

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

googleapiclient/http.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,11 @@ def __init__(self, filename, mimetype=None, chunksize=DEFAULT_CHUNK_SIZE,
425425
self._filename = filename
426426
fd = open(self._filename, 'rb')
427427
if mimetype is None:
428-
(mimetype, encoding) = mimetypes.guess_type(filename)
428+
# No mimetype provided, make a guess.
429+
mimetype, _ = mimetypes.guess_type(filename)
430+
if mimetype is None:
431+
# Guess failed, use octet-stream.
432+
mimetype = 'application/octet-stream'
429433
super(MediaFileUpload, self).__init__(fd, mimetype, chunksize=chunksize,
430434
resumable=resumable)
431435

tests/data/empty

Whitespace-only changes.

tests/test_http.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ def test_set_user_agent_nested(self):
131131

132132
class TestMediaUpload(unittest.TestCase):
133133

134+
def test_media_file_upload_mimetype_detection(self):
135+
upload = MediaFileUpload(datafile('small.png'))
136+
self.assertEqual('image/png', upload.mimetype())
137+
138+
upload = MediaFileUpload(datafile('empty'))
139+
self.assertEqual('application/octet-stream', upload.mimetype())
140+
134141
def test_media_file_upload_to_from_json(self):
135142
upload = MediaFileUpload(
136143
datafile('small.png'), chunksize=500, resumable=True)

0 commit comments

Comments
 (0)