静态文件¶
- class litestar.static_files.StaticFiles¶
基类:
objectASGI 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-dispositionheader ofattachmentorinlineresolve_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.
- 参数:
- 返回:
A tuple with an optional resolved
Pathinstance and an optionalstat_result.
- class litestar.static_files.StaticFilesConfig¶
基类:
objectConfiguration for static file service.
To enable static files, pass an instance of this class to the
Litestarconstructor using the 'static_files_config' key.- html_mode: bool = False¶
Flag dictating whether serving html.
If true, the default file will be 'index.html'.
- file_system: Any = <litestar.file_system.BaseLocalFileSystem object>¶
The file_system spec to use for serving files.
备注
- A file_system is a class that adheres to the
FileSystemProtocol.
- You can use any of the file systems exported from the
[fsspec](https://filesystem-spec.readthedocs.io/en/latest/) library for this purpose.
- 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¶
- exception_handlers: ExceptionHandlersMap | None = None¶
A dictionary that maps handler functions to status codes and/or exception types.
- to_static_files_app() ASGIRouteHandler¶
Return an ASGI app serving static files based on the config.
- 返回:
- 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: 用于加载文件的文件系统。
BaseFileSystem、fsspec.spec.AbstractFileSystem、fsspec.asyn.AsyncFileSystem的实例将直接使用。如果传入字符串,则使用它从FileSystemRegistry中查找对应的文件系统。如果未提供, 文件将从default加载 send_as_attachment: 是否将文件作为附件发送 html_mode: 在 HTML 模式下: - 从/提供index.html文件 - 当文件未找到时提供404.htmlname: 传递给生成的处理程序的名称 after_request: 传递给路由器的after_request处理程序 after_response: 传递给路由器的after_response处理程序 before_request: 传递给路由器的before_request处理程序 cache_control: 传递给路由器的cache_controlexception_handlers: 传递给路由器的异常处理程序 guards: 传递给路由器的守卫 include_in_schema: 是否在 OpenAPI 架构中包含路由/路由器 middleware: 传递给路由器的中间件 opt: 传递给路由器的选项 security: 传递给路由器的安全选项 tags: 传递给路由器的tagsrouter_class: 用于构造路由器的类 allow_symlinks_outside_directory: 允许提供链接到基础目录(在 'directories' 中指定)内部路径 到其外部路径的文件。此操作应谨慎处理,因为它允许通过符号链接链潜在地意外访问 定义 'directories' 之外的文件。 .. seealso:: usage/static-files:Handling symlinks