FileIO

public struct FileIO

FileIO is a convenience wrapper around SwiftNIO’s NonBlockingFileIO.

It can read files, both in their entirety and chunked.

let data = try req.fileio().read(file: "/path/to/file.txt").wait()
print(data) // file data

It can also create HTTP chunked streams for use as HTTP bodies.

router.get("file-stream") { req -> HTTPResponse in
    let stream = try req.fileio().chunkedStream(file: "/path/to/file.txt")
    var res = HTTPResponse(status: .ok, body: stream)
    res.contentType = .plainText
    return res
}

Use Request.fileio() to create one.

  • Reads the contents of a file at the supplied path.

    let data = try req.fileio().read(file: "/path/to/file.txt").wait()
    print(data) // file data
    

    Declaration

    Swift

    public func read(file: String) -> Future<Data>

    Parameters

    file

    Path to file on the disk.

    Return Value

    Future containing the file data.

  • Reads the contents of a file at the supplied path in chunks.

    try req.fileio().readChunked(file: "/path/to/file.txt") { chunk in
        print("chunk: \(data)")
    }.wait()
    

    Declaration

    Swift

    public func readChunked(file: String, chunkSize: Int = NonBlockingFileIO.defaultChunkSize, onRead: @escaping (Data) -> Void) -> Future<Void>

    Parameters

    file

    Path to file on the disk.

    chunkSize

    Maximum size for the file data chunks.

    onRead

    Closure to be called sequentially for each file data chunk.

    Return Value

    Future that will complete when the file read is finished.

  • Generates a chunked HTTPResponse 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. This method will also set the "Content-Type" header automatically if an appropriate MediaType can be found for the file’s suffix.

    router.get("file-stream") { req -> HTTPResponse in
        return try req.fileio().chunkedResponse(file: "/path/to/file.txt")
    }
    

    Declaration

    Swift

    public func chunkedResponse(file: String, for req: HTTPRequest, chunkSize: Int = NonBlockingFileIO.defaultChunkSize) -> HTTPResponse

    Parameters

    file

    Path to file on the disk.

    req

    HTTPRequest to parse "If-None-Match" header from.

    chunkSize

    Maximum size for the file data chunks.

    Return Value

    A 200 OK response containing the file stream and appropriate headers.

  • Reads the contents of a file at the supplied path into an HTTPChunkedStream.

    router.get("file-stream") { req -> HTTPResponse in
        let stream = try req.fileio().chunkedStream(file: "/path/to/file.txt")
        var res = HTTPResponse(status: .ok, body: stream)
        res.contentType = .plainText
        return res
    }
    

    Declaration

    Swift

    public func chunkedStream(file: String, chunkSize: Int = NonBlockingFileIO.defaultChunkSize) -> HTTPChunkedStream

    Parameters

    file

    Path to file on the disk.

    chunkSize

    Maximum size for the file data chunks.

    Return Value

    An HTTPChunkedStream containing the file stream.