From 678340efac8b6b528f25d9e436bb8bac17e1ae7e Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 13 Mar 2024 07:22:21 -0400 Subject: [PATCH] Remove processing of imageData to a file. Store imageData in image field in database. --- .../Controllers/FuelEntryV2Controller.swift | 91 ++++++++++--------- Sources/App/Migrations/CreateFuelEntry.swift | 74 +++++++++------ Sources/App/Models/FuelEntry.swift | 8 +- Sources/App/configure.swift | 1 + Sources/App/routes.swift | 6 +- 5 files changed, 102 insertions(+), 78 deletions(-) diff --git a/Sources/App/Controllers/FuelEntryV2Controller.swift b/Sources/App/Controllers/FuelEntryV2Controller.swift index 52aecd5..8ddd5cc 100644 --- a/Sources/App/Controllers/FuelEntryV2Controller.swift +++ b/Sources/App/Controllers/FuelEntryV2Controller.swift @@ -11,7 +11,7 @@ struct FuelEntryV2Controller: RouteCollection { func boot(routes: RoutesBuilder) throws { let fuelEntries = routes.grouped("fuel_entries") fuelEntries.get(use: index) - fuelEntries.get("with-images", use: getAllEntriesWithImages) +// fuelEntries.get("with-images", use: getAllEntriesWithImages) fuelEntries.post(use: create) // fuelEntries.post("upload-image", use: uploadImage) fuelEntries.group(":fuelEntryID") { fuelEntry in @@ -23,51 +23,58 @@ struct FuelEntryV2Controller: RouteCollection { try await FuelEntry.query(on: req.db).all() } - func getAllEntriesWithImages(req: Request) async throws -> [FuelEntry] { - let fuelEntries = try await FuelEntry.query(on: req.db).all() - - for var fuelEntry in fuelEntries { - if let imagePath = fuelEntry.image { - let fileURL = URL(fileURLWithPath: imagePath) - if let imageData = try? Data(contentsOf: fileURL) { - fuelEntry.imageData = imageData - } - } - } - return fuelEntries - } +// func getAllEntriesWithImages(req: Request) async throws -> [FuelEntry] { +// let fuelEntries = try await FuelEntry.query(on: req.db).all() +// +// for var fuelEntry in fuelEntries { +// if let imagePath = fuelEntry.image { +// let fileURL = URL(fileURLWithPath: imagePath) +// if let imageData = try? Data(contentsOf: fileURL) { +// fuelEntry.imageData = imageData +// } +// } +// } +// return fuelEntries +// } func create(req: Request) async throws -> [FuelEntry] { - let fuelEntries = try await req.content.decode([FuelEntry].self) - let directory = DirectoryConfiguration.detect().workingDirectory + "Images/" - - var updatedFuelEntries: [FuelEntry] = [] - - for fuelEntry in fuelEntries { - let updatedFuelEntry = try await { () async throws -> FuelEntry in - var updatedFuelEntry = fuelEntry - - if let imageData = updatedFuelEntry.imageData { - let imageFilename = "\(updatedFuelEntry.id!).png" - let tempImageURL = URL(fileURLWithPath: directory + imageFilename) - try await imageData.write(to: tempImageURL) - updatedFuelEntry.image = tempImageURL.absoluteString - } - - do { - _ = try await updatedFuelEntry.save(on: req.db) - return updatedFuelEntry - } catch { - throw Abort(.internalServerError, reason: "Error saving FuelEntry: \(error)") - } - }() - - updatedFuelEntries.append(updatedFuelEntry) - } - - return updatedFuelEntries + let fuelEntry = try req.content.decode([FuelEntry].self) + try await fuelEntry.create(on: req.db) + return fuelEntry } + //Create with image file written to server storage instead of database +// func create(req: Request) async throws -> [FuelEntry] { +// let fuelEntries = try await req.content.decode([FuelEntry].self) +// let directory = DirectoryConfiguration.detect().workingDirectory + "Images/" +// +// var updatedFuelEntries: [FuelEntry] = [] +// +// for fuelEntry in fuelEntries { +// let updatedFuelEntry = try await { () async throws -> FuelEntry in +// var updatedFuelEntry = fuelEntry +// +// if let imageData = updatedFuelEntry.imageData { +// let imageFilename = "\(updatedFuelEntry.id!).png" +// let tempImageURL = URL(fileURLWithPath: directory + imageFilename) +// try await imageData.write(to: tempImageURL) +// updatedFuelEntry.image = tempImageURL.absoluteString +// } +// +// do { +// _ = try await updatedFuelEntry.save(on: req.db) +// return updatedFuelEntry +// } catch { +// throw Abort(.internalServerError, reason: "Error saving FuelEntry: \(error)") +// } +// }() +// +// updatedFuelEntries.append(updatedFuelEntry) +// } +// +// return updatedFuelEntries +// } + func delete(req: Request) async throws -> HTTPStatus { guard let fuelEntry = try await FuelEntry.find(req.parameters.get("fuelEntryID"), on: req.db) else { throw Abort(.notFound) diff --git a/Sources/App/Migrations/CreateFuelEntry.swift b/Sources/App/Migrations/CreateFuelEntry.swift index 76de0c1..ad0151d 100755 --- a/Sources/App/Migrations/CreateFuelEntry.swift +++ b/Sources/App/Migrations/CreateFuelEntry.swift @@ -1,6 +1,6 @@ // // File.swift -// +// // // Created by Bill Ryckman on 4/26/23. // @@ -26,43 +26,59 @@ struct CreateFuelEntry: AsyncMigration { .field("is_override", .bool) .create() } - + func revert(on database: Database) async throws { try await database.schema("fuel_entries").delete() } } struct AddLatitudeAndLongitudeAndRemoveGpsLocationAndImage: AsyncMigration { - func prepare(on database: Database) async throws { - try await database.schema("fuel_entries") - .field("latitude", .double) - .field("longitude", .double) - .deleteField("gps_location") - .deleteField("image") - .update() - } - - func revert(on database: Database) async throws { - try await database.schema("fuel_entries") - .deleteField("latitude") - .deleteField("longitude") - .field("gps_location", .string) - .field("image", .string) - .update() - } + func prepare(on database: Database) async throws { + try await database.schema("fuel_entries") + .field("latitude", .double) + .field("longitude", .double) + .deleteField("gps_location") + .deleteField("image") + .update() + } + + func revert(on database: Database) async throws { + try await database.schema("fuel_entries") + .deleteField("latitude") + .deleteField("longitude") + .field("gps_location", .string) + .field("image", .string) + .update() + } } struct AddImageBackIn: AsyncMigration { - func prepare(on database: Database) async throws { - try await database.schema("fuel_entries") - .field("image", .string) - .update() - } + func prepare(on database: Database) async throws { + try await database.schema("fuel_entries") + .field("image", .string) + .update() + } + + func revert(on database: Database) async throws { + try await database.schema("fuel_entries") + .deleteField("image") + .update() + } +} - func revert(on database: Database) async throws { - try await database.schema("fuel_entries") - .deleteField("image") - .update() - } +struct StoreImageDataInDatabase: AsyncMigration { + func prepare(on database: Database) async throws { + try await database.schema("fuel_entries") + .deleteField("image") + .field("image", .data) + .update() + } + + func revert(on database: Database) async throws { + try await database.schema("fuel_entries") + .deleteField("image") + .field("image", .string) + .update() + } } diff --git a/Sources/App/Models/FuelEntry.swift b/Sources/App/Models/FuelEntry.swift index 9ae6206..9d60f10 100755 --- a/Sources/App/Models/FuelEntry.swift +++ b/Sources/App/Models/FuelEntry.swift @@ -27,7 +27,7 @@ final class FuelEntry: Model, Content { var longitude: Double @Field(key: "image") - var image: String? + var image: Data? @Field(key: "product") var product: String @@ -53,11 +53,11 @@ final class FuelEntry: Model, Content { @Field(key: "is_override") var isOverride: Bool - var imageData: Data? +// var imageData: Data? init() {} - init(id: UUID? = nil, timestamp: Date, userID: String, latitude: Double, longitude: Double, image: String?, product: String, make: String, model: String, serialNumber: String, tankID: String, fuelAmount: Double, fuelType: String, isOverride: Bool, imageData: Data?) { + init(id: UUID? = nil, timestamp: Date, userID: String, latitude: Double, longitude: Double, image: Data?, product: String, make: String, model: String, serialNumber: String, tankID: String, fuelAmount: Double, fuelType: String, isOverride: Bool) { //imageData: Data? self.id = id self.timestamp = timestamp self.userID = userID @@ -72,6 +72,6 @@ final class FuelEntry: Model, Content { self.fuelAmount = fuelAmount self.fuelType = fuelType self.isOverride = isOverride - self.imageData = imageData +// self.imageData = imageData } } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 6b67492..e454857 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -14,6 +14,7 @@ public func configure(_ app: Application) async throws { app.migrations.add(CreateFuelEntry()) app.migrations.add(AddLatitudeAndLongitudeAndRemoveGpsLocationAndImage()) app.migrations.add(AddImageBackIn()) + app.migrations.add(StoreImageDataInDatabase()) try await app.autoMigrate().get() // register routes diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 61e693e..cd36958 100755 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -23,9 +23,9 @@ func routes(_ app: Application) throws { try await fuelEntryV2Controller.index(req: req) } - v2.get("fuel-entries", "with-images") {req in - try await fuelEntryV2Controller.getAllEntriesWithImages(req: req) - } +// v2.get("fuel-entries", "with-images") {req in +// try await fuelEntryV2Controller.getAllEntriesWithImages(req: req) +// } v2.post("fuel-entries") {req in try await fuelEntryV2Controller.create(req: req)