Instance Methodvapor 4.106.2Vapor
streamFile(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 streamFile(at path: String, chunkSize: Int = NonBlockingFileIO.defaultChunkSize, mediaType: HTTPMediaType? = nil, advancedETagComparison: Bool, onCompleted: @escaping (Result<Void, Error>) -> () = { _ in }) -> EventLoopFuture<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.streamFile(at: "/path/to/file.txt")
}
Other members in extension
Types
struct FileChunks
Wrapper around
NIOFileSystem.FileChunks
. This can be removed onceNIOFileSystem
reaches a stable API.
Instance members
func asyncStreamFile(at: String, chunkSize: Int, mediaType: HTTPMediaType?, advancedETagComparison: Bool, onCompleted: @escaping (Result<Void, Error>) async throws -> ()
) async throws -> Response Generates a chunked
Response
for the specified file. This method respects values in the"ETag"
header and is capable of responding304 Not Modified
if the file in question has not been modified since last served. IfadvancedETagComparison
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 appropriateMediaType
can be found for the file’s suffix.func collectFile(at: String
) async throws -> ByteBuffer Reads the contents of a file at the supplied path.
func collectFile(at: String
) -> EventLoopFuture<ByteBuffer> Reads the contents of a file at the supplied path.
func readFile(at: String, chunkSize: Int, offset: Int64?, byteCount: Int?
) async throws -> FileChunks Reads the contents of a file at the supplied path in chunks.
func readFile(at: String, chunkSize: Int, onRead: @escaping (ByteBuffer) -> EventLoopFuture<Void>
) -> EventLoopFuture<Void> Reads the contents of a file at the supplied path in chunks.
func streamFile(at: String, chunkSize: Int, mediaType: HTTPMediaType?, onCompleted: @escaping (Result<Void, Error>) -> ()
) -> Response Generates a chunked
Response
for the specified file. This method respects values in the"ETag"
header and is capable of responding304 Not Modified
if the file in question has not been modified since last served. This method will also set the"Content-Type"
header automatically if an appropriateMediaType
can be found for the file’s suffix.func writeFile(ByteBuffer, at: String
) -> EventLoopFuture<Void> Write the contents of buffer to a file at the supplied path.
func writeFile(ByteBuffer, at: String
) async throws Write the contents of buffer to a file at the supplied path.