Skip to content

Commit

Permalink
Update upload
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Mar 13, 2024
1 parent d9002e3 commit eb1741f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion todos-mongokitten-openapi/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription

let package = Package(
name: "todos-mongokitten-openapi",
platforms: [ .macOS(.v14) ],
platforms: [.macOS(.v14)],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", branch: "2.x.x"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.0.0"),
Expand Down
6 changes: 3 additions & 3 deletions upload/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import Hummingbird
import Logging
import NIOCore

func buildApplication(args: AppArguments) -> some HBApplicationProtocol {
let router = HBRouter()
func buildApplication(args: AppArguments) -> some ApplicationProtocol {
let router = Router()
FileController().addRoutes(to: router.group("files"))
return HBApplication(router: router)
return Application(router: router)
}
12 changes: 6 additions & 6 deletions upload/Sources/App/Controllers/FileController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import Hummingbird

/// Handles file transfers
struct FileController {
let fileIO = HBFileIO()
let fileIO = FileIO()

func addRoutes(to group: HBRouterGroup<some HBRequestContext>) {
func addRoutes(to group: RouterGroup<some RequestContext>) {
group.get(":filename", use: self.download)
group.post("/", use: self.upload)
}
Expand All @@ -36,7 +36,7 @@ struct FileController {
/// then that name will be used as the file name on disk, otherwise
/// a UUID will be used.
/// - Returns: A JSONEncoded ``UploadModel``
@Sendable private func upload(_ request: HBRequest, context: some HBRequestContext) async throws -> UploadModel {
@Sendable private func upload(_ request: Request, context: some RequestContext) async throws -> UploadModel {
let fileName = fileName(for: request)

let uploadModel = UploadModel(filename: fileName)
Expand All @@ -58,15 +58,15 @@ struct FileController {
/// - Returns: HBResponse of chunked bytes if success
/// Note that this download has no login checks and allows anyone to download
/// by its filename alone.
@Sendable private func download(_ request: HBRequest, context: some HBRequestContext) async throws -> HBResponse {
@Sendable private func download(_ request: Request, context: some RequestContext) async throws -> Response {
let filename = try context.parameters.require("filename", as: String.self)
let uploadModel = UploadModel(filename: filename)
let uploadURL = try uploadModel.destinationURL(allowsOverwrite: true)
let body = try await self.fileIO.loadFile(
path: uploadURL.path,
context: context
)
return HBResponse(
return Response(
status: .ok,
headers: self.headers(for: filename),
body: body
Expand All @@ -89,7 +89,7 @@ extension FileController {
return UUID().uuidString.appending(ext)
}

private func fileName(for request: HBRequest) -> String {
private func fileName(for request: Request) -> String {
guard let fileName = request.headers[.fileName] else {
return self.uuidFileName()
}
Expand Down
4 changes: 2 additions & 2 deletions upload/Sources/App/Models/UploadModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Hummingbird
/// A good practice might be to extend this
/// in a way that can be stored a persistable database
/// See ``todos-fluent`` for an example of using databases
struct UploadModel: HBResponseCodable {
struct UploadModel: ResponseCodable {
let filename: String
}

Expand All @@ -29,7 +29,7 @@ extension UploadModel {

guard allowsOverwrite == false else { return fileURL }
guard FileManager.default.fileExists(atPath: fileURL.path) == false else {
throw HBHTTPError(.conflict)
throw HTTPError(.conflict)
}
return fileURL
}
Expand Down

0 comments on commit eb1741f

Please sign in to comment.