static_files#

class litestar.static_files.StaticFiles#

Bases: object

ASGI App that handles file sending.

__init__(is_html_mode: bool, directories: Sequence[PathType], file_system: FileSystemProtocol, send_as_attachment: bool = False) None#

Initialize the Application.

Parameters:
  • is_html_mode – Flag dictating whether serving html. If true, the default file will be index.html.

  • directories – A list of directories to serve files from.

  • file_system – The file_system spec to use for serving files.

  • send_as_attachment – Whether to send the file with a content-disposition header of attachment or inline

async get_fs_info(directories: Sequence[PathType], file_path: PathType) tuple[Path, FileInfo] | tuple[None, None]#

Return the resolved path and a stat_result.

Parameters:
  • directories – A list of directory paths.

  • file_path – A file path to resolve

Returns:

A tuple with an optional resolved Path instance and an optional stat_result.

async __call__(scope: Scope, receive: Receive, send: Send) None#

ASGI callable.

Parameters:
  • scope – ASGI scope

  • receive – ASGI receive callable

  • send – ASGI send callable

Returns:

None

class litestar.static_files.StaticFilesConfig#

Bases: object

Configuration for static file service.

To enable static files, pass an instance of this class to the Litestar constructor using the ‘static_files_config’ key.

path: str#

Path to serve static files from.

Note that the path cannot contain path parameters.

directories: list[PathType]#

A list of directories to serve files from.

html_mode: bool = False#

Flag dictating whether serving html.

If true, the default file will be ‘index.html’.

name: str | None = None#

An optional string identifying the static files handler.

file_system: Any = <litestar.file_system.BaseLocalFileSystem object>#

The file_system spec to use for serving files.

Notes

__init__(path: str, directories: list[PathType], html_mode: bool = False, name: str | None = None, file_system: Any = <litestar.file_system.BaseLocalFileSystem object>, opt: dict[str, Any] | None = None, guards: list[Guard] | None = None, exception_handlers: ExceptionHandlersMap | None = None, send_as_attachment: bool = False) None#
opt: dict[str, Any] | None = None#

A string key dictionary of arbitrary values that will be added to the static files handler.

guards: list[Guard] | None = None#

A list of Guard callables.

exception_handlers: ExceptionHandlersMap | None = None#

A dictionary that maps handler functions to status codes and/or exception types.

send_as_attachment: bool = False#

Whether to send the file as an attachment.

to_static_files_app() ASGIRouteHandler#

Return an ASGI app serving static files based on the config.

Returns:

StaticFiles