Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[azure] Add method to set ContentDisposition #898

Merged
merged 1 commit into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/backends/azure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,8 @@ The following settings are available:

A variable to set the Cache-Control HTTP response header. E.g.
``AZURE_CACHE_CONTROL = "public,max-age=31536000,immutable"``

``AZURE_OBJECT_PARAMETERS``

Use this to set content settings on all objects. To set these on a per-object
basis, subclass the backend and override ``AzureStorage.get_object_parameters``.
37 changes: 28 additions & 9 deletions storages/backends/azure_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def get_default_settings(self):
return {
"account_name": setting("AZURE_ACCOUNT_NAME"),
"account_key": setting("AZURE_ACCOUNT_KEY"),
"object_parameters": setting("AZURE_OBJECT_PARAMETERS", {}),
"azure_container": setting("AZURE_CONTAINER"),
"azure_ssl": setting("AZURE_SSL", True),
"upload_max_conn": setting("AZURE_UPLOAD_MAX_CONN", 2),
Expand Down Expand Up @@ -242,11 +243,7 @@ def size(self, name):
def _save(self, name, content):
cleaned_name = clean_name(name)
name = self._get_valid_path(name)
guessed_type, content_encoding = mimetypes.guess_type(name)
content_type = (
_content_type(content) or
guessed_type or
self.default_content_type)
params = self._get_content_settings_parameters(name, content)

# Unwrap django file (wrapped by parent's save call)
if isinstance(content, File):
Expand All @@ -257,10 +254,7 @@ def _save(self, name, content):
container_name=self.azure_container,
blob_name=name,
stream=content,
content_settings=ContentSettings(
content_type=content_type,
content_encoding=content_encoding,
cache_control=self.cache_control),
content_settings=ContentSettings(**params),
max_connections=self.upload_max_conn,
timeout=self.timeout)
return cleaned_name
Expand All @@ -287,6 +281,31 @@ def url(self, name, expire=None):
protocol=self.azure_protocol,
**make_blob_url_kwargs)

def _get_content_settings_parameters(self, name, content=None):
params = {}

guessed_type, content_encoding = mimetypes.guess_type(name)
content_type = (
_content_type(content) or
guessed_type or
self.default_content_type)

params['cache_control'] = self.cache_control
params['content_type'] = content_type
params['content_encoding'] = content_encoding

params.update(self.get_object_parameters(name))
return params

def get_object_parameters(self, name):
"""
Returns a dictionary that is passed to content settings. Override this
method to adjust this on a per-object basis to set e.g ContentDisposition.

By default, returns the value of AZURE_OBJECT_PARAMETERS.
"""
return self.object_parameters.copy()

def get_modified_time(self, name):
"""
Returns an (aware) datetime object containing the last modified time if
Expand Down