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

fix: MediaFileUpload error if file does not exist #1127

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions googleapiclient/http.py
Expand Up @@ -573,20 +573,22 @@ def __init__(
resumable: bool, True if this is a resumable upload. False means upload
in a single request.
"""
self._fd = None
self._filename = filename
fd = open(self._filename, "rb")
self._fd = open(self._filename, "rb")
if mimetype is None:
# 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
self._fd, mimetype, chunksize=chunksize, resumable=resumable
)

def __del__(self):
self._fd.close()
if self._fd:
self._fd.close()

def to_json(self):
"""Creating a JSON representation of an instance of MediaFileUpload.
Expand Down
4 changes: 4 additions & 0 deletions tests/test_http.py
Expand Up @@ -248,6 +248,10 @@ def test_media_file_upload_to_from_json(self):
self.assertEqual(500, new_upload.chunksize())
self.assertEqual(b"PNG", new_upload.getbytes(1, 3))

def test_media_file_upload_raises_on_file_not_found(self):
with self.assertRaises(FileNotFoundError):
MediaFileUpload(datafile("missing.png"))

def test_media_file_upload_raises_on_invalid_chunksize(self):
self.assertRaises(
InvalidChunkSizeError,
Expand Down