Source code for flask_fs.backends
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import six
from flask_fs import files
__all__ = [i.encode('ascii') for i in ('BaseBackend', 'DEFAULT_BACKEND')]
DEFAULT_BACKEND = 'local'
[docs]class BaseBackend(object):
'''
Abstract class to implement backend.
'''
root = None
DEFAULT_MIME = 'application/octet-stream'
def __init__(self, name, config):
self.name = name
self.config = config
[docs] def exists(self, filename):
'''Test wether a file exists or not given its filename in the storage'''
raise NotImplementedError('Existance checking is not implemented')
[docs] def open(self, filename, *args, **kwargs):
'''Open a file given its filename relative to the storage root'''
raise NotImplementedError('Open operation is not implemented')
[docs] def read(self, filename):
'''Read a file content given its filename in the storage'''
raise NotImplementedError('Read operation is not implemented')
[docs] def write(self, filename, content):
'''Write content into a file given its filename in the storage'''
raise NotImplementedError('Write operation is not implemented')
[docs] def delete(self, filename):
'''Delete a file given its filename in the storage'''
raise NotImplementedError('Delete operation is not implemented')
[docs] def copy(self, filename, target):
'''Copy a file given its filename to another path in the storage'''
raise NotImplementedError('Copy operation is not implemented')
[docs] def move(self, filename, target):
'''
Move a file given its filename to another path in the storage
Default implementation perform a copy then a delete.
Backends should overwrite it if there is a better way.
'''
self.copy(filename, target)
self.delete(filename)
[docs] def save(self, file_or_wfs, filename, overwrite=False):
'''
Save a file-like object or a `werkzeug.FileStorage` with the specified filename.
:param storage: The file or the storage to be saved.
:param filename: The destination in the storage.
:param overwrite: if `False`, raise an exception if file exists in storage
:raises FileExists: when file exists and overwrite is `False`
'''
self.write(filename, file_or_wfs.read())
return filename
return meta
raise NotImplementedError('Copy operation is not implemented')
[docs] def serve(self, filename):
'''Serve a file given its filename'''
raise NotImplementedError('serve operation is not implemented')
[docs] def as_binary(self, content, encoding='utf8'):
'''Perform content encoding for binary write'''
if hasattr(content, 'read'):
return content.read()
elif isinstance(content, six.text_type):
return content.encode(encoding)
else:
return content