From 23859d9d6872ec68145cf119e1c1f6056c1bc391 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Fri, 24 Jun 2022 11:31:09 +0200 Subject: [PATCH] Increase body size --- Sources/App/Error.swift | 29 +++++++++++++++++++++++++++++ Sources/App/routes.swift | 12 +++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Sources/App/Error.swift b/Sources/App/Error.swift index 6e93861..55ad49c 100644 --- a/Sources/App/Error.swift +++ b/Sources/App/Error.swift @@ -10,11 +10,40 @@ import Vapor enum CapError: Error { + /** + + HTTP Code: 404 + */ case unknownId + + /** + + HTTP Code: 400 + */ case invalidBody + + /** + + HTTP Code: 409 + */ case dataInconsistency + + /** + + HTTP Code: 412 + */ case invalidFile + + /** + + HTTP Code: 500 + */ case invalidConfiguration + + /** + + HTTP Code: 406 + */ case invalidData var response: HTTPResponseStatus { diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index c8ef39a..819310b 100755 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -1,14 +1,15 @@ import Vapor +import Foundation /// The decoder to extract caps from JSON payloads given to the `cap` route. private let decoder = JSONDecoder() private func authorize(_ request: Request) throws { guard let key = request.headers.first(name: "key") else { - throw Abort(.badRequest) + throw Abort(.badRequest) // 400 } guard server.hasAuthorization(for: key) else { - throw Abort(.forbidden) + throw Abort(.forbidden) // 403 } } @@ -45,21 +46,22 @@ func routes(_ app: Application) throws { } // Update the classifier - app.postCatching("classifier", ":version") { request in + app.on(.POST, "classifier", ":version", body: .collect(maxSize: "50mb")) { request -> HTTPStatus in try authorize(request) guard let version = request.parameters.get("version", as: Int.self) else { log("Invalid parameter for version") throw Abort(.badRequest) } guard version > server.classifierVersion else { - throw Abort(.alreadyReported) + throw Abort(.alreadyReported) // 208 } guard let buffer = request.body.data else { log("Missing body data: \(request.body.description)") throw CapError.invalidBody } let data = Data(buffer: buffer) - try server.save(image: data, for: version) + try server.save(classifier: data, version: version) + return .ok } // Update the trained classes