Skip to content

Commit

Permalink
implement generate_filename
Browse files Browse the repository at this point in the history
  • Loading branch information
thijstriemstra committed May 22, 2016
1 parent ed1d0a0 commit 1a86ba0
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions queued_storage/backends.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import six

from django import VERSION
Expand Down Expand Up @@ -166,7 +168,7 @@ def open(self, name, mode='rb'):
"""
return self.get_storage(name).open(name, mode)

def save(self, name, content):
def save(self, name, content, max_length=None):
"""
Saves the given content with the given name using the local
storage. If the :attr:`~queued_storage.backends.QueuedStorage.delayed`
Expand All @@ -178,14 +180,17 @@ def save(self, name, content):
:type name: str
:param content: content of the file specified by name
:type content: :class:`~django:django.core.files.File`
:param max_length: The length of the filename will not exceed
`max_length`, if provided.
:type max_length: int
:rtype: str
"""
cache_key = self.get_cache_key(name)
cache.set(cache_key, False)

# Use a name that is available on both the local and remote storage
# systems and save locally.
name = self.get_available_name(name)
name = self.get_available_name(name, max_length)
name = self.local.save(name, content)

# Pass on the cache key to prevent duplicate cache key creation,
Expand Down Expand Up @@ -222,7 +227,7 @@ def get_valid_name(self, name):
"""
return self.get_storage(name).get_valid_name(name)

def get_available_name(self, name):
def get_available_name(self, name, max_length=None):
"""
Returns a filename that's free on both the local and remote storage
systems, and available for new content to be written to.
Expand All @@ -238,6 +243,15 @@ def get_available_name(self, name):
return remote_available_name
return local_available_name

def generate_filename(self, filename):
"""
Validate the filename by calling get_valid_name() and return a filename
to be passed to the save() method.
"""
# `filename` may include a path as returned by FileField.upload_to.
dirname, filename = os.path.split(filename)
return os.path.normpath(os.path.join(dirname, self.get_valid_name(filename)))

def path(self, name):
"""
Returns a local filesystem path where the file can be retrieved using
Expand Down

0 comments on commit 1a86ba0

Please sign in to comment.