Skip to content

Commit

Permalink
Added auto mime detection if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
asbjorn committed Apr 10, 2019
1 parent 732aa44 commit 8e5b6e8
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion django_gcloud_storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import re
from tempfile import SpooledTemporaryFile
import mimetypes

import django
from django.conf import settings
Expand Down Expand Up @@ -162,9 +163,14 @@ def _save(self, name, content):

# Required for InMemoryUploadedFile objects, as they have no fileno
total_bytes = None if not hasattr(content, 'size') else content.size
content_type = None if not hasattr(content, 'content_type') else content.content_type
if content_type is None:
_, file_extension = os.path.splitext(name)
content_type = mimetypes.types_map.get(file_extension, None)


blob = self.bucket.blob(name)
blob.upload_from_file(content, size=total_bytes)
blob.upload_from_file(content, size=total_bytes, content_type=content_type)

return name

Expand Down

0 comments on commit 8e5b6e8

Please sign in to comment.