diff --git a/Sources/App/CapServer.swift b/Sources/App/CapServer.swift index e8bb13c..bc694e4 100644 --- a/Sources/App/CapServer.swift +++ b/Sources/App/CapServer.swift @@ -120,7 +120,7 @@ final class CapServer { } private func loadClassifierVersion(at url: URL) { - guard fm.fileExists(atPath: url.path) else { + guard exists(url) else { return } let content: String @@ -206,10 +206,28 @@ final class CapServer { private func organizeImages() { caps.values.sorted().forEach(organizeImages) } + + private func createImageFolder(for cap: Int) throws { + let folderUrl = folder(of: cap) + do { + try fm.createDirectory(at: folderUrl, withIntermediateDirectories: true) + } catch { + log("Failed to create folder for cap \(cap): \(error)") + throw error + } + } private func organizeImages(for cap: Cap) { var cap = cap - guard let images = try? images(in: folder(of: cap.id)) else { + let folderUrl = folder(of: cap.id) + guard exists(folderUrl) else { + try? createImageFolder(for: cap.id) + cap.count = 0 + caps[cap.id] = cap + return + } + + guard let images = try? images(in: folderUrl) else { log("Failed to get image urls for cap \(cap.id)") return } @@ -254,6 +272,10 @@ final class CapServer { folder(of: cap).appendingPathComponent(String(format: "%04d-%02d.jpg", cap, version)) } + private func exists(_ url: URL) -> Bool { + fm.fileExists(atPath: url.path) + } + // MARK: Counts private func images(in folder: URL) throws -> [URL] { @@ -265,11 +287,11 @@ final class CapServer { Get the image count of a cap. */ func count(of cap: Int) throws -> Int { - let f = folder(of: cap) - guard fm.fileExists(atPath: f.path) else { + let capImageFolder = folder(of: cap) + guard exists(capImageFolder) else { return 0 } - return try images(in: f).count + return try images(in: capImageFolder).count } // MARK: Images @@ -288,16 +310,16 @@ final class CapServer { } var id = 0 let capFolder = folder(of: cap) - var f = imageUrl(of: cap, version: id) - if fm.fileExists(atPath: capFolder.path) { - while fm.fileExists(atPath: f.path) { + var capImageUrl = imageUrl(of: cap, version: id) + if exists(capFolder) { + while exists(capImageUrl) { id += 1 - f = imageUrl(of: cap, version: id) + capImageUrl = imageUrl(of: cap, version: id) } } else { - try fm.createDirectory(at: capFolder, withIntermediateDirectories: true) + try createImageFolder(for: cap) } - try data.write(to: f) + try data.write(to: capImageUrl) caps[cap]!.count = try count(of: cap) addChangedImageToLog(cap: cap, image: id) log("Added image \(id) for cap \(cap)") @@ -332,7 +354,7 @@ final class CapServer { } private func ensureExistenceOfChangedImagesFile() throws { - guard !fm.fileExists(atPath: changedImagesFile.path) else { + if exists(changedImagesFile) { return } do { @@ -366,8 +388,8 @@ final class CapServer { } func switchMainImage(to version: Int, for cap: Int) throws { - let file2 = imageUrl(of: cap, version: version) - guard fm.fileExists(atPath: file2.path) else { + let capImageUrl = imageUrl(of: cap, version: version) + guard exists(capImageUrl) else { log("No image \(version) for cap \(cap)") throw CapError.invalidFile } @@ -401,8 +423,8 @@ final class CapServer { if cap.name != "" { updatedCap.name = cap.name } - let url = imageUrl(of: existingCap.id, version: cap.mainImage) - if fm.fileExists(atPath: url.path) { + let capImageUrl = imageUrl(of: existingCap.id, version: cap.mainImage) + if exists(capImageUrl) { updatedCap.mainImage = cap.mainImage } if let color = cap.color { @@ -416,8 +438,8 @@ final class CapServer { guard let cap = caps[capId] else { return false } - let url = imageUrl(of: capId, version: version) - guard fm.fileExists(atPath: url.path) else { + let capImageUrl = imageUrl(of: capId, version: version) + guard exists(capImageUrl) else { return false } organizeImages(for: cap) @@ -453,7 +475,7 @@ final class CapServer { // MARK: Grid func getListOfMissingThumbnails() -> [Int] { - caps.keys.filter { !fm.fileExists(atPath: thumbnail(of: $0).path) } + caps.keys.filter { !exists(thumbnail(of: $0)) } } func saveThumbnail(_ data: Data, for cap: Int) {