Skip to content

Commit

Permalink
Merge pull request #63 from PostsDesert/master
Browse files Browse the repository at this point in the history
Fixes file preview in Google Cloud Console and sets correct content-type for a file upload
  • Loading branch information
nhorvath authored May 4, 2023
2 parents bfc5a8e + 0128ce8 commit c9f74ff
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pyrebase/pyrebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from gcloud import storage
from requests.packages.urllib3.contrib.appengine import is_appengine_sandbox
from requests_toolbelt.adapters import appengine
from uuid import uuid4

import python_jwt as jwt
from Crypto.PublicKey import RSA
Expand Down Expand Up @@ -454,7 +455,7 @@ def child(self, *args):
self.path = new_path
return self

def put(self, file, token=None):
def put(self, file, token=None, content_type=None):
# reset path
path = self.path
self.path = None
Expand All @@ -470,10 +471,17 @@ def put(self, file, token=None):
return request_object.json()
elif self.credentials:
blob = self.bucket.blob(path)

# Add metadata to enable file previews in console
blob.metadata = {"firebaseStorageDownloadTokens": str(uuid4())}
if isinstance(file, str):
return blob.upload_from_filename(filename=file)
else:
return blob.upload_from_file(file_obj=file)
# If the file is not a string we need to patch the blob after upload to set the content type
blob.upload_from_string(file)
if content_type:
blob.content_type = content_type
blob.patch()
else:
request_object = self.requests.post(request_ref, data=file_object)
raise_detailed_error(request_object)
Expand Down

0 comments on commit c9f74ff

Please sign in to comment.