1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 from __future__
import print_function
21 from mediagoblin
.storage
import StorageInterface
, clean_listy_filepath
24 class MountError(Exception):
28 class MountStorage(StorageInterface
):
30 Experimental "Mount" virtual Storage Interface
32 This isn't an interface to some real storage, instead it's a
33 redirecting interface, that redirects requests to other
36 For example, say you have the paths:
38 1. ['user_data', 'cwebber', 'avatar.jpg']
39 2. ['user_data', 'elrond', 'avatar.jpg']
40 3. ['media_entries', '34352f304c3f4d0ad8ad0f043522b6f2', 'thumb.jpg']
42 You could mount media_entries under CloudFileStorage and user_data
43 under BasicFileStorage. Then 1 would be passed to
44 BasicFileStorage under the path ['cwebber', 'avatar.jpg'] and 3
45 would be passed to CloudFileStorage under
46 ['34352f304c3f4d0ad8ad0f043522b6f2', 'thumb.jpg'].
48 In other words, this is kind of like mounting /home/ and /etc/
49 under different filesystems on your operating system... but with
50 mediagoblin filestorages :)
52 To set this up, you currently need to call the mount() method with
53 the target path and a backend, that shall be available under that
54 target path. You have to mount things in a sensible order,
55 especially you can't mount ["a", "b"] before ["a"].
57 def __init__(self
, **kwargs
):
60 def mount(self
, dirpath
, backend
):
62 Mount a new backend under dirpath
64 new_ent
= clean_listy_filepath(dirpath
)
66 print("Mounting:", repr(new_ent
))
67 already
, rem_1
, table
, rem_2
= self
._resolve
_to
_backend
(new_ent
, True)
68 print("===", repr(already
), repr(rem_1
), repr(rem_2
), len(table
))
70 assert (len(rem_2
) > 0) or (None not in table
), \
71 "That path is already mounted"
72 assert (len(rem_2
) > 0) or (len(table
) == 0), \
73 "A longer path is already mounted here"
80 def _resolve_to_backend(self
, filepath
, extra_info
=False):
82 extra_info = True is for internal use!
84 Normally, returns the backend and the filepath inside that backend.
86 With extra_info = True it returns the last directory node and the
87 remaining filepath from there in addition.
90 filepath
= filepath
[:]
93 new_be
= table
.get(None)
94 if (new_be
is not None) or res_fp
is None:
97 res_extra
= (table
, filepath
[:])
98 # print "... New res: %r, %r, %r" % (res_be, res_fp, res_extra)
99 if len(filepath
) == 0:
101 query
= filepath
.pop(0)
102 entry
= table
.get(query
)
103 if entry
is not None:
105 res_extra
= (table
, filepath
[:])
109 return (res_be
, res_fp
) + res_extra
111 return (res_be
, res_fp
)
113 def resolve_to_backend(self
, filepath
):
114 backend
, filepath
= self
._resolve
_to
_backend
(filepath
)
116 raise MountError("Path not mounted")
117 return backend
, filepath
119 def __repr__(self
, table
=None, indent
=[]):
122 res
.append("MountStorage<")
123 table
= self
.mounttab
126 res
.append(" " * len(indent
) + repr(indent
) + ": " + repr(v
))
127 for k
, v
in six
.iteritems(table
):
130 res
.append(" " * len(indent
) + repr(k
) + ":")
131 res
+= self
.__repr
__(v
, indent
+ [k
])
132 if table
is self
.mounttab
:
134 return "\n".join(res
)
138 def file_exists(self
, filepath
):
139 backend
, filepath
= self
.resolve_to_backend(filepath
)
140 return backend
.file_exists(filepath
)
142 def get_file(self
, filepath
, mode
='r'):
143 backend
, filepath
= self
.resolve_to_backend(filepath
)
144 return backend
.get_file(filepath
, mode
)
146 def delete_file(self
, filepath
):
147 backend
, filepath
= self
.resolve_to_backend(filepath
)
148 return backend
.delete_file(filepath
)
150 def file_url(self
, filepath
):
151 backend
, filepath
= self
.resolve_to_backend(filepath
)
152 return backend
.file_url(filepath
)
154 def get_local_path(self
, filepath
):
155 backend
, filepath
= self
.resolve_to_backend(filepath
)
156 return backend
.get_local_path(filepath
)
158 def copy_locally(self
, filepath
, dest_path
):
160 Need to override copy_locally, because the local_storage
161 attribute is not correct.
163 backend
, filepath
= self
.resolve_to_backend(filepath
)
164 backend
.copy_locally(filepath
, dest_path
)