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.

    FileIO.swift:244
    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 once NIOFileSystem reaches a stable API.

    Instance members