diff options
author | Joar Wandborg <git@wandborg.com> | 2011-09-12 02:32:03 +0200 |
---|---|---|
committer | Joar Wandborg <git@wandborg.com> | 2011-09-12 02:32:03 +0200 |
commit | a2468d18ca39afed9102d707e7aea6a613ff2dab (patch) | |
tree | efede9c515269b6ce56b8afcf8b472a1d2dde48b /mediagoblin/storage/filestorage.py | |
parent | 55376ff403496a3271154627c45fe59dc2b44ca7 (diff) | |
download | mediagoblin-a2468d18ca39afed9102d707e7aea6a613ff2dab.tar.lz mediagoblin-a2468d18ca39afed9102d707e7aea6a613ff2dab.tar.xz mediagoblin-a2468d18ca39afed9102d707e7aea6a613ff2dab.zip |
Feature #587 - Split storage.py into submodules
* Removed storage.py
* Created submodules for filestorage, cloudfiles, mountstorage
* Changed test_storage to reflect the changes made in the storage
module structure
* Added mediagoblin.storage.filestorage.BasicFileStorage as a
default for both publicstore and queuestore's `storage_class`
Diffstat (limited to 'mediagoblin/storage/filestorage.py')
-rw-r--r-- | mediagoblin/storage/filestorage.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/mediagoblin/storage/filestorage.py b/mediagoblin/storage/filestorage.py new file mode 100644 index 00000000..22d6eb5a --- /dev/null +++ b/mediagoblin/storage/filestorage.py @@ -0,0 +1,78 @@ +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011 MediaGoblin contributors. See AUTHORS. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +from mediagoblin.storage import ( + StorageInterface, + clean_listy_filepath, + NoWebServing) + +import os +import urlparse + + +class BasicFileStorage(StorageInterface): + """ + Basic local filesystem implementation of storage API + """ + + local_storage = True + + def __init__(self, base_dir, base_url=None, **kwargs): + """ + Keyword arguments: + - base_dir: Base directory things will be served out of. MUST + be an absolute path. + - base_url: URL files will be served from + """ + self.base_dir = base_dir + self.base_url = base_url + + def _resolve_filepath(self, filepath): + """ + Transform the given filepath into a local filesystem filepath. + """ + return os.path.join( + self.base_dir, *clean_listy_filepath(filepath)) + + def file_exists(self, filepath): + return os.path.exists(self._resolve_filepath(filepath)) + + def get_file(self, filepath, mode='r'): + # Make directories if necessary + if len(filepath) > 1: + directory = self._resolve_filepath(filepath[:-1]) + if not os.path.exists(directory): + os.makedirs(directory) + + # Grab and return the file in the mode specified + return open(self._resolve_filepath(filepath), mode) + + def delete_file(self, filepath): + # TODO: Also delete unused directories if empty (safely, with + # checks to avoid race conditions). + os.remove(self._resolve_filepath(filepath)) + + def file_url(self, filepath): + if not self.base_url: + raise NoWebServing( + "base_url not set, cannot provide file urls") + + return urlparse.urljoin( + self.base_url, + '/'.join(clean_listy_filepath(filepath))) + + def get_local_path(self, filepath): + return self._resolve_filepath(filepath) |