Add function to delete an image

This commit is contained in:
Christoph Hagen 2023-01-15 16:45:07 +01:00
parent df89f4eabd
commit 267a71d78c
2 changed files with 40 additions and 8 deletions

View File

@ -184,10 +184,10 @@ final class CapServer: ServerOwner {
}
private func organizeImages() {
caps.values.sorted().forEach(organizeImage)
caps.values.sorted().forEach(organizeImages)
}
private func organizeImage(for cap: Cap) {
private func organizeImages(for cap: Cap) {
var cap = cap
guard let images = try? images(in: folder(of: cap.id)) else {
log("Failed to get image urls for cap \(cap.id)")
@ -204,7 +204,7 @@ final class CapServer: ServerOwner {
continue
}
let lastImage = sorted.popLast()!
let newUrl = file(of: cap.id, version: version)
let newUrl = imageUrl(of: cap.id, version: version)
do {
try fm.moveItem(at: lastImage.url, to: newUrl)
} catch {
@ -230,7 +230,7 @@ final class CapServer: ServerOwner {
thumbnailFolder.appendingPathComponent(String(format: "%04d.jpg", cap))
}
func file(of cap: Int, version: Int) -> URL {
func imageUrl(of cap: Int, version: Int) -> URL {
folder(of: cap).appendingPathComponent(String(format: "%04d-%02d.jpg", cap, version))
}
@ -292,11 +292,11 @@ final class CapServer: ServerOwner {
}
var id = 0
let capFolder = folder(of: cap)
var f = file(of: cap, version: id)
var f = imageUrl(of: cap, version: id)
if fm.fileExists(atPath: capFolder.path) {
while fm.fileExists(atPath: f.path) {
id += 1
f = file(of: cap, version: id)
f = imageUrl(of: cap, version: id)
}
} else {
try fm.createDirectory(at: capFolder, withIntermediateDirectories: true)
@ -370,7 +370,7 @@ final class CapServer: ServerOwner {
}
func switchMainImage(to version: Int, for cap: Int) throws {
let file2 = file(of: cap, version: version)
let file2 = imageUrl(of: cap, version: version)
guard fm.fileExists(atPath: file2.path) else {
log("No image \(version) for cap \(cap)")
throw CapError.invalidFile
@ -405,7 +405,7 @@ final class CapServer: ServerOwner {
if cap.name != "" {
updatedCap.name = cap.name
}
let url = file(of: existingCap.id, version: cap.mainImage)
let url = imageUrl(of: existingCap.id, version: cap.mainImage)
if fm.fileExists(atPath: url.path) {
updatedCap.mainImage = cap.mainImage
}
@ -416,6 +416,20 @@ final class CapServer: ServerOwner {
log("Updated cap \(existingCap.id)")
}
func deleteImage(version: Int, for capId: Int) -> Bool {
guard let cap = caps[capId] else {
return false
}
let url = imageUrl(of: capId, version: version)
guard fm.fileExists(atPath: url.path) else {
return false
}
organizeImages(for: cap)
return true
}
// MARK: Classifier
func updateTrainedClasses(content: String) {
let trainedCaps = content
.components(separatedBy: "\n")
@ -440,6 +454,8 @@ final class CapServer: ServerOwner {
log("Updated classifier to version \(version)")
}
// MARK: Grid
func getListOfMissingThumbnails() -> [Int] {
caps.keys.filter { !fm.fileExists(atPath: thumbnail(of: $0).path) }
}

View File

@ -92,6 +92,22 @@ func routes(_ app: Application) {
let data = try request.getBodyData()
server.saveThumbnail(data, for: cap)
}
// Delete the image of a cap
app.postCatching("delete", ":cap", ":version") { request in
guard let cap = request.parameters.get("cap", as: Int.self) else {
log("Invalid cap parameter for image deletion")
throw Abort(.badRequest)
}
try authorize(request)
guard let version = request.parameters.get("version", as: Int.self) else {
log("Invalid version parameter for image deletion")
throw Abort(.badRequest)
}
guard server.deleteImage(version: version, for: cap) else {
throw Abort(.gone)
}
}
}
private extension Request {