sqlalchemy_imageattach.stores.fs
— Filesystem-backed image storage¶It provides two filesystem-backed image storage implementations:
FileSystemStore
locate()
method returns URLs
of the hard-coded base URL.HttpExposedFileSystemStore
FileSystemStore
except it provides
WSGI middleware (wsgi_middleware()
)
which actually serves image files and its
locate()
method returns URLs
based on the actual requested URL.sqlalchemy_imageattach.stores.fs.
BaseFileSystemStore
(path)¶Abstract base class of FileSystemStore
and
HttpExposedFileSystemStore
.
sqlalchemy_imageattach.stores.fs.
FileSystemStore
(path, base_url)¶Filesystem-backed storage implementation with hard-coded URL routing.
sqlalchemy_imageattach.stores.fs.
HttpExposedFileSystemStore
(path, prefix='__images__', host_url_getter=None, cors=False)¶Filesystem-backed storage implementation with WSGI middleware which serves actual image files.
from flask import Flask
from sqlalchemy_imageattach.stores.fs import HttpExposedFileSystemStore
app = Flask(__name__)
fs_store = HttpExposedFileSystemStore('userimages', 'images/')
app.wsgi_app = fs_store.wsgi_middleware(app.wsgi_app)
To determine image urls, the address of server also has to be determined.
Although it can be automatically detected using wsgi_middleware()
,
WSGI unfortunately is not always there. For example, Celery tasks aren’t
executed by HTTP requests, so there’s no reachable Host
header.
When its host url is not determined you would get RuntimeError
if you try locating image urls:
Traceback (most recent call last):
...
File "/.../sqlalchemy_imageattach/stores/fs.py", line 93, in get_url
base_url = self.base_url
File "/.../sqlalchemy_imageattach/stores/fs.py", line 151, in base_url
type(self)
RuntimeError: could not determine image url. there are two ways to workaround this:
- set host_url_getter parameter to sqlalchemy_imageattach.stores.fs.HttpExposedFileSystemStore
- use sqlalchemy_imageattach.stores.fs.HttpExposedFileSystemStore.wsgi_middleware
see docs of sqlalchemy_imageattach.stores.fs.HttpExposedFileSystemStore for more details
For such case, you can optionally set host_url_getter
option.
It takes a callable which takes no arguments and returns a host url string
like 'http://servername/'
.
fs_store = HttpExposedFileSystemStore(
'userimages', 'images/',
host_url_getter=lambda:
'https://{0}/'.format(app.config['SERVER_NAME'])
)
Parameters: |
|
---|
New in version 1.0.0: Added host_url_getter
option.
wsgi_middleware
(app, cors=False)¶WSGI middlewares that wraps the given app
and serves
actual image files.
fs_store = HttpExposedFileSystemStore('userimages', 'images/')
app = fs_store.wsgi_middleware(app)
Parameters: | app (Callable [[],
Iterable [bytes ]]) – the wsgi app to wrap |
---|---|
Returns: | the another wsgi app that wraps app |
Return type: | StaticServerMiddleware |
sqlalchemy_imageattach.stores.fs.
StaticServerMiddleware
(app, url_path, dir_path, block_size=8192, cors=False)¶Simple static server WSGI middleware.
Parameters: |
|
---|