Static files¶
To serve static files (i.e., serve arbitrary files from a given directory), the
create_static_files_router()
can be used to create a
Router
to handle this task.
create_static_files_router
¶from pathlib import Path
from litestar import Litestar
from litestar.static_files import create_static_files_router
ASSETS_DIR = Path("assets")
def on_startup():
ASSETS_DIR.mkdir(exist_ok=True)
ASSETS_DIR.joinpath("hello.txt").write_text("Hello, world!")
app = Litestar(
route_handlers=[
create_static_files_router(path="/static", directories=["assets"]),
],
on_startup=[on_startup],
)
Run it
> curl http://127.0.0.1:8000/static/hello.txt
Hello, world!
In this example, files from the directory assets
will be served on the path
/static
. A file assets/hello.txt
would now be available on /static/hello.txt
Attention
Directories are interpreted as relative to the working directory from which the application is started
Sending files as attachments¶
By default, files are sent “inline”, meaning they will have a
Content-Disposition: inline
header.
Setting send_as_attachment
to True
will send
them with a Content-Disposition: attachment
instead:
send_as_attachment
parameter
of create_static_files_router()
¶from litestar import Litestar
from litestar.static_files import create_static_files_router
app = Litestar(
route_handlers=[
create_static_files_router(
path="/static",
directories=["assets"],
send_as_attachment=True,
)
]
)
HTML mode¶
“HTML mode” can be enabled by setting html_mode
to True
.
This will:
Serve and
/index.html
when the path/
is requestedAttempt to serve
/404.html
when a requested file is not found
html_mode
parameter of create_static_files_router()
¶from pathlib import Path
from litestar import Litestar
from litestar.static_files import create_static_files_router
HTML_DIR = Path("html")
def on_startup() -> None:
HTML_DIR.mkdir(exist_ok=True)
HTML_DIR.joinpath("index.html").write_text("<strong>Hello, world!</strong>")
HTML_DIR.joinpath("404.html").write_text("<h1>Not found</h1>")
app = Litestar(
route_handlers=[
create_static_files_router(
path="/",
directories=["html"],
html_mode=True,
)
],
on_startup=[on_startup],
)
Run it
> curl http://127.0.0.1:8000/
<strong>Hello, world!</strong>
> curl http://127.0.0.1:8000/index.html
<strong>Hello, world!</strong>
> curl http://127.0.0.1:8000/something
<h1>Not found</h1>
Passing options to the generated router¶
Options available on Router
can be passed to directly
create_static_files_router()
:
create_static_files_router()
¶from litestar import Litestar
from litestar.static_files import create_static_files_router
app = Litestar(
route_handlers=[
create_static_files_router(
path="/",
directories=["assets"],
opt={"some": True},
include_in_schema=False,
tags=["static"],
)
]
)
Using a custom router class¶
The router class used can be customized with the
router_class
parameter:
create_static_files_router()
¶from litestar import Litestar
from litestar.router import Router
from litestar.static_files import create_static_files_router
class MyRouter(Router):
pass
app = Litestar(
route_handlers=[
create_static_files_router(
path="/static",
directories=["assets"],
router_class=MyRouter,
)
]
)
Retrieving paths to static files¶
route_reverse()
and
url_for()
can be used to retrieve the path
under which a specific file will be available:
route_reverse()
¶from litestar import Litestar
from litestar.static_files import create_static_files_router
app = Litestar(
route_handlers=[
create_static_files_router(path="/static", directories=["assets"]),
]
)
print(app.route_reverse(name="static", file_path="/some_file.txt")) # /static/some_file.txt
Tip
The name
parameter has to match the name
parameter passed to
create_static_files_router()
, which defaults to static
.
(Remote) file systems¶
To customize how Litestar interacts with the file system, a class implementing the
BaseFileSystem
or any
fsspec file system can be passed
to file_system
, providing integrations for FTP, SFTP, Hadoop, SMB, GitHub and
many more,
with support for popular cloud providers available via 3rd party implementations such as
create_static_files_router()
¶from fsspec.implementations.ftp import FTPFileSystem
from litestar import Litestar
from litestar.static_files import create_static_files_router
app = Litestar(
route_handlers=[
create_static_files_router(
path="/static",
directories=["assets"],
file_system=FTPFileSystem(host="127.0.0.1"),
),
]
)
Handling symlinks¶
Local file systems (e.g. Litestar’s BaseLocalFileSystem
or fsspec.implementations.local.LocalFileSystem
) may support symlinks, which
can cause unexpected behaviour.
The allow_symlinks_outside_directory
parameter controls whether symbolic links
inside the configured base directories can point to locations outside those directories.
By default, this setting is disabled to ensure strict access control and prevent
unintended exposure of files outside the specified directories.
Danger
Keep this option disabled (default) unless absolutely necessary, to prevent accidental exposure of files outside the intended directories.
It should only be enabled in controlled environments where symlink behavior is well understood.
Security considerations¶
Enabling this option introduces potential security risks, as it may allow access to files that were not intended to be served. Improperly configured symlinks could allow to traverse directory boundaries and expose sensitive information.
This option should only be enabled if all symlinks in the base directory are known and it is ensured that proper file system permissions are in place to prevent unintended exposure.
Behavior based on file system support¶
The behavior of allow_symlinks_outside_directory
depends on the underlying
file system’s symlink capabilities:
- File systems that do not support symlinks
If the configured
file_system
does not inherit fromLinkableFileSystem
, settingallow_symlinks_outside_directory
to any value other thanNone
will raise aTypeError
. This ensures that unsupported file systems do not inadvertently allow symlink traversal.- File systems that support symlinks
If the file system supports symlinking, the default value is
False
. This means that, unless explicitly enabled, symlinks will be restricted to paths inside the defined base directories.