Compare commits

..

2 Commits

Author SHA1 Message Date
Christoph Hagen
e7a54ec8ec Log cap count when adding image 2023-03-13 11:10:38 +01:00
Christoph Hagen
b094762297 Allow deletion of caps 2023-03-13 10:41:24 +01:00
2 changed files with 49 additions and 2 deletions

View File

@ -120,6 +120,19 @@ extension CapServer {
} }
return try encoder.encode(cap) return try encoder.encode(cap)
} }
// Delete a cap completely, with all images
app.postCatching("delete", ":cap") { request in
guard let cap = request.parameters.get("cap", as: Int.self) else {
log("/delete/:cap/: Invalid 'cap' parameter for image deletion")
throw Abort(.badRequest)
}
try self.ensureOperability()
try authenticator.authorize(request)
guard self.delete(cap: cap) else {
throw Abort(.gone)
}
}
} }
} }

View File

@ -358,9 +358,10 @@ final class CapServer {
log("Failed to write image \(id) for cap \(cap): \(error)") log("Failed to write image \(id) for cap \(cap): \(error)")
throw CapError.invalidFile throw CapError.invalidFile
} }
caps[cap]!.count = try count(of: cap) let count = try count(of: cap)
caps[cap]!.count = count
addChangedImageToLog(cap: cap, image: id) addChangedImageToLog(cap: cap, image: id)
log("Added image \(id) for cap \(cap)") log("Added image \(id) for cap \(cap) (\(count) total)")
} }
private func writeChangedImagesToDisk() throws { private func writeChangedImagesToDisk() throws {
@ -505,6 +506,39 @@ final class CapServer {
return caps[capId]! return caps[capId]!
} }
func delete(cap capId: Int) -> Bool {
guard caps[capId] != nil else {
log("Attempting to delete unknown cap \(capId)")
return false
}
// 1. Remove all images
do {
let imageFolderUrl = folder(of: capId)
if exists(imageFolderUrl) {
try fm.removeItem(at: imageFolderUrl)
}
} catch {
log("Failed to delete image folder of cap \(capId): \(error)")
return false
}
// 2. Remove thumbnail
do {
let url = thumbnail(of: capId)
if exists(url) {
try fm.removeItem(at: url)
}
} catch {
log("Failed to delete thumbnail of cap \(capId): \(error)")
return false
}
// 3. Remove cap
caps[capId] = nil
saveCapCountHTML()
updateGridCapCount()
return true
}
// MARK: Classifier // MARK: Classifier
func updateTrainedClasses(content: String) { func updateTrainedClasses(content: String) {