静态文件

class litestar.static_files.StaticFiles

基类:object

ASGI App that handles file sending.

__init__(is_html_mode: bool, directories: Sequence[PathType], file_system: FileSystemProtocol, send_as_attachment: bool = False, resolve_symlinks: bool = True, headers: dict[str, str] | None = None) None

Initialize the Application.

参数:
  • 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

  • resolve_symlinks -- Resolve symlinks to the directories

  • headers -- Headers that will be sent with every response.

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

Return the resolved path and a stat_result.

在 2.8.3 版本发生变更: Prevent CVE-2024-32982 by ensuring that the resolved path is within the configured directory as part of advisory GHSA-83pv-qr33-2vcf.

参数:
  • directories -- A list of directory paths.

  • file_path -- A file path to resolve

返回:

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

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

ASGI callable.

参数:
  • scope -- ASGI scope

  • receive -- ASGI receive callable

  • send -- ASGI send callable

返回:

None

class litestar.static_files.StaticFilesConfig

基类: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.

备注

opt: dict[str, Any] | None = None

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

__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
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.

返回:

StaticFiles

litestar.static_files.create_static_files_router(path: str, directories: list[PathType], file_system: Any = None, send_as_attachment: bool = False, html_mode: bool = False, name: str = 'static', after_request: AfterRequestHookHandler | None = None, after_response: AfterResponseHookHandler | None = None, before_request: BeforeRequestHookHandler | None = None, cache_control: CacheControlHeader | None = None, exception_handlers: ExceptionHandlersMap | None = None, guards: list[Guard] | None = None, include_in_schema: bool | EmptyType = False, middleware: Sequence[Middleware] | None = None, opt: dict[str, Any] | None = None, security: Sequence[SecurityRequirement] | None = None, tags: Sequence[str] | None = None, router_class: type[Router] = <class 'litestar.router.Router'>, resolve_symlinks: bool = True) Router

创建带有处理程序的路由器以提供静态文件。 Args: path: 提供静态文件的路径 directories: 提供静态文件的目录 file_system: 用于加载文件的文件系统。 BaseFileSystemfsspec.spec.AbstractFileSystemfsspec.asyn.AsyncFileSystem 的实例将直接使用。如果传入字符串,则使用它从 FileSystemRegistry 中查找对应的文件系统。如果未提供, 文件将从 default 加载 send_as_attachment: 是否将文件作为附件发送 html_mode: 在 HTML 模式下: - 从 / 提供 index.html 文件 - 当文件未找到时提供 404.html name: 传递给生成的处理程序的名称 after_request: 传递给路由器的 after_request 处理程序 after_response: 传递给路由器的 after_response 处理程序 before_request: 传递给路由器的 before_request 处理程序 cache_control: 传递给路由器的 cache_control exception_handlers: 传递给路由器的异常处理程序 guards: 传递给路由器的守卫 include_in_schema: 是否在 OpenAPI 架构中包含路由/路由器 middleware: 传递给路由器的中间件 opt: 传递给路由器的选项 security: 传递给路由器的安全选项 tags: 传递给路由器的 tags router_class: 用于构造路由器的类 allow_symlinks_outside_directory: 允许提供链接到基础目录(在 'directories' 中指定)内部路径 到其外部路径的文件。此操作应谨慎处理,因为它允许通过符号链接链潜在地意外访问 定义 'directories' 之外的文件。 .. seealso:: usage/static-files:Handling symlinks