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 {
/**
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 {

View File

@ -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