Added delete_file, url_for_file methods to BasicFileStorage

This commit is contained in:
Christopher Allan Webber 2011-04-16 09:36:36 -05:00
parent 3c1a9d753c
commit b1bb050b27

View File

@ -22,6 +22,7 @@ from werkzeug.utils import secure_filename
class Error(Exception): pass class Error(Exception): pass
class InvalidFilepath(Error): pass class InvalidFilepath(Error): pass
class NoWebServing(Error): pass
class NotImplementedError(Error): pass class NotImplementedError(Error): pass
@ -136,15 +137,15 @@ class BasicFileStorage(StorageInterface):
Basic local filesystem implementation of storage API Basic local filesystem implementation of storage API
""" """
def __init__(self, base_dir, serve_url=None): def __init__(self, base_dir, base_url=None):
""" """
Keyword arguments: Keyword arguments:
- base_dir: Base directory things will be served out of. MUST - base_dir: Base directory things will be served out of. MUST
be an absolute path. be an absolute path.
- serve_url: URL files will be served from - base_url: URL files will be served from
""" """
self.base_dir = base_dir self.base_dir = base_dir
self.serve_url = serve_url self.base_url = base_url
def _resolve_filepath(self, filepath): def _resolve_filepath(self, filepath):
""" """
@ -166,9 +167,16 @@ class BasicFileStorage(StorageInterface):
# Grab and return the file in the mode specified # Grab and return the file in the mode specified
return open(self._resolve_filepath(filepath), mode) return open(self._resolve_filepath(filepath), mode)
def delete_file(self, filepath): def delete_file(self, filepath):
pass # TODO: Also delete unused directories if empty (safely, with
# checks to avoid race conditions).
os.remove(self._resolve_filepath(filepath))
def url_for_file(self, filepath): def url_for_file(self, filepath):
pass 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)))