1 # GNU Mediagoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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/>.
20 from werkzeug
.utils
import secure_filename
23 class Error(Exception): pass
24 class InvalidFilepath(Error
): pass
26 class NotImplementedError(Error
): pass
29 def clean_listy_filepath(listy_filepath
):
31 Take a listy filepath (like ['dir1', 'dir2', 'filename.jpg']) and
32 clean out any nastiness from it.
35 >>> clean_listy_filepath([u'/dir1/', u'foo/../nasty', u'linooks.jpg'])
36 [u'dir1', u'foo_.._nasty', u'linooks.jpg']
39 - listy_filepath: a list of filepath components, mediagoblin
43 A cleaned list of unicode objects.
46 unicode(secure_filename(filepath
))
47 for filepath
in listy_filepath
]
49 if u
'' in cleaned_filepath
:
50 raise InvalidFilepath(
51 "A filename component could not be resolved into a usable name.")
53 return cleaned_filepath
56 class StorageInterface(object):
58 Interface for the storage API.
60 This interface doesn't actually provide behavior, but it defines
61 what kind of storage patterns subclasses should provide.
63 It is important to note that the storage API idea of a "filepath"
64 is actually like ['dir1', 'dir2', 'file.jpg'], so keep that in
65 mind while reading method documentation.
67 # def __init__(self, *args, **kwargs):
70 def __raise_not_implemented(self
):
72 Raise a warning about some component not implemented by a
73 subclass of this interface.
75 raise NotImplementedError(
76 "This feature not implemented in this storage API implementation.")
78 def file_exists(self
, filepath
):
80 Return a boolean asserting whether or not file at filepath
81 exists in our storage system.
84 True / False depending on whether file exists or not.
86 # Subclasses should override this method.
87 self
.__raise
_not
_implemented
()
89 def get_file(self
, filepath
, mode
):
91 Return a file-like object for reading/writing from this filepath.
93 Should create directories, buckets, whatever, as necessary.
95 # Subclasses should override this method.
96 self
.__raise
_not
_implemented
()
98 def delete_file(self
, filepath
):
100 Delete or dereference the file at filepath.
102 This might need to delete directories, buckets, whatever, for
103 cleanliness. (Be sure to avoid race conditions on that though)
105 # Subclasses should override this method.
106 self
.__raise
_not
_implemented
()
108 def url_for_file(self
, filepath
):
110 Get the URL for this file. This assumes our storage has been
111 mounted with some kind of URL which makes this possible.
113 # Subclasses should override this method.
114 self
.__raise
_not
_implemented
()
116 def get_unique_filepath(self
, filepath
):
118 If a filename at filepath already exists, generate a new name.
120 Eg, if the filename doesn't exist:
121 >>> storage_handler.get_unique_filename(['dir1', 'dir2', 'fname.jpg'])
122 [u'dir1', u'dir2', u'fname.jpg']
124 But if a file does exist, let's get one back with at uuid tacked on:
125 >>> storage_handler.get_unique_filename(['dir1', 'dir2', 'fname.jpg'])
126 [u'dir1', u'dir2', u'd02c3571-dd62-4479-9d62-9e3012dada29-fname.jpg']
128 if self
.file_exists(filepath
):
129 return filepath
[:-1] + ["%s-%s" % (uuid
.uuid4(), filepath
[-1])]
134 class BasicFileStorage(StorageInterface
):
136 Basic local filesystem implementation of storage API
139 def __init__(self
, base_dir
, serve_url
=None):
142 - base_dir: Base directory things will be served out of. MUST
144 - serve_url: URL files will be served from
146 self
.base_dir
= base_dir
147 self
.serve_url
= serve_url
149 def _resolve_filepath(self
, filepath
):
151 Transform the given filepath into a local filesystem filepath.
154 self
.base_dir
, *clean_listy_filepath(filepath
))
156 def file_exists(self
, filepath
):
157 return os
.path
.exists(self
._resolve
_filepath
(filepath
))
159 def get_file(self
, filepath
, mode
):
162 def delete_file(self
, filepath
):
165 def url_for_file(self
, filepath
):