Fix image upload path, more logging

This commit is contained in:
Christoph Hagen 2023-02-26 18:03:57 +01:00
parent 0cbddf61ab
commit 9ed0be8cc1

View File

@ -173,7 +173,7 @@ final class Database: ObservableObject {
do { do {
data = try Data(contentsOf: localDbUrl) data = try Data(contentsOf: localDbUrl)
} catch { } catch {
print("Failed to read database file: \(error)") log("Failed to read database file: \(error)")
return return
} }
do { do {
@ -182,7 +182,7 @@ final class Database: ObservableObject {
// Prevent immediate save after modifying caps // Prevent immediate save after modifying caps
nextSaveTime = nil nextSaveTime = nil
} catch { } catch {
print("Failed to decode database file: \(error)") log("Failed to decode database file: \(error)")
return return
} }
} }
@ -212,15 +212,15 @@ final class Database: ObservableObject {
do { do {
data = try encoder.encode(caps.values.sorted()) data = try encoder.encode(caps.values.sorted())
} catch { } catch {
print("Failed to encode database: \(error)") log("Failed to encode database: \(error)")
return return
} }
do { do {
try data.write(to: localDbUrl) try data.write(to: localDbUrl)
} catch { } catch {
print("Failed to save database: \(error)") log("Failed to save database: \(error)")
} }
print("Database saved") log("Database saved")
} }
@discardableResult @discardableResult
@ -241,20 +241,20 @@ final class Database: ObservableObject {
@discardableResult @discardableResult
func downloadCaps() async -> Bool { func downloadCaps() async -> Bool {
print("Downloading cap data from \(serverDbUrl)") log("Downloading cap data from \(serverDbUrl)")
let data: Data let data: Data
let response: URLResponse let response: URLResponse
do { do {
(data, response) = try await URLSession.shared.data(from: serverDbUrl) (data, response) = try await URLSession.shared.data(from: serverDbUrl)
} catch { } catch {
print("Failed to download classifier version: \(error)") log("Failed to download classifier version: \(error)")
return false return false
} }
guard let httpResponse = response as? HTTPURLResponse else { guard let httpResponse = response as? HTTPURLResponse else {
return false return false
} }
guard httpResponse.statusCode == 200 else { guard httpResponse.statusCode == 200 else {
print("Failed to download caps: \(httpResponse.statusCode)") log("Failed to download caps: \(httpResponse.statusCode)")
return false return false
} }
@ -262,10 +262,9 @@ final class Database: ObservableObject {
do { do {
capData = try decoder.decode([CapData].self, from: data) capData = try decoder.decode([CapData].self, from: data)
} catch { } catch {
print("Failed to decode server database: \(error)") log("Failed to decode server database: \(error)")
return false return false
} }
print("Downloaded \(capData) caps")
var inserts = 0 var inserts = 0
var updates = 0 var updates = 0
for cap in capData { for cap in capData {
@ -288,7 +287,7 @@ final class Database: ObservableObject {
updates += 1 updates += 1
} }
} }
print("Updated database from server (\(inserts) added, \(updates) updated)") log("Updated database from server (\(inserts) added, \(updates) updated)")
return true return true
} }
@ -299,7 +298,7 @@ final class Database: ObservableObject {
do { do {
(data, response) = try await URLSession.shared.data(from: serverClassifierVersionUrl) (data, response) = try await URLSession.shared.data(from: serverClassifierVersionUrl)
} catch { } catch {
print("Failed to download classifier version: \(error)") log("Failed to download classifier version: \(error)")
return false return false
} }
guard (response as? HTTPURLResponse)?.statusCode == 200 else { guard (response as? HTTPURLResponse)?.statusCode == 200 else {
@ -318,22 +317,22 @@ final class Database: ObservableObject {
self.serverClassifierVersion = serverVersion self.serverClassifierVersion = serverVersion
} }
guard serverVersion > self.classifierVersion else { guard serverVersion > self.classifierVersion else {
print("No new classifier available (Local: \(classifierVersion) Server: \(serverVersion))") log("No new classifier available (Local: \(classifierVersion) Server: \(serverVersion))")
return false return false
} }
print("New classifier available (Local: \(classifierVersion) Server: \(serverVersion))") log("New classifier available (Local: \(classifierVersion) Server: \(serverVersion))")
return true return true
} }
@discardableResult @discardableResult
func downloadClassifier() async -> Bool { func downloadClassifier() async -> Bool {
print("Downloading classifier") log("Downloading classifier")
let tempUrl: URL let tempUrl: URL
let response: URLResponse let response: URLResponse
do { do {
(tempUrl, response) = try await URLSession.shared.download(from: serverClassifierUrl) (tempUrl, response) = try await URLSession.shared.download(from: serverClassifierUrl)
} catch { } catch {
print("Failed to download classifier version: \(error)") log("Failed to download classifier version: \(error)")
return false return false
} }
guard (response as? HTTPURLResponse)?.statusCode == 200 else { guard (response as? HTTPURLResponse)?.statusCode == 200 else {
@ -346,14 +345,14 @@ final class Database: ObservableObject {
} }
try self.fm.moveItem(at: tempUrl, to: url) try self.fm.moveItem(at: tempUrl, to: url)
} catch { } catch {
print("Failed to replace classifier: \(error)") log("Failed to replace classifier: \(error)")
return false return false
} }
DispatchQueue.main.async { DispatchQueue.main.async {
self.classifierVersion = self.serverClassifierVersion self.classifierVersion = self.serverClassifierVersion
self.classifier = nil self.classifier = nil
} }
print("Downloaded classifier \(classifierVersion)") log("Downloaded classifier \(classifierVersion)")
return true return true
} }
@ -509,10 +508,11 @@ final class Database: ObservableObject {
return false return false
} }
guard let data = try? Data(contentsOf: url) else { guard let data = try? Data(contentsOf: url) else {
log("No image data found for image \(url.lastPathComponent) (Cap \(cap))")
return false return false
} }
let url = serverUrl let url = serverUrl
.appendingPathComponent("images") .appendingPathComponent("image")
.appendingPathComponent("\(cap)") .appendingPathComponent("\(cap)")
var request = URLRequest(url: url) var request = URLRequest(url: url)
request.addValue(serverAuthenticationKey, forHTTPHeaderField: "key") request.addValue(serverAuthenticationKey, forHTTPHeaderField: "key")
@ -524,7 +524,7 @@ final class Database: ObservableObject {
return false return false
} }
guard httpResponse.statusCode == 200 else { guard httpResponse.statusCode == 200 else {
log("Failed to upload image \(url.lastPathComponent): Response \(httpResponse.statusCode)") log("Failed to upload image \(url.path): Response \(httpResponse.statusCode)")
return false return false
} }
return true return true
@ -653,7 +653,7 @@ final class Database: ObservableObject {
.filter { $0.pathExtension == "caps" } .filter { $0.pathExtension == "caps" }
.map { $0.deletingPathExtension().lastPathComponent } .map { $0.deletingPathExtension().lastPathComponent }
} catch { } catch {
print("Failed to load available grids: \(error)") log("Failed to load available grids: \(error)")
return [] return []
} }
} }
@ -677,10 +677,10 @@ final class Database: ObservableObject {
// Add all missing caps to the end of the image // Add all missing caps to the end of the image
let newCaps = Set(caps.keys).subtracting(loaded.capPlacements).sorted() let newCaps = Set(caps.keys).subtracting(loaded.capPlacements).sorted()
loaded.capPlacements += newCaps loaded.capPlacements += newCaps
print("Grid \(grid) loaded (\(newCaps.count) new caps)") log("Grid \(grid) loaded (\(newCaps.count) new caps)")
return loaded return loaded
} catch { } catch {
print("Failed to load grid \(grid): \(error)") log("Failed to load grid \(grid): \(error)")
return nil return nil
} }
} }
@ -691,10 +691,10 @@ final class Database: ObservableObject {
do { do {
let data = try encoder.encode(grid) let data = try encoder.encode(grid)
try data.write(to: url) try data.write(to: url)
print("Grid \(name) saved") log("Grid \(name) saved")
return true return true
} catch { } catch {
print("Failed to save grid \(name): \(error)") log("Failed to save grid \(name): \(error)")
return false return false
} }
} }
@ -716,7 +716,7 @@ final class Database: ObservableObject {
try data.write(to: url) try data.write(to: url)
return true return true
} catch { } catch {
print("Failed to save grid image \(grid): \(error)") log("Failed to save grid image \(grid): \(error)")
return false return false
} }
} }