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