|
@@ -68,7 +68,7 @@ class App(FastAPI):
|
|
|
globals.server.should_exit = True
|
|
|
|
|
|
def add_static_files(self, url_path: str, local_directory: str) -> None:
|
|
|
- """Add static files.
|
|
|
+ """Add directory of static files.
|
|
|
|
|
|
`add_static_files()` makes a local directory available at the specified endpoint, e.g. `'/static'`.
|
|
|
This is useful for providing local data like images to the frontend.
|
|
@@ -83,6 +83,15 @@ class App(FastAPI):
|
|
|
globals.app.mount(url_path, StaticFiles(directory=local_directory))
|
|
|
|
|
|
def add_static_file(self, local_file: Union[str, Path], url_path: Optional[str] = None) -> None:
|
|
|
+ """Add single static file.
|
|
|
+
|
|
|
+ Allows a local file to be accessed online with enabled caching.
|
|
|
+ If `url_path` is not specified, a random path will be generated.
|
|
|
+
|
|
|
+ :param local_file: local file to serve as media content
|
|
|
+ :param url_path: string that starts with a slash "/" and identifies the path at which the file should be served (default: None -> random path)
|
|
|
+ :return: url_path which can be used to access the file
|
|
|
+ """
|
|
|
file = Path(local_file)
|
|
|
if not file.is_file():
|
|
|
raise ValueError(f'File not found: {file}')
|
|
@@ -98,9 +107,15 @@ class App(FastAPI):
|
|
|
return url_path
|
|
|
|
|
|
def add_media_files(self, url_path: str, local_directory: Union[str, Path]) -> None:
|
|
|
- """Add media files.
|
|
|
+ """Add directory of media files.
|
|
|
|
|
|
`add_media_files()` allows a local files to be streamed from a specified endpoint, e.g. `'/media'`.
|
|
|
+ This should be used for media files to support proper streaming.
|
|
|
+ Otherwise the browser would not be able to access and load the the files incrementally or jump to different positions in the stream.
|
|
|
+ Do only put non-security-critical files in there, as they are accessible to everyone.
|
|
|
+
|
|
|
+ :param url_path: string that starts with a slash "/" and identifies the path at which the files should be served
|
|
|
+ :param local_directory: local folder with files to serve as media content
|
|
|
"""
|
|
|
@self.get(f'{url_path}/' + '{filename}')
|
|
|
async def read_item(request: Request, filename: str) -> StreamingResponse:
|
|
@@ -110,6 +125,15 @@ class App(FastAPI):
|
|
|
return helpers.get_streaming_response(filepath, request)
|
|
|
|
|
|
def add_media_file(self, local_file: Union[str, Path], url_path: Optional[str] = None) -> None:
|
|
|
+ """Add a single media file.
|
|
|
+
|
|
|
+ Allows a local file to be streamed.
|
|
|
+ If `url_path` is not specified, a random path will be generated.
|
|
|
+
|
|
|
+ :param local_file: local file to serve as media content
|
|
|
+ :param url_path: string that starts with a slash "/" and identifies the path at which the file should be served (default: None -> random path)
|
|
|
+ :return: url_path which can be used to access the file
|
|
|
+ """
|
|
|
file = Path(local_file)
|
|
|
if not file.is_file():
|
|
|
raise ValueError(f'File not found: {local_file}')
|