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

Add FileIO.writeFile #2418

merged 7 commits into from Jul 17, 2020

Conversation

t-ae
Copy link
Contributor

@t-ae t-ae commented Jun 30, 2020

Adds writeFile method to request's FileIO helper (#2418).

// Writes "hello" to /path/to/file.txt
// Returns a future indicating when the write has completed. 
req.fileio.writeFile(.init(string: "hello"), at: "/path/to/file.txt")

/// - 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())
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.

Copy link
Member

@tanner0101 tanner0101 left a 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 tanner0101 added the enhancement New feature or request label Jul 14, 2020
@tanner0101 tanner0101 added this to Awaiting Review in Vapor 4 via automation Jul 14, 2020
@tanner0101 tanner0101 moved this from Awaiting Review to Awaiting Updates in Vapor 4 Jul 14, 2020
@t-ae
Copy link
Contributor Author

t-ae commented Jul 16, 2020

@tanner0101
Changed the name as you suggested.

What do you think about appending?
#2418 (comment)
FileIO is just a utility class and users can append files by using NonBlockingFileIO directly.
So I think FileIO doesn't have to have appending method.

Tests/VaporTests/FileTests.swift Outdated Show resolved Hide resolved
@tanner0101
Copy link
Member

@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

@tanner0101 tanner0101 added the semver-minor Contains new API label Jul 16, 2020
@tanner0101 tanner0101 changed the title Add FileIO.write Add FileIO.writeFile Jul 17, 2020
@tanner0101
Copy link
Member

Thanks!

@tanner0101 tanner0101 merged commit 562aca7 into vapor:master Jul 17, 2020
Vapor 4 automation moved this from Awaiting Updates to Done Jul 17, 2020
@tanner0101
Copy link
Member

These changes are now available in 4.24.0

@t-ae t-ae deleted the fileio-write branch July 17, 2020 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request semver-minor Contains new API
Projects
Vapor 4
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants