67 lines
1.7 KiB
Swift
67 lines
1.7 KiB
Swift
|
import Foundation
|
||
|
import AppKit
|
||
|
|
||
|
final class ImageReader {
|
||
|
|
||
|
/// The content folder where the input data is stored
|
||
|
let contentFolder: URL
|
||
|
|
||
|
private let fileUpdates: FileUpdateChecker
|
||
|
|
||
|
let runDataFolder: URL
|
||
|
|
||
|
init(in input: URL, runFolder: URL, fileUpdates: FileUpdateChecker) {
|
||
|
self.contentFolder = input
|
||
|
self.runDataFolder = runFolder
|
||
|
self.fileUpdates = fileUpdates
|
||
|
}
|
||
|
|
||
|
var numberOfFilesLoaded: Int {
|
||
|
fileUpdates.numberOfFilesLoaded
|
||
|
}
|
||
|
|
||
|
var numberOfFilesAccessed: Int {
|
||
|
fileUpdates.numberOfFilesAccessed
|
||
|
}
|
||
|
|
||
|
func loadData() -> FileUpdateChecker.LoadResult {
|
||
|
fileUpdates.loadPreviousRun(from: runDataFolder)
|
||
|
}
|
||
|
|
||
|
func writeDetectedFileChangesToDisk() -> String? {
|
||
|
fileUpdates.writeDetectedFileChanges(to: runDataFolder)
|
||
|
}
|
||
|
|
||
|
func imageHasChanged(at path: String) -> Bool {
|
||
|
fileUpdates.fileHasChanged(at: path)
|
||
|
}
|
||
|
|
||
|
func getImage(atPath path: String) -> NSImage? {
|
||
|
guard let data = getData(atPath: path) else {
|
||
|
// TODO: log.error("Failed to load file", source: path)
|
||
|
return nil
|
||
|
}
|
||
|
guard let image = NSImage(data: data) else {
|
||
|
// TODO: log.error("Failed to read image", source: path)
|
||
|
return nil
|
||
|
}
|
||
|
return image
|
||
|
}
|
||
|
|
||
|
private func getData(atPath path: String) -> Data? {
|
||
|
let url = contentFolder.appendingPathComponent(path)
|
||
|
guard url.exists else {
|
||
|
return nil
|
||
|
}
|
||
|
do {
|
||
|
let data = try Data(contentsOf: url)
|
||
|
fileUpdates.didLoad(data, at: path)
|
||
|
return data
|
||
|
} catch {
|
||
|
// TODO: log.error("Failed to read data: \(error)", source: path)
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|