1
- import os
2
1
import io
3
2
import mimetypes
4
3
import json
@@ -262,9 +261,7 @@ def SetContentString(self, content, encoding="utf-8"):
262
261
:param content: content of the file in string.
263
262
:type content: str
264
263
"""
265
- if content :
266
- self .content = io .BytesIO (content .encode (encoding ))
267
-
264
+ self .content = io .BytesIO (content .encode (encoding ))
268
265
if self .get ("mimeType" ) is None :
269
266
self ["mimeType" ] = "text/plain"
270
267
@@ -278,9 +275,7 @@ def SetContentFile(self, filename):
278
275
:param filename: name of the file to be uploaded.
279
276
:type filename: str.
280
277
"""
281
- if os .path .getsize (filename ):
282
- self .content = open (filename , "rb" )
283
-
278
+ self .content = open (filename , "rb" )
284
279
if self .get ("title" ) is None :
285
280
self ["title" ] = filename
286
281
if self .get ("mimeType" ) is None :
@@ -358,32 +353,20 @@ def download(fd, request):
358
353
download (fd , files .get_media (fileId = file_id ))
359
354
except errors .HttpError as error :
360
355
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
+ ):
386
360
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 )
387
370
388
371
if mimetype == "text/plain" and remove_bom :
389
372
fd .seek (0 )
0 commit comments