Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: da9a0917c1
Fetching contributors…

Cannot retrieve contributors at this time

file 97 lines (69 sloc) 3.062 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
import urllib

from django.core.cache import cache
from django.core.files.storage import get_storage_class, Storage

from queued_storage.tasks import SaveToRemoteTask

QUEUED_REMOTE_STORAGE_CACHE_KEY_PREFIX = 'queued_remote_storage_'

class QueuedRemoteStorage(Storage):

    def __init__(self, local, remote, cache_prefix=QUEUED_REMOTE_STORAGE_CACHE_KEY_PREFIX, task=None):
        self.local_class = local
        self.remote_class = remote
        self.cache_prefix = cache_prefix
        self._local_instance = None
        self._remote_instance = None

        # allow users to override the task that uploads the image to the remote
        # server
        self.task = task or SaveToRemoteTask

    @property
    def local(self):
        if self._local_instance is None:
            self._local_instance = get_storage_class(self.local_class)()
        return self._local_instance

    @property
    def remote(self):
        if self._remote_instance is None:
            self._remote_instance = get_storage_class(self.remote_class)()
        return self._remote_instance

    def get_storage(self, name):
        cache_result = cache.get(self.get_cache_key(name))

        if cache_result:
            return self.remote
        elif cache_result is None and self.remote.exists(name):
            cache.set(self.get_cache_key(name), True)
            return self.remote

        return self.local

    def get_cache_key(self, name):
        return '%s%s' % (self.cache_prefix, urllib.quote(name))

    def using_local(self, name):
        return self.get_storage(name) is self.local

    def using_remote(self, name):
        return self.get_storage(name) is self.remote

    def open(self, name, *args, **kwargs):
        return self.get_storage(name).open(name, *args, **kwargs)

    def save(self, name, content):
        cache.set(self.get_cache_key(name), False)
        name = self.local.save(name, content)

        self.task.delay(name, self.local_class, self.remote_class, self.get_cache_key(name))

        return name

    def get_valid_name(self, name):
        return self.get_storage(name).get_valid_name(name)

    def get_available_name(self, name):
        return self.get_storage(name).get_available_name(name)

    def path(self, name):
        return self.get_storage(name).path(name)

    def delete(self, name):
        return self.get_storage(name).delete(name)

    def exists(self, name):
        return self.get_storage(name).exists(name)

    def listdir(self, name):
        return self.get_storage(name).listdir(name)

    def size(self, name):
        return self.get_storage(name).size(name)

    def url(self, name):
        return self.get_storage(name).url(name)

class FileSystemAndS3Backend(QueuedRemoteStorage):

    def __init__(self, cache_prefix=QUEUED_REMOTE_STORAGE_CACHE_KEY_PREFIX):
        super(FileSystemAndS3Backend, self).__init__(
            local='django.core.files.storage.FileSystemStorage',
            remote='storages.backends.s3boto.S3BotoStorage',
            cache_prefix=cache_prefix
        )
Something went wrong with that request. Please try again.