Skip to content

Set content picks default title/mime if file is created for the first time #276

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

Merged
merged 3 commits into from
Apr 11, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions pydrive2/files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import os
import mimetypes
import json

Expand Down Expand Up @@ -235,30 +236,35 @@ def SetContentString(self, content, encoding="utf-8"):
"""Set content of this file to be a string.

Creates io.BytesIO instance of utf-8 encoded string.
Sets mimeType to be 'text/plain' if not specified.
Sets mimeType to be 'text/plain' if not specified and file id is not
set (means that we are uploading this file for the first time).

:param encoding: The encoding to use when setting the content of this file.
:type encoding: str
:param content: content of the file in string.
:type content: str
"""
self.content = io.BytesIO(content.encode(encoding))
if self.get("mimeType") is None:
if self.get("mimeType") is None and self.get("id") is None:
self["mimeType"] = "text/plain"

def SetContentFile(self, filename):
"""Set content of this file from a file.

Opens the file specified by this method.
Will be read, uploaded, and closed by Upload() method.
Sets metadata 'title' and 'mimeType' automatically if not specified.
Sets metadata 'title' and 'mimeType' automatically if not specified and
the file is uploaded for the first time (id is not set).

:param filename: name of the file to be uploaded.
:type filename: str.
"""
self.content = open(filename, "rb")
if self.get("mimeType") is None:
self["mimeType"] = mimetypes.guess_type(filename)[0]
if self.get("id") is None:
if self.get("title") is None:
self["title"] = os.path.basename(filename)
if self.get("mimeType") is None:
self["mimeType"] = mimetypes.guess_type(filename)[0]

def GetContentString(
self, mimetype=None, encoding="utf-8", remove_bom=False
Expand Down
6 changes: 3 additions & 3 deletions pydrive2/test/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ def test_Files_Insert_Content_Unicode_String(self):
def test_Files_Insert_Content_File(self):
drive = GoogleDrive(self.ga)
file1 = drive.CreateFile()
filename = self.getTempFile("filecontent")
file1["title"] = filename
contentFile = self.getTempFile("actual_content", "some string")
file1.SetContentFile(contentFile)
pydrive_retry(file1.Upload) # Files.insert

self.assertEqual(file1.metadata["title"], filename)
self.assertEqual(
file1.metadata["title"], os.path.basename(contentFile)
)
pydrive_retry(
file1.FetchContent
) # Force download and double check content.
Expand Down