|
|
|
@@ -13,7 +13,7 @@ struct FuelEntryV2Controller: RouteCollection { |
|
|
|
fuelEntries.get(use: index) |
|
|
|
fuelEntries.get("with-images", use: getAllEntriesWithImages) |
|
|
|
fuelEntries.post(use: create) |
|
|
|
fuelEntries.post("upload-image", use: uploadImage) |
|
|
|
// fuelEntries.post("upload-image", use: uploadImage) |
|
|
|
fuelEntries.group(":fuelEntryID") { fuelEntry in |
|
|
|
fuelEntry.delete(use: delete) |
|
|
|
} |
|
|
|
@@ -76,30 +76,30 @@ struct FuelEntryV2Controller: RouteCollection { |
|
|
|
return .noContent |
|
|
|
} |
|
|
|
|
|
|
|
func uploadImage(req: Request) async throws -> FuelEntry { |
|
|
|
let fuelEntryID: UUID = try req.parameters.require("id") |
|
|
|
|
|
|
|
let file = try await req.content.decode(File.self) |
|
|
|
let filename = fuelEntryID.uuidString + ".png" // Generate a unique filename for the uploaded image |
|
|
|
let directory = DirectoryConfiguration.detect().workingDirectory + "Images/" |
|
|
|
|
|
|
|
if let imageData = file.data.getData(at: 0, length: file.data.readableBytes) { |
|
|
|
do { |
|
|
|
try Data(imageData).write(to: URL(fileURLWithPath: directory + filename)) // Save the uploaded image to server |
|
|
|
} catch { |
|
|
|
throw Abort(.internalServerError, reason: "Failed to save image file") |
|
|
|
} |
|
|
|
|
|
|
|
// Update FuelEntry model after saving the image |
|
|
|
if let fuelEntry = try await FuelEntry.find(fuelEntryID, on: req.db) { |
|
|
|
return fuelEntry |
|
|
|
} else { |
|
|
|
throw Abort(.notFound, reason: "FuelEntry not found") |
|
|
|
} |
|
|
|
} else { |
|
|
|
throw Abort(.badRequest, reason: "Failed to extract image data") |
|
|
|
} |
|
|
|
} |
|
|
|
// func uploadImage(req: Request) async throws -> FuelEntry { |
|
|
|
// let fuelEntryID: UUID = try req.parameters.require("id") |
|
|
|
// |
|
|
|
// let file = try await req.content.decode(File.self) |
|
|
|
// let filename = fuelEntryID.uuidString + ".png" // Generate a unique filename for the uploaded image |
|
|
|
// let directory = DirectoryConfiguration.detect().workingDirectory + "Images/" |
|
|
|
// |
|
|
|
// if let imageData = file.data.getData(at: 0, length: file.data.readableBytes) { |
|
|
|
// do { |
|
|
|
// try Data(imageData).write(to: URL(fileURLWithPath: directory + filename)) // Save the uploaded image to server |
|
|
|
// } catch { |
|
|
|
// throw Abort(.internalServerError, reason: "Failed to save image file") |
|
|
|
// } |
|
|
|
// |
|
|
|
// // Update FuelEntry model after saving the image |
|
|
|
// if let fuelEntry = try await FuelEntry.find(fuelEntryID, on: req.db) { |
|
|
|
// return fuelEntry |
|
|
|
// } else { |
|
|
|
// throw Abort(.notFound, reason: "FuelEntry not found") |
|
|
|
// } |
|
|
|
// } else { |
|
|
|
// throw Abort(.badRequest, reason: "Failed to extract image data") |
|
|
|
// } |
|
|
|
// } |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|