Fix upload of failed images, missing caps

This commit is contained in:
Christoph Hagen 2024-02-29 15:39:07 +01:00
parent 902c4b08c4
commit 1d992b0bd2

View File

@ -89,10 +89,14 @@ final class Database: ObservableObject {
pendingImageUploadStorage = newValue.map { cap, images in pendingImageUploadStorage = newValue.map { cap, images in
"\(cap)-\(images.map { "\($0)" }.joined(separator: ":"))" "\(cap)-\(images.map { "\($0)" }.joined(separator: ":"))"
}.joined(separator: ";") }.joined(separator: ";")
pendingImageUploadCount = newValue.values.reduce(0) { $0 + $1.count } updatePendingImageUploadCount(imageUploads: newValue)
} }
} }
private func updatePendingImageUploadCount(imageUploads: [Int: [Int]]) {
pendingImageUploadCount = imageUploads.values.reduce(0) { $0 + $1.count }
}
@Published @Published
private(set) var pendingImageUploadCount = 0 private(set) var pendingImageUploadCount = 0
@ -171,6 +175,7 @@ final class Database: ObservableObject {
ensureFolderExistence(gridStorageFolder) ensureFolderExistence(gridStorageFolder)
loadCaps() loadCaps()
updatePendingImageUploadCount(imageUploads: imageUploads)
} }
func mainImage(for cap: Int) -> Int { func mainImage(for cap: Int) -> Int {
@ -587,7 +592,7 @@ final class Database: ObservableObject {
continue continue
} }
guard await upload(imageAt: url, for: cap) else { guard await upload(imageAt: url, for: cap) else {
log("Failed to upload image \(url.lastPathComponent)") //log("Failed to upload image \(url.lastPathComponent)")
continue continue
} }
log("Uploaded image \(image) for cap \(cap)") log("Uploaded image \(image) for cap \(cap)")
@ -614,10 +619,10 @@ final class Database: ObservableObject {
log("No image data found for image \(url.lastPathComponent) (Cap \(cap))") log("No image data found for image \(url.lastPathComponent) (Cap \(cap))")
return false return false
} }
let url = serverUrl let serverUrl = serverUrl
.appendingPathComponent("image") .appendingPathComponent("image")
.appendingPathComponent("\(cap)") .appendingPathComponent("\(cap)")
var request = URLRequest(url: url) var request = URLRequest(url: serverUrl)
request.addValue(serverAuthenticationKey, forHTTPHeaderField: "key") request.addValue(serverAuthenticationKey, forHTTPHeaderField: "key")
request.httpMethod = "POST" request.httpMethod = "POST"
do { do {
@ -627,7 +632,15 @@ final class Database: ObservableObject {
return false return false
} }
guard httpResponse.statusCode == 200 else { guard httpResponse.statusCode == 200 else {
log("Failed to upload image \(url.path): Response \(httpResponse.statusCode)") if httpResponse.statusCode == 410 {
log("Missing cap for image \(url.lastPathComponent), reupload cap")
// Missing cap, force upload
DispatchQueue.main.async {
self.changedCaps.insert(cap)
}
} else {
log("Failed to upload image \(url.lastPathComponent): Response \(httpResponse.statusCode)")
}
return false return false
} }
return true return true