summaryrefslogtreecommitdiff
path: root/fs/expose/django_storage.py
blob: 9a84b2a21169fda09dbd13fce1bcfa6d0ac0ab54 (plain)
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
"""
fs.expose.django
================

Use an FS object for Django File Storage

This module exposes the class "FSStorage", a simple adapter for using FS
objects as Django storage objects.  Simply include the following lines
in your settings.py::

    DEFAULT_FILE_STORAGE = fs.expose.django_storage.FSStorage
    DEFAULT_FILE_STORAGE_FS = OSFS('foo/bar')  # Or whatever FS


"""

from django.conf import settings
from django.core.files.storage import Storage
from django.core.files import File

from fs.path import abspath, dirname
from fs.errors import convert_fs_errors, ResourceNotFoundError

class FSStorage(Storage):
    """Expose an FS object as a Django File Storage object."""

    def __init__(self, fs=None, base_url=None):
        """
        :param fs: an FS object
        :param base_url: The url to prepend to the path
         
        """
        if fs is None:
            fs = settings.DEFAULT_FILE_STORAGE_FS
        if base_url is None:
            base_url = settings.MEDIA_URL
        base_url = base_url.rstrip('/')
        self.fs = fs
        self.base_url = base_url

    def exists(self, name):
        return self.fs.isfile(name)

    def path(self, name):
        path = self.fs.getsyspath(name)
        if path is None:
            raise NotImplementedError
        return path

    @convert_fs_errors
    def size(self, name):
        return self.fs.getsize(name)

    @convert_fs_errors
    def url(self, name):
        return self.base_url + abspath(name)

    @convert_fs_errors
    def _open(self, name, mode):
        return File(self.fs.open(name, mode))

    @convert_fs_errors
    def _save(self, name, content):
        self.fs.makedir(dirname(name), allow_recreate=True, recursive=True)
        self.fs.setcontents(name, content)
        return name

    @convert_fs_errors
    def delete(self, name):
        try:
            self.fs.remove(name)
        except ResourceNotFoundError:
            pass