Skip to content

Structure

FileIO

FileIO is a convenience wrapper around SwiftNIO’s NonBlockingFileIO.
struct FileIO

Overview

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

req.fileio.readFile(at: "/path/to/file.txt") { chunk in
    print(chunk) // part of file
}

req.fileio.collectFile(at: "/path/to/file.txt").map { file in
    print(file) // entire file
}

It can also create streaming HTTP responses.

app.get("file-stream") { req -> Response in
    return req.fileio.streamFile(at: "/path/to/file.txt", for: req)
}

Streaming file responses respect E-Tag headers present in the request.

Topics

Structures

  • FileIO.FileChunks
    Wrapper around NIOFileSystem.FileChunks. This can be removed once NIOFileSystem reaches a stable API.

Instance Methods

  • 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.
  • collectFile(at:)
    Reads the contents of a file at the supplied path.
  • readFile(at:chunkSize:offset:byteCount:)
    Reads the contents of a file at the supplied path in chunks.
  • readFile(at:chunkSize:onRead:)
    Reads the contents of a file at the supplied path in chunks.
  • 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.
  • streamFile(at:chunkSize:mediaType: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. This method will also set the "Content-Type" header automatically if an appropriate MediaType can be found for the file’s suffix.
  • writeFile(_:at:)
    Write the contents of buffer to a file at the supplied path.

Relationships

Conforms To

  • Swift.Sendable
  • Swift.SendableMetatype