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 """Delete file at filepath
67 Raises OSError in case filepath is a directory."""
69 os
.remove(self
._resolve
_filepath
(filepath
))
71 def delete_dir(self
, dirpath
, recursive
=False):
72 """returns True on succes, False on failure"""
74 dirpath
= self
._resolve
_filepath
(dirpath
)
76 # Shortcut the default and simple case of nonempty=F, recursive=F
79 shutil
.rmtree(dirpath
)
81 #TODO: log something here
83 else: # recursively delete everything
87 #TODO: log something here
91 def file_url(self
, filepath
):
94 "base_url not set, cannot provide file urls")
96 return urlparse
.urljoin(
98 '/'.join(clean_listy_filepath(filepath
)))
100 def get_local_path(self
, filepath
):
101 return self
._resolve
_filepath
(filepath
)
103 def copy_local_to_storage(self
, filename
, filepath
):
105 Copy this file from locally to the storage system.
107 # Make directories if necessary
108 if len(filepath
) > 1:
109 directory
= self
._resolve
_filepath
(filepath
[:-1])
110 if not os
.path
.exists(directory
):
111 os
.makedirs(directory
)
112 # This uses chunked copying of 16kb buffers (Py2.7):
113 shutil
.copy(filename
, self
.get_local_path(filepath
))
115 def get_file_size(self
, filepath
):
116 return os
.stat(self
._resolve
_filepath
(filepath
)).st_size