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

gdrive: remove empty file hack #64

Merged
merged 1 commit into from Oct 22, 2020
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
47 changes: 15 additions & 32 deletions pydrive2/files.py
@@ -1,4 +1,3 @@
import os
import io
import mimetypes
import json
Expand Down Expand Up @@ -262,9 +261,7 @@ def SetContentString(self, content, encoding="utf-8"):
:param content: content of the file in string.
:type content: str
"""
if content:
self.content = io.BytesIO(content.encode(encoding))

self.content = io.BytesIO(content.encode(encoding))
if self.get("mimeType") is None:
self["mimeType"] = "text/plain"

Expand All @@ -278,9 +275,7 @@ def SetContentFile(self, filename):
:param filename: name of the file to be uploaded.
:type filename: str.
"""
if os.path.getsize(filename):
self.content = open(filename, "rb")

self.content = open(filename, "rb")
if self.get("title") is None:
self["title"] = filename
if self.get("mimeType") is None:
Expand Down Expand Up @@ -358,32 +353,20 @@ def download(fd, request):
download(fd, files.get_media(fileId=file_id))
except errors.HttpError as error:
exc = ApiRequestError(error)
code = exc.error["code"]
reason = exc.GetField("reason")
if code == 403 and reason == "fileNotDownloadable":
mimetype = mimetype or "text/plain"
fd.seek(0) # just in case `download()` modified `fd`
try:
download(
fd,
files.export_media(
fileId=file_id, mimeType=mimetype
),
)
except errors.HttpError as error:
raise ApiRequestError(error)
elif code == 416 and reason == "requestedRangeNotSatisfiable":
# NOTE: An empty file case. Wasting one API call to make
# absolutely sure. See
# https://github.com/iterative/dvc/issues/4507
try:
self.FetchMetadata(fields="fileSize")
if int(self["fileSize"]) != 0:
raise exc
except errors.HttpError:
raise exc
else:
if (
exc.error["code"] != 403
or exc.GetField("reason") != "fileNotDownloadable"
):
raise exc
mimetype = mimetype or "text/plain"
fd.seek(0) # just in case `download()` modified `fd`
try:
download(
fd,
files.export_media(fileId=file_id, mimeType=mimetype),
)
except errors.HttpError as error:
raise ApiRequestError(error)

if mimetype == "text/plain" and remove_bom:
fd.seek(0)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -26,7 +26,7 @@
description="Google Drive API made easy. Maintained fork of PyDrive.",
long_description=open("README.rst").read(),
install_requires=[
"google-api-python-client >= 1.12.1",
"google-api-python-client >= 1.12.5",
"six >= 1.13.0",
"oauth2client >= 4.0.0",
"PyYAML >= 3.0",
Expand Down