|
1 | 1 | import io
|
| 2 | +import os |
2 | 3 | import mimetypes
|
3 | 4 | import json
|
4 | 5 |
|
@@ -235,30 +236,35 @@ def SetContentString(self, content, encoding="utf-8"):
|
235 | 236 | """Set content of this file to be a string.
|
236 | 237 |
|
237 | 238 | 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). |
239 | 241 |
|
240 | 242 | :param encoding: The encoding to use when setting the content of this file.
|
241 | 243 | :type encoding: str
|
242 | 244 | :param content: content of the file in string.
|
243 | 245 | :type content: str
|
244 | 246 | """
|
245 | 247 | 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: |
247 | 249 | self["mimeType"] = "text/plain"
|
248 | 250 |
|
249 | 251 | def SetContentFile(self, filename):
|
250 | 252 | """Set content of this file from a file.
|
251 | 253 |
|
252 | 254 | Opens the file specified by this method.
|
253 | 255 | 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). |
255 | 258 |
|
256 | 259 | :param filename: name of the file to be uploaded.
|
257 | 260 | :type filename: str.
|
258 | 261 | """
|
259 | 262 | 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] |
262 | 268 |
|
263 | 269 | def GetContentString(
|
264 | 270 | self, mimetype=None, encoding="utf-8", remove_bom=False
|
|
0 commit comments