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
struct FileChunksWrapper aroundNIOFileSystem.FileChunks. This can be removed onceNIOFileSystemreaches a stable API.
Instance Methods
func asyncStreamFile(at: String, chunkSize: Int, mediaType: HTTPMediaType?, advancedETagComparison: Bool, onCompleted: (Result<Void, any Error>) async throws -> ()) async throws -> ResponseGenerates a chunkedResponsefor the specified file. This method respects values in the"ETag"header and is capable of responding304 Not Modifiedif the file in question has not been modified since last served. IfadvancedETagComparisonis 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 appropriateMediaTypecan be found for the file’s suffix.func collectFile(at:)Reads the contents of a file at the supplied path.func readFile(at: String, chunkSize: Int, offset: Int64?, byteCount: Int?) async throws -> FileIO.FileChunksReads the contents of a file at the supplied path in chunks.func readFile(at: String, chunkSize: Int, onRead: (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?, advancedETagComparison: Bool, onCompleted: (Result<Void, any Error>) -> ()) -> EventLoopFuture<Response>Generates a chunkedResponsefor the specified file. This method respects values in the"ETag"header and is capable of responding304 Not Modifiedif the file in question has not been modified since last served. IfadvancedETagComparisonis 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 appropriateMediaTypecan be found for the file’s suffix.func streamFile(at: String, chunkSize: Int, mediaType: HTTPMediaType?, onCompleted: (Result<Void, any Error>) -> ()) -> ResponseGenerates a chunkedResponsefor the specified file. This method respects values in the"ETag"header and is capable of responding304 Not Modifiedif the file in question has not been modified since last served. This method will also set the"Content-Type"header automatically if an appropriateMediaTypecan be found for the file’s suffix.func writeFile(_:at:)Write the contents of buffer to a file at the supplied path.
Relationships
Conforms To
Swift.SendableSwift.SendableMetatype