Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FileIO.writeFile #2418

Merged
merged 7 commits into from Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions Sources/Vapor/Utilities/FileIO.swift
Expand Up @@ -186,4 +186,26 @@ public struct FileIO {
return self.request.eventLoop.makeFailedFuture(error)
}
}

/// Write the contents of buffer to a file at the supplied path.
///
/// let data = ByteBuffer(string: "ByteBuffer")
/// try req.fileio.writeFile(data, at: "/path/to/file.txt").wait()
///
/// - parameters:
/// - path: Path to file on the disk.
/// - buffer: The `ByteBuffer` to write.
/// - returns: `Future` that will complete when the file write is finished.
public func writeFile(_ buffer: ByteBuffer, at path: String) -> EventLoopFuture<Void> {
do {
let fd = try NIOFileHandle(path: path, mode: .write, flags: .allowFileCreation())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, this is a useful addition! I just wonder what should happen to existing files. Would they append to or overwrite the existing file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method overwrites target file.
For appending, we have to pass O_APPEND flag to NIOFileHandle.

How about to add FileIO.write(fileHandle: NIOFileHandle, buffer: ByteBuffer)?
User can create NIOFileHandle with O_APPEND flag and pass it.
The merit of this method is that user can control when to close fileHandle.
It would be useful especially when appending to single file many times.

let done = io.write(fileHandle: fd, buffer: buffer, eventLoop: self.request.eventLoop)
done.whenComplete { _ in
try? fd.close()
}
return done
} catch {
return self.request.eventLoop.makeFailedFuture(error)
}
}
}
16 changes: 16 additions & 0 deletions Tests/VaporTests/FileTests.swift
Expand Up @@ -32,6 +32,22 @@ final class FileTests: XCTestCase {
XCTAssertContains(res.body.string, test)
}
}

func testFileWrite() throws {
let app = Application(.testing)
defer { app.shutdown() }

let request = Request(application: app, on: app.eventLoopGroup.next())

let data = "Hello"
let path = "/tmp/fileio_write.txt"

try request.fileio.writeFile(ByteBuffer(string: data), at: path).wait()
defer { try? FileManager.default.removeItem(atPath: path) }

let result = try String(contentsOfFile: path)
XCTAssertEqual(result, data)
}

func testPercentDecodedFilePath() throws {
let app = Application(.testing)
Expand Down