Skip to content

Commit 77e4b65

Browse files
shchekleinefiop
andauthored
Set content picks default title/mime if file is created for the first time (#276)
* fix(272): trying less disruptive change * fix(272/update): use basename of a file by default in SetContentFile * docs: undo unrelated changes --------- Co-authored-by: Ruslan Kuprieiev <[email protected]>
1 parent 045ebe2 commit 77e4b65

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

pydrive2/files.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import io
2+
import os
23
import mimetypes
34
import json
45

@@ -235,30 +236,35 @@ def SetContentString(self, content, encoding="utf-8"):
235236
"""Set content of this file to be a string.
236237
237238
Creates io.BytesIO instance of utf-8 encoded string.
238-
Sets mimeType to be 'text/plain' if not specified.
239+
Sets mimeType to be 'text/plain' if not specified and file id is not
240+
set (means that we are uploading this file for the first time).
239241
240242
:param encoding: The encoding to use when setting the content of this file.
241243
:type encoding: str
242244
:param content: content of the file in string.
243245
:type content: str
244246
"""
245247
self.content = io.BytesIO(content.encode(encoding))
246-
if self.get("mimeType") is None:
248+
if self.get("mimeType") is None and self.get("id") is None:
247249
self["mimeType"] = "text/plain"
248250

249251
def SetContentFile(self, filename):
250252
"""Set content of this file from a file.
251253
252254
Opens the file specified by this method.
253255
Will be read, uploaded, and closed by Upload() method.
254-
Sets metadata 'title' and 'mimeType' automatically if not specified.
256+
Sets metadata 'title' and 'mimeType' automatically if not specified and
257+
the file is uploaded for the first time (id is not set).
255258
256259
:param filename: name of the file to be uploaded.
257260
:type filename: str.
258261
"""
259262
self.content = open(filename, "rb")
260-
if self.get("mimeType") is None:
261-
self["mimeType"] = mimetypes.guess_type(filename)[0]
263+
if self.get("id") is None:
264+
if self.get("title") is None:
265+
self["title"] = os.path.basename(filename)
266+
if self.get("mimeType") is None:
267+
self["mimeType"] = mimetypes.guess_type(filename)[0]
262268

263269
def GetContentString(
264270
self, mimetype=None, encoding="utf-8", remove_bom=False

pydrive2/test/test_file.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ def test_Files_Insert_Content_Unicode_String(self):
129129
def test_Files_Insert_Content_File(self):
130130
drive = GoogleDrive(self.ga)
131131
file1 = drive.CreateFile()
132-
filename = self.getTempFile("filecontent")
133-
file1["title"] = filename
134132
contentFile = self.getTempFile("actual_content", "some string")
135133
file1.SetContentFile(contentFile)
136134
pydrive_retry(file1.Upload) # Files.insert
137135

138-
self.assertEqual(file1.metadata["title"], filename)
136+
self.assertEqual(
137+
file1.metadata["title"], os.path.basename(contentFile)
138+
)
139139
pydrive_retry(
140140
file1.FetchContent
141141
) # Force download and double check content.

0 commit comments

Comments
 (0)