Allow deletion of caps

This commit is contained in:
Christoph Hagen 2023-03-13 10:41:24 +01:00
parent 6e5cc06d31
commit b094762297
2 changed files with 46 additions and 0 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

@ -504,6 +504,39 @@ final class CapServer {
organizeImages(for: cap) organizeImages(for: cap)
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