Increase body size

This commit is contained in:
Christoph Hagen 2022-06-24 11:31:09 +02:00
parent b707c27734
commit 23859d9d68
2 changed files with 36 additions and 5 deletions

View File

@ -10,11 +10,40 @@ import Vapor
enum CapError: Error { enum CapError: Error {
/**
HTTP Code: 404
*/
case unknownId case unknownId
/**
HTTP Code: 400
*/
case invalidBody case invalidBody
/**
HTTP Code: 409
*/
case dataInconsistency case dataInconsistency
/**
HTTP Code: 412
*/
case invalidFile case invalidFile
/**
HTTP Code: 500
*/
case invalidConfiguration case invalidConfiguration
/**
HTTP Code: 406
*/
case invalidData case invalidData
var response: HTTPResponseStatus { var response: HTTPResponseStatus {

View File

@ -1,14 +1,15 @@
import Vapor import Vapor
import Foundation
/// The decoder to extract caps from JSON payloads given to the `cap` route. /// The decoder to extract caps from JSON payloads given to the `cap` route.
private let decoder = JSONDecoder() private let decoder = JSONDecoder()
private func authorize(_ request: Request) throws { private func authorize(_ request: Request) throws {
guard let key = request.headers.first(name: "key") else { guard let key = request.headers.first(name: "key") else {
throw Abort(.badRequest) throw Abort(.badRequest) // 400
} }
guard server.hasAuthorization(for: key) else { 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 // Update the classifier
app.postCatching("classifier", ":version") { request in app.on(.POST, "classifier", ":version", body: .collect(maxSize: "50mb")) { request -> HTTPStatus in
try authorize(request) try authorize(request)
guard let version = request.parameters.get("version", as: Int.self) else { guard let version = request.parameters.get("version", as: Int.self) else {
log("Invalid parameter for version") log("Invalid parameter for version")
throw Abort(.badRequest) throw Abort(.badRequest)
} }
guard version > server.classifierVersion else { guard version > server.classifierVersion else {
throw Abort(.alreadyReported) throw Abort(.alreadyReported) // 208
} }
guard let buffer = request.body.data else { guard let buffer = request.body.data else {
log("Missing body data: \(request.body.description)") log("Missing body data: \(request.body.description)")
throw CapError.invalidBody throw CapError.invalidBody
} }
let data = Data(buffer: buffer) 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 // Update the trained classes