Only encode to 'utf-8' if needed
This commit is contained in:
parent
0fc328ab0c
commit
30630c46fc
1 changed files with 13 additions and 5 deletions
|
|
@ -49,16 +49,20 @@ class GoogleFile(object):
|
||||||
if guess is not None:
|
if guess is not None:
|
||||||
body['mimeType'] = guess
|
body['mimeType'] = guess
|
||||||
|
|
||||||
params = {}
|
create_params = {}
|
||||||
|
|
||||||
if content:
|
if content:
|
||||||
cont = io.BytesIO(content.encode('utf-8'))
|
try:
|
||||||
|
utf_content = content.encode('utf-8')
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
pass
|
||||||
|
cont = io.BytesIO(utf_content)
|
||||||
media_body = MediaIoBaseUpload(cont,
|
media_body = MediaIoBaseUpload(cont,
|
||||||
body['mimeType'],
|
body['mimeType'],
|
||||||
resumable=True)
|
resumable=True)
|
||||||
params['media_body'] = media_body
|
create_params['media_body'] = media_body
|
||||||
|
|
||||||
meta_data = service.files().create(body=body, fields=fields, **params).execute()
|
meta_data = service.files().create(body=body, fields=fields, **create_params).execute()
|
||||||
|
|
||||||
google_file = GoogleFile(service, meta_data)
|
google_file = GoogleFile(service, meta_data)
|
||||||
google_file.content = content
|
google_file.content = content
|
||||||
|
|
@ -213,7 +217,11 @@ class GoogleFile(object):
|
||||||
params = {}
|
params = {}
|
||||||
|
|
||||||
if content:
|
if content:
|
||||||
cont = io.BytesIO(content.encode('utf-8'))
|
try:
|
||||||
|
utf_content = content.encode('utf-8')
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
pass
|
||||||
|
cont = io.BytesIO(utf_content)
|
||||||
media_body = MediaIoBaseUpload(cont,
|
media_body = MediaIoBaseUpload(cont,
|
||||||
self.meta_data['mimeType'],
|
self.meta_data['mimeType'],
|
||||||
resumable=True)
|
resumable=True)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue