ensure that image changes are always accessible

This commit is contained in:
Christoph Hagen 2023-01-14 23:09:03 +01:00
parent c983cbc1b3
commit 0d3a8dbe6e
2 changed files with 15 additions and 8 deletions

View File

@ -84,6 +84,7 @@ final class CapServer: ServerOwner {
try loadCaps()
try updateCounts()
saveCapCountHTML()
try ensureExistenceOfChangedImagesFile()
}
private func loadClassifierVersion(at url: URL) {
@ -257,9 +258,6 @@ final class CapServer: ServerOwner {
guard !unwrittenImageChanges.isEmpty else {
return
}
if !fm.fileExists(atPath: changedImagesFile.path) {
try Data().write(to: changedImagesFile)
}
let handle = try FileHandle(forWritingTo: changedImagesFile)
try handle.seekToEnd()
@ -286,14 +284,23 @@ final class CapServer: ServerOwner {
}
}
func deleteChangedImageListFile() {
guard fm.fileExists(atPath: changedImagesFile.path) else {
private func ensureExistenceOfChangedImagesFile() throws {
guard !fm.fileExists(atPath: changedImagesFile.path) else {
return
}
do {
try fm.removeItem(at: changedImagesFile)
try Data().write(to: changedImagesFile)
} catch {
log("Failed to delete changed images file: \(error)")
log("Failed to create changed images file: \(error)")
throw error
}
}
func emptyChangedImageListFile() {
do {
try Data().write(to: changedImagesFile)
} catch {
log("Failed to empty changed images file: \(error)")
}
}

View File

@ -82,6 +82,6 @@ func routes(_ app: Application) {
// Reset the changed images list
app.postCatching("changes") { request in
try authorize(request)
server.deleteChangedImageListFile()
server.emptyChangedImageListFile()
}
}