Return a Response to send the filename given. Arguments: filename_or_io: The filename (path) to send, remember to use :func:`safe_join`. mimetype: Mimetype to use, by default it will be guessed or revert to the DEFAULT_MIMETYPE. as_attachment: If
(
filename_or_io: FilePath | BytesIO,
mimetype: str | None = None,
as_attachment: bool = False,
attachment_filename: str | None = None,
add_etags: bool = True,
cache_timeout: int | None = None,
conditional: bool = False,
last_modified: datetime | None = None,
)
| 299 | |
| 300 | |
| 301 | async def send_file( |
| 302 | filename_or_io: FilePath | BytesIO, |
| 303 | mimetype: str | None = None, |
| 304 | as_attachment: bool = False, |
| 305 | attachment_filename: str | None = None, |
| 306 | add_etags: bool = True, |
| 307 | cache_timeout: int | None = None, |
| 308 | conditional: bool = False, |
| 309 | last_modified: datetime | None = None, |
| 310 | ) -> Response: |
| 311 | """Return a Response to send the filename given. |
| 312 | |
| 313 | Arguments: |
| 314 | filename_or_io: The filename (path) to send, remember to use |
| 315 | :func:`safe_join`. |
| 316 | mimetype: Mimetype to use, by default it will be guessed or |
| 317 | revert to the DEFAULT_MIMETYPE. |
| 318 | as_attachment: If true use the attachment filename in a |
| 319 | Content-Disposition attachment header. |
| 320 | attachment_filename: Name for the filename, if it differs |
| 321 | add_etags: Set etags based on the filename, size and |
| 322 | modification time. |
| 323 | last_modified: Used to override the last modified value. |
| 324 | cache_timeout: Time in seconds for the response to be cached. |
| 325 | |
| 326 | """ |
| 327 | file_body: ResponseBody |
| 328 | file_size: int | None = None |
| 329 | etag: str | None = None |
| 330 | if isinstance(filename_or_io, BytesIO): |
| 331 | file_body = current_app.response_class.io_body_class(filename_or_io) |
| 332 | file_size = filename_or_io.getbuffer().nbytes |
| 333 | else: |
| 334 | file_path = file_path_to_path(filename_or_io) |
| 335 | file_size = file_path.stat().st_size |
| 336 | if attachment_filename is None: |
| 337 | attachment_filename = file_path.name |
| 338 | file_body = current_app.response_class.file_body_class(file_path) |
| 339 | if last_modified is None: |
| 340 | last_modified = file_path.stat().st_mtime # type: ignore |
| 341 | if cache_timeout is None: |
| 342 | cache_timeout = current_app.get_send_file_max_age(str(file_path)) |
| 343 | etag = ( |
| 344 | f"{file_path.stat().st_mtime}-{file_path.stat().st_size}" |
| 345 | f"-{adler32(bytes(file_path))}" |
| 346 | ) |
| 347 | |
| 348 | if mimetype is None and attachment_filename is not None: |
| 349 | mimetype = mimetypes.guess_type(attachment_filename)[0] or DEFAULT_MIMETYPE |
| 350 | if mimetype is None: |
| 351 | raise ValueError( |
| 352 | "The mime type cannot be inferred, please set it manually via the" |
| 353 | " mimetype argument." |
| 354 | ) |
| 355 | |
| 356 | response = current_app.response_class(file_body, mimetype=mimetype) |
| 357 | response.content_length = file_size |
| 358 |
searching dependent graphs…