From 0d3a8dbe6e22048c181c02332df0836046e3ed22 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Sat, 14 Jan 2023 23:09:03 +0100 Subject: [PATCH] ensure that image changes are always accessible --- Sources/App/CapServer.swift | 21 ++++++++++++++------- Sources/App/routes.swift | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Sources/App/CapServer.swift b/Sources/App/CapServer.swift index 6c18a83..7bb17ce 100644 --- a/Sources/App/CapServer.swift +++ b/Sources/App/CapServer.swift @@ -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)") } } diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 5103ce5..b104d5b 100755 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -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() } }