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
Add FileIO.writeFile #2418
Conversation
/// - returns: `Future` that will complete when the file write is finished. | ||
public func write(to path: String, buffer: ByteBuffer) -> EventLoopFuture<Void> { | ||
do { | ||
let fd = try NIOFileHandle(path: path, mode: .write, flags: .allowFileCreation()) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to adding this. My only concern here is naming.
The other methods on FileIO
are named:
collectFile(at:)
readFile(at:)
streamFile(at:)
This newly added method write(to:buffer:)
doesn't really fit.
Given that the method creates a new file, what would you think about:
writeFile(_ data: ByteBuffer, at path: String) -> ELFuture<Void>
@tanner0101 What do you think about appending? |
@t-ae we can always add an append api in the future, I think this is good for now :) Would you mind including a code example in the PR body? These will auto gen release notes, more info here: https://github.com/vapor/vapor/blob/master/.github/contributing.md#releases |
Thanks! |
These changes are now available in 4.24.0 |
Adds
writeFile
method to request'sFileIO
helper (#2418).