I don't think we need _create_dirs_for_filepath.
[mediagoblin.git] / mediagoblin / storage.py
CommitLineData
a6b378ef
CAW
1# GNU Mediagoblin -- federated, autonomous media hosting
2# Copyright (C) 2011 Free Software Foundation, Inc
3#
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.
8#
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.
13#
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/>.
16
779f2b94 17import os
a6b378ef
CAW
18
19from werkzeug.utils import secure_filename
20
21
770c12be
CAW
22class Error(Exception): pass
23class InvalidFilepath(Error): pass
24
797be93c
CAW
25class NotImplementedError(Error): pass
26
770c12be 27
a6b378ef
CAW
28def clean_listy_filepath(listy_filepath):
29 """
30 Take a listy filepath (like ['dir1', 'dir2', 'filename.jpg']) and
31 clean out any nastiness from it.
32
33 For example:
34 >>> clean_listy_filepath([u'/dir1/', u'foo/../nasty', u'linooks.jpg'])
35 [u'dir1', u'foo_.._nasty', u'linooks.jpg']
36
37 Args:
38 - listy_filepath: a list of filepath components, mediagoblin
39 storage API style.
40
41 Returns:
42 A cleaned list of unicode objects.
43 """
770c12be 44 cleaned_filepath = [
a6b378ef
CAW
45 unicode(secure_filename(filepath))
46 for filepath in listy_filepath]
47
770c12be
CAW
48 if u'' in cleaned_filepath:
49 raise InvalidFilepath(
50 "A filename component could not be resolved into a usable name.")
51
52 return cleaned_filepath
53
a6b378ef 54
797be93c
CAW
55class StorageInterface(object):
56 """
57 Interface for the storage API.
58
59 This interface doesn't actually provide behavior, but it defines
60 what kind of storage patterns subclasses should provide.
61
62 It is important to note that the storage API idea of a "filepath"
63 is actually like ['dir1', 'dir2', 'file.jpg'], so keep that in
64 mind while reading method documentation.
65 """
66 # def __init__(self, *args, **kwargs):
67 # pass
68
69 def __raise_not_implemented(self):
70 """
71 Raise a warning about some component not implemented by a
72 subclass of this interface.
73 """
74 raise NotImplementedError(
75 "This feature not implemented in this storage API implementation.")
76
77 def file_exists(self, filepath):
78 """
79 Return a boolean asserting whether or not file at filepath
80 exists in our storage system.
81
82 Returns:
83 True / False depending on whether file exists or not.
84 """
85 # Subclasses should override this method.
86 self.__raise_not_implemented()
87
b0de01cf
CAW
88 def get_file(self, filepath, mode):
89 """
90 Return a file-like object for reading/writing from this filepath.
91
92 Should create directories, buckets, whatever, as necessary.
93 """
0b9cf289
CAW
94 # Subclasses should override this method.
95 self.__raise_not_implemented()
96
97 def delete_file(self, filepath):
b0de01cf
CAW
98 """
99 Delete or dereference the file at filepath.
100
101 This might need to delete directories, buckets, whatever, for
102 cleanliness. (Be sure to avoid race conditions on that though)
103 """
0b9cf289
CAW
104 # Subclasses should override this method.
105 self.__raise_not_implemented()
106
797be93c
CAW
107 def get_unique_filename(self, filepath):
108 """
109 If a filename at filepath already exists, generate a new name.
110
111 Eg, if the filename doesn't exist:
112 >>> storage_handler.get_unique_filename(['dir1', 'dir2', 'fname.jpg'])
113 [u'dir1', u'dir2', u'fname.jpg']
114
115 But if a file does exist, let's get one back with at uuid tacked on:
116 >>> storage_handler.get_unique_filename(['dir1', 'dir2', 'fname.jpg'])
ef10e3a2 117 [u'dir1', u'dir2', u'd02c3571-dd62-4479-9d62-9e3012dada29-fname.jpg']
797be93c 118 """
0b9cf289
CAW
119 if self.file_exists(filepath):
120 return filepath[:-1] + ["%s-%s" % (uuid.uuid4(), filepath[-1])]
121 else:
122 return filepath
779f2b94
CAW
123
124
125class BasicFileStorage(StorageInterface):
126 """
127 Basic local filesystem implementation of storage API
128 """
129
130 def __init__(self, base_dir, serve_url=None):
131 """
132 Keyword arguments:
133 - base_dir: Base directory things will be served out of. MUST
134 be an absolute path.
135 - serve_url: URL files will be served from
136 """
137 self.base_dir = base_dir
138 self.serve_url = serve_url
139
140 def _resolve_filepath(self, filepath):
141 """
142 Transform the given filepath into a local filesystem filepath.
143 """
144 return os.path.join(
145 self.base_dir, *clean_listy_filepath(filepath))
146
779f2b94
CAW
147 def file_exists(self, filepath):
148 return os.path.exists(self._resolve_filepath(filepath))
149
150 def get_file(self, filepath, mode):
151 pass
152
153 def delete_file(self, filepath):
154 pass