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 mediagoblin
.storage
import (
27 class BasicFileStorage(StorageInterface
):
29 Basic local filesystem implementation of storage API
34 def __init__(self
, base_dir
, base_url
=None, **kwargs
):
37 - base_dir: Base directory things will be served out of. MUST
39 - base_url: URL files will be served from
41 self
.base_dir
= base_dir
42 self
.base_url
= base_url
44 def _resolve_filepath(self
, filepath
):
46 Transform the given filepath into a local filesystem filepath.
49 self
.base_dir
, *clean_listy_filepath(filepath
))
51 def file_exists(self
, filepath
):
52 return os
.path
.exists(self
._resolve
_filepath
(filepath
))
54 def get_file(self
, filepath
, mode
='r'):
55 # Make directories if necessary
57 directory
= self
._resolve
_filepath
(filepath
[:-1])
58 if not os
.path
.exists(directory
):
59 os
.makedirs(directory
)
61 # Grab and return the file in the mode specified
62 return open(self
._resolve
_filepath
(filepath
), mode
)
64 def delete_file(self
, filepath
):
65 # TODO: Also delete unused directories if empty (safely, with
66 # checks to avoid race conditions).
68 os
.remove(self
._resolve
_filepath
(filepath
))
70 # the file do not exists!
71 # This should fix bug #255
74 def file_url(self
, filepath
):
77 "base_url not set, cannot provide file urls")
79 return urlparse
.urljoin(
81 '/'.join(clean_listy_filepath(filepath
)))
83 def get_local_path(self
, filepath
):
84 return self
._resolve
_filepath
(filepath
)
86 def copy_local_to_storage(self
, filename
, filepath
):
88 Copy this file from locally to the storage system.
90 # Make directories if necessary
92 directory
= self
._resolve
_filepath
(filepath
[:-1])
93 if not os
.path
.exists(directory
):
94 os
.makedirs(directory
)
97 filename
, self
.get_local_path(filepath
))