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

adds draft file upload example #19

Merged
merged 5 commits into from Nov 29, 2021
Merged

Conversation

mcritz
Copy link
Contributor

@mcritz mcritz commented Oct 29, 2021

A practical demo of how and why one may use async await.

@mcritz
Copy link
Contributor Author

mcritz commented Oct 29, 2021

  • Write test
  • Perhaps a stream file to download example?

@adam-fowler
Copy link
Member

Your example immediately make it obvious we don't have async/await support for HBFileIO. I'll add that. Then your route can be

try await request.fileIO.writeFile(
    contents: request.body,
    path: fileURL.path,
    context: request.context,
    logger: request.logger
)
return "Uploaded as \(fileName)"

Adding a download route as well would be cool

@adam-fowler
Copy link
Member

@mcritz
Copy link
Contributor Author

mcritz commented Nov 1, 2021

@adam-fowler I was hoping for a minor tag on hummingbird/hummingbird so I can update the Package without using .branch("main").

@adam-fowler
Copy link
Member

@mcritz mcritz marked this pull request as ready for review November 4, 2021 17:10
@mcritz
Copy link
Contributor Author

mcritz commented Nov 4, 2021

I think this example is sufficiently simple.

Copy link
Member

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

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

It would be good to include a download options as well to round the example off.
If you could do the file upload/download via a web page served by the server that'd be even better

path: fileURL.path,
context: request.context,
logger: request.logger)
return "Uploaded as \(fileName)"
Copy link
Member

Choose a reason for hiding this comment

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

Maybe return as JSON {"filename": "\(filename)"}. You'd want to do that with the JSONEncoder as well, so you get the correct content-type header, so set application.encoder = JSONEncoder() and then return

struct UploadResponse: ResponseEncodable {
    let filename: String
}
return UploadResponse(filename: "\(fileName)")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

import HummingbirdFoundation

extension HBRequest {
var fileIO: HBFileIO { HBFileIO(application: self.application) }
Copy link
Member

Choose a reason for hiding this comment

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

Is this really necessary? I'm sure it would be more obvious what you are doing if you just create an HBFileIO in the route handler

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

Copy link
Member

Choose a reason for hiding this comment

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

Hey @mcritz I'm not seeing any of these changes

return "Uploaded as \(fileName)"
}

private var tempDirectory: URL = FileManager.default.temporaryDirectory
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth having this save to a local folder, so files are persistent between runs

@mcritz
Copy link
Contributor Author

mcritz commented Nov 27, 2021

@adam-fowler Sorry for the delays. The challenges of parenting and American holidays. I have updated presently.

@adam-fowler
Copy link
Member

@mcritz dont worry. Especially at this time your family should be priority

Copy link
Member

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

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

Looks good
There are a couple of minor typos in the readme

- In this example, we allow arbitrary uploads without a `File-Name` header and substitute a UUID instead.
- In this example, we configure the route’s ability to overwrite an existing filename with the same filename. Developers should consider abstracting filenames from users entirely. A good practice is to assign UUID based filenames and store associated metadata in a database.

Also noteworthy is that we’ve updaed the Application.configure
Copy link
Member

Choose a reason for hiding this comment

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

updated

upload-async/README.md Show resolved Hide resolved
@mcritz
Copy link
Contributor Author

mcritz commented Nov 29, 2021

@adam-fowler Updated!

@adam-fowler adam-fowler merged commit 8bdb16d into hummingbird-project:main Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants