Commit | Line | Data |
---|---|---|
a2468d18 JW |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS. | |
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 | ||
17 | from mediagoblin.storage import ( | |
18 | StorageInterface, | |
19 | clean_listy_filepath, | |
20 | NoWebServing) | |
21 | ||
22 | import os | |
23 | import urlparse | |
24 | ||
25 | ||
26 | class BasicFileStorage(StorageInterface): | |
27 | """ | |
28 | Basic local filesystem implementation of storage API | |
29 | """ | |
30 | ||
31 | local_storage = True | |
32 | ||
33 | def __init__(self, base_dir, base_url=None, **kwargs): | |
34 | """ | |
35 | Keyword arguments: | |
36 | - base_dir: Base directory things will be served out of. MUST | |
37 | be an absolute path. | |
38 | - base_url: URL files will be served from | |
39 | """ | |
40 | self.base_dir = base_dir | |
41 | self.base_url = base_url | |
42 | ||
43 | def _resolve_filepath(self, filepath): | |
44 | """ | |
45 | Transform the given filepath into a local filesystem filepath. | |
46 | """ | |
47 | return os.path.join( | |
48 | self.base_dir, *clean_listy_filepath(filepath)) | |
49 | ||
50 | def file_exists(self, filepath): | |
51 | return os.path.exists(self._resolve_filepath(filepath)) | |
52 | ||
53 | def get_file(self, filepath, mode='r'): | |
54 | # Make directories if necessary | |
55 | if len(filepath) > 1: | |
56 | directory = self._resolve_filepath(filepath[:-1]) | |
57 | if not os.path.exists(directory): | |
58 | os.makedirs(directory) | |
59 | ||
60 | # Grab and return the file in the mode specified | |
61 | return open(self._resolve_filepath(filepath), mode) | |
62 | ||
63 | def delete_file(self, filepath): | |
64 | # TODO: Also delete unused directories if empty (safely, with | |
65 | # checks to avoid race conditions). | |
66 | os.remove(self._resolve_filepath(filepath)) | |
67 | ||
68 | def file_url(self, filepath): | |
69 | if not self.base_url: | |
70 | raise NoWebServing( | |
71 | "base_url not set, cannot provide file urls") | |
72 | ||
73 | return urlparse.urljoin( | |
74 | self.base_url, | |
75 | '/'.join(clean_listy_filepath(filepath))) | |
76 | ||
77 | def get_local_path(self, filepath): | |
78 | return self._resolve_filepath(filepath) |