asyncStreamFile(at:chunkSize:mediaType:advancedETagComparison:onCompleted:)
Generates a chunked Response
for the specified file. This method respects values in the "ETag"
header and is capable of responding 304 Not Modified
if the file in question has not been modified since last served. If advancedETagComparison
is set to true, the response will have its ETag field set to a byte-by-byte hash of the requested file. If set to false, a simple ETag consisting of the last modified date and file size will be used. This method will also set the "Content-Type"
header automatically if an appropriate MediaType
can be found for the file’s suffix.
func asyncStreamFile(at path: String, chunkSize: Int = NonBlockingFileIO.defaultChunkSize, mediaType: HTTPMediaType? = nil, advancedETagComparison: Bool = false, onCompleted: @escaping (Result<Void, Error>) async throws -> () = { _ in }) async throws -> Response
Parameters
- path
Path to file on the disk.
- chunkSize
Maximum size for the file data chunks.
- mediaType
HTTPMediaType, if not specified, will be created from file extension.
- advancedETagComparison
The method used when ETags are generated. If true, a byte-by-byte hash is created (and cached), otherwise a simple comparison based on the file’s last modified date and size.
- onCompleted
Closure to be run on completion of stream.
Returns
A 200 OK
response containing the file stream and appropriate headers.
app.get("file-stream") { req in
return req.fileio.asyncStreamFile(at: "/path/to/file.txt")
}
Async equivalent of streamFile(at:chunkSize:mediaType:advancedETagComparison:onCompleted:)
using Swift Concurrency functions under the hood