Skip to content

Commit f0dd362

Browse files
authored
Merge pull request #64 from iterative/fix-59
gdrive: remove empty file hack
2 parents 4d35d86 + 5871dd9 commit f0dd362

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
lines changed

pydrive2/files.py

+15-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import io
32
import mimetypes
43
import json
@@ -262,9 +261,7 @@ def SetContentString(self, content, encoding="utf-8"):
262261
:param content: content of the file in string.
263262
:type content: str
264263
"""
265-
if content:
266-
self.content = io.BytesIO(content.encode(encoding))
267-
264+
self.content = io.BytesIO(content.encode(encoding))
268265
if self.get("mimeType") is None:
269266
self["mimeType"] = "text/plain"
270267

@@ -278,9 +275,7 @@ def SetContentFile(self, filename):
278275
:param filename: name of the file to be uploaded.
279276
:type filename: str.
280277
"""
281-
if os.path.getsize(filename):
282-
self.content = open(filename, "rb")
283-
278+
self.content = open(filename, "rb")
284279
if self.get("title") is None:
285280
self["title"] = filename
286281
if self.get("mimeType") is None:
@@ -358,32 +353,20 @@ def download(fd, request):
358353
download(fd, files.get_media(fileId=file_id))
359354
except errors.HttpError as error:
360355
exc = ApiRequestError(error)
361-
code = exc.error["code"]
362-
reason = exc.GetField("reason")
363-
if code == 403 and reason == "fileNotDownloadable":
364-
mimetype = mimetype or "text/plain"
365-
fd.seek(0) # just in case `download()` modified `fd`
366-
try:
367-
download(
368-
fd,
369-
files.export_media(
370-
fileId=file_id, mimeType=mimetype
371-
),
372-
)
373-
except errors.HttpError as error:
374-
raise ApiRequestError(error)
375-
elif code == 416 and reason == "requestedRangeNotSatisfiable":
376-
# NOTE: An empty file case. Wasting one API call to make
377-
# absolutely sure. See
378-
# https://github.com/iterative/dvc/issues/4507
379-
try:
380-
self.FetchMetadata(fields="fileSize")
381-
if int(self["fileSize"]) != 0:
382-
raise exc
383-
except errors.HttpError:
384-
raise exc
385-
else:
356+
if (
357+
exc.error["code"] != 403
358+
or exc.GetField("reason") != "fileNotDownloadable"
359+
):
386360
raise exc
361+
mimetype = mimetype or "text/plain"
362+
fd.seek(0) # just in case `download()` modified `fd`
363+
try:
364+
download(
365+
fd,
366+
files.export_media(fileId=file_id, mimeType=mimetype),
367+
)
368+
except errors.HttpError as error:
369+
raise ApiRequestError(error)
387370

388371
if mimetype == "text/plain" and remove_bom:
389372
fd.seek(0)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
description="Google Drive API made easy. Maintained fork of PyDrive.",
2727
long_description=open("README.rst").read(),
2828
install_requires=[
29-
"google-api-python-client >= 1.12.1",
29+
"google-api-python-client >= 1.12.5",
3030
"six >= 1.13.0",
3131
"oauth2client >= 4.0.0",
3232
"PyYAML >= 3.0",

0 commit comments

Comments
 (0)