Create missing image folders

This commit is contained in:
Christoph Hagen 2023-02-17 11:39:32 +01:00
parent 623a226816
commit cf3d5d0169

View File

@ -120,7 +120,7 @@ final class CapServer {
} }
private func loadClassifierVersion(at url: URL) { private func loadClassifierVersion(at url: URL) {
guard fm.fileExists(atPath: url.path) else { guard exists(url) else {
return return
} }
let content: String let content: String
@ -207,9 +207,27 @@ final class CapServer {
caps.values.sorted().forEach(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) { private func organizeImages(for cap: Cap) {
var 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)") log("Failed to get image urls for cap \(cap.id)")
return return
} }
@ -254,6 +272,10 @@ final class CapServer {
folder(of: cap).appendingPathComponent(String(format: "%04d-%02d.jpg", cap, version)) folder(of: cap).appendingPathComponent(String(format: "%04d-%02d.jpg", cap, version))
} }
private func exists(_ url: URL) -> Bool {
fm.fileExists(atPath: url.path)
}
// MARK: Counts // MARK: Counts
private func images(in folder: URL) throws -> [URL] { private func images(in folder: URL) throws -> [URL] {
@ -265,11 +287,11 @@ final class CapServer {
Get the image count of a cap. Get the image count of a cap.
*/ */
func count(of cap: Int) throws -> Int { func count(of cap: Int) throws -> Int {
let f = folder(of: cap) let capImageFolder = folder(of: cap)
guard fm.fileExists(atPath: f.path) else { guard exists(capImageFolder) else {
return 0 return 0
} }
return try images(in: f).count return try images(in: capImageFolder).count
} }
// MARK: Images // MARK: Images
@ -288,16 +310,16 @@ final class CapServer {
} }
var id = 0 var id = 0
let capFolder = folder(of: cap) let capFolder = folder(of: cap)
var f = imageUrl(of: cap, version: id) var capImageUrl = imageUrl(of: cap, version: id)
if fm.fileExists(atPath: capFolder.path) { if exists(capFolder) {
while fm.fileExists(atPath: f.path) { while exists(capImageUrl) {
id += 1 id += 1
f = imageUrl(of: cap, version: id) capImageUrl = imageUrl(of: cap, version: id)
} }
} else { } 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) caps[cap]!.count = try count(of: cap)
addChangedImageToLog(cap: cap, image: id) addChangedImageToLog(cap: cap, image: id)
log("Added image \(id) for cap \(cap)") log("Added image \(id) for cap \(cap)")
@ -332,7 +354,7 @@ final class CapServer {
} }
private func ensureExistenceOfChangedImagesFile() throws { private func ensureExistenceOfChangedImagesFile() throws {
guard !fm.fileExists(atPath: changedImagesFile.path) else { if exists(changedImagesFile) {
return return
} }
do { do {
@ -366,8 +388,8 @@ final class CapServer {
} }
func switchMainImage(to version: Int, for cap: Int) throws { func switchMainImage(to version: Int, for cap: Int) throws {
let file2 = imageUrl(of: cap, version: version) let capImageUrl = imageUrl(of: cap, version: version)
guard fm.fileExists(atPath: file2.path) else { guard exists(capImageUrl) else {
log("No image \(version) for cap \(cap)") log("No image \(version) for cap \(cap)")
throw CapError.invalidFile throw CapError.invalidFile
} }
@ -401,8 +423,8 @@ final class CapServer {
if cap.name != "" { if cap.name != "" {
updatedCap.name = cap.name updatedCap.name = cap.name
} }
let url = imageUrl(of: existingCap.id, version: cap.mainImage) let capImageUrl = imageUrl(of: existingCap.id, version: cap.mainImage)
if fm.fileExists(atPath: url.path) { if exists(capImageUrl) {
updatedCap.mainImage = cap.mainImage updatedCap.mainImage = cap.mainImage
} }
if let color = cap.color { if let color = cap.color {
@ -416,8 +438,8 @@ final class CapServer {
guard let cap = caps[capId] else { guard let cap = caps[capId] else {
return false return false
} }
let url = imageUrl(of: capId, version: version) let capImageUrl = imageUrl(of: capId, version: version)
guard fm.fileExists(atPath: url.path) else { guard exists(capImageUrl) else {
return false return false
} }
organizeImages(for: cap) organizeImages(for: cap)
@ -453,7 +475,7 @@ final class CapServer {
// MARK: Grid // MARK: Grid
func getListOfMissingThumbnails() -> [Int] { 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) { func saveThumbnail(_ data: Data, for cap: Int) {