import Foundation import CryptoKit import AppKit typealias SourceFile = (data: Data, didChange: Bool) typealias SourceTextFile = (content: String, didChange: Bool) final class FileSystem { private static let hashesFileName = "hashes.json" private let input: URL private let output: URL private let source = "FileChangeMonitor" private var hashesFile: URL { input.appendingPathComponent(FileSystem.hashesFileName) } /** The hashes of all accessed files from the previous run The key is the relative path to the file from the source */ private var previousFiles: [String : Data] = [:] /** The paths of all files which were accessed, with their new hashes This list is used to check if a file was modified, and to write all accessed files back to disk */ private var accessedFiles: [String : Data] = [:] /** All files which should be copied to the output folder */ private var requiredFiles: Set = [] /** The image creation tasks. The key is the destination path. */ private var imageTasks: [String : ImageOutput] = [:] init(in input: URL, to output: URL) { self.input = input self.output = output guard exists(hashesFile) else { log.add(info: "No file hashes loaded, regarding all content as new", source: source) return } let data: Data do { data = try Data(contentsOf: hashesFile) } catch { log.add( warning: "File hashes could not be read, regarding all content as new", source: source, error: error) return } do { self.previousFiles = try JSONDecoder().decode(from: data) } catch { log.add( warning: "File hashes could not be decoded, regarding all content as new", source: source, error: error) return } } func urlInOutputFolder(_ path: String) -> URL { output.appendingPathComponent(path) } func urlInContentFolder(_ path: String) -> URL { input.appendingPathComponent(path) } /** Get the current hash of file data at a path. If the hash has been computed previously during the current run, then this function directly returns it. */ private func hash(_ data: Data, at path: String) -> Data { accessedFiles[path] ?? SHA256.hash(data: data).data } private func exists(_ url: URL) -> Bool { FileManager.default.fileExists(atPath: url.path) } func dataOfRequiredFile(atPath path: String, source: String) -> Data? { let url = input.appendingPathComponent(path) guard exists(url) else { log.failedToOpen(path, requiredBy: source, error: nil) return nil } do { return try Data(contentsOf: url) } catch { log.failedToOpen(path, requiredBy: source, error: error) return nil } } func dataOfOptionalFile(atPath path: String, source: String) -> Data? { let url = input.appendingPathComponent(path) guard exists(url) else { return nil } do { return try Data(contentsOf: url) } catch { log.failedToOpen(path, requiredBy: source, error: error) return nil } } func contentOfOptionalFile(atPath path: String, source: String) -> String? { let url = input.appendingPathComponent(path) guard exists(url) else { return nil } do { return try String(contentsOf: url) } catch { log.failedToOpen(path, requiredBy: source, error: error) return nil } } private func getData(atPath path: String) -> SourceFile? { let url = input.appendingPathComponent(path) guard exists(url) else { return nil } let data: Data do { data = try Data(contentsOf: url) } catch { log.add(error: "Failed to read data at \(path)", source: source, error: error) return nil } let newHash = hash(data, at: path) defer { accessedFiles[path] = newHash } guard let oldHash = previousFiles[path] else { return (data: data, didChange: true) } return (data: data, didChange: oldHash != newHash) } func writeHashes() { do { let encoder = JSONEncoder() encoder.outputFormatting = .prettyPrinted let data = try encoder.encode(accessedFiles) try data.write(to: hashesFile) } catch { log.add(warning: "Failed to save file hashes", source: source, error: error) } } // MARK: Images private func loadImage(atPath path: String) -> (image: NSImage, changed: Bool)? { guard let (data, changed) = getData(atPath: path) else { print("Failed to load image data \(path)") return nil } guard let image = NSImage(data: data) else { print("Failed to read image \(path)") return nil } return (image, changed) } @discardableResult func requireImage(source: String, destination: String, width: Int, desiredHeight: Int? = nil, createDoubleVersion: Bool = false) -> NSSize { let height = desiredHeight.unwrapped(CGFloat.init) let sourceUrl = input.appendingPathComponent(source) let image = ImageOutput(source: source, width: width, desiredHeight: desiredHeight) let standardSize = NSSize(width: CGFloat(width), height: height ?? CGFloat(width) / 16 * 9) guard sourceUrl.exists else { log.add(error: "Missing image \(source) with size (\(width),\(desiredHeight ?? -1)", source: "Image Processor") return standardSize } guard let imageSize = loadImage(atPath: image.source)?.image.size else { log.add(error: "Unreadable image \(source) with size (\(width),\(desiredHeight ?? -1)", source: "Image Processor") return standardSize } let scaledSize = imageSize.scaledDown(to: CGFloat(width)) guard let existing = imageTasks[destination] else { //print("Image(\(image.width),\(image.desiredHeight ?? -1)) requested for \(destination)") imageTasks[destination] = image return scaledSize } guard existing.source == source else { log.add(error: "Multiple sources (\(existing.source),\(source)) for image \(destination)", source: "Image Processor") return scaledSize } guard existing.hasSimilarRatio(as: image) else { log.add(error: "Multiple ratios (\(existing.ratio!),\(image.ratio!)) for image \(destination)", source: "Image Processor") return scaledSize } if image.width > existing.width { log.add(info: "Increasing size of image \(destination) from \(existing.width) to \(width)", source: "Image Processor") imageTasks[destination] = image } return scaledSize } #warning("Implement image functions") func createImages() { for (destination, image) in imageTasks.sorted(by: { $0.key < $1.key }) { createImageIfNeeded(image, for: destination) } } private func createImageIfNeeded(_ image: ImageOutput, for destination: String) { guard let (sourceImageData, sourceImageChanged) = getData(atPath: image.source) else { log.add(error: "Failed to open image \(image.source)", source: "Image Processor") return } let destinationUrl = output.appendingPathComponent(destination) // Check if image needs to be updated guard !destinationUrl.exists || sourceImageChanged else { return } // Ensure that image file is supported let ext = destinationUrl.pathExtension.lowercased() guard MediaType.isProcessableImage(ext) else { log.add(info: "Copying image \(image.source)", source: "Image Processor") do { let sourceUrl = input.appendingPathComponent(image.source) try destinationUrl.ensureParentFolderExistence() try sourceUrl.copy(to: destinationUrl) } catch { log.add(error: "Failed to copy image \(image.source) to \(destination)", source: "Image Processor") } return } guard let sourceImage = NSImage(data: sourceImageData) else { print("Failed to read image \(image.source)") return } let desiredWidth = CGFloat(image.width) let desiredHeight = image.desiredHeight.unwrapped(CGFloat.init) let destinationSize = sourceImage.size.scaledDown(to: desiredWidth) let scaledImage = sourceImage.scaledDown(to: destinationSize) let scaledSize = scaledImage.size if abs(scaledImage.size.width - desiredWidth) > 2 { print("[WARN] Image \(destination) scaled incorrectly (wanted width \(desiredWidth), is \(scaledSize.width))") } if abs(destinationSize.height - scaledImage.size.height) > 2 { print("[WARN] Image \(destination) scaled incorrectly (wanted height \(destinationSize.height), is \(scaledSize.height))") } if let desiredHeight = desiredHeight { let desiredRatio = desiredHeight / desiredWidth let adjustedDesiredHeight = scaledSize.width * desiredRatio if abs(adjustedDesiredHeight - scaledSize.height) > 5 { log.add(warning: "Image \(image.source): Desired height \(adjustedDesiredHeight) (actually \(desiredHeight)), got \(scaledSize.height) after reduction", source: "Image Processor") return } } if scaledSize.width > desiredWidth { print("[WARN] Image \(image.source) is too large (expected width \(desiredWidth), got \(scaledSize.width)") } let destinationExtension = destinationUrl.pathExtension.lowercased() guard let type = MediaType.supportedImage(destinationExtension) else { log.add(error: "No image type for \(destination) with extension \(destinationExtension)", source: "Image Processor") return } guard let tiff = scaledImage.tiffRepresentation, let tiffData = NSBitmapImageRep(data: tiff) else { log.add(error: "Failed to get data for image \(image.source)", source: "Image Processor") return } guard let data = tiffData.representation(using: type, properties: [.compressionFactor: NSNumber(0.7)]) else { log.add(error: "Failed to get data for image \(image.source)", source: "Image Processor") return } do { try data.createFolderAndWrite(to: destinationUrl) } catch { log.add(error: "Failed to write image \(destination)", source: "Image Processor", error: error) return } } // MARK: File copying /** Add a file as required, so that it will be copied to the output directory. */ func require(file: String) { requiredFiles.insert(file) } func copyRequiredFiles() { var missingFiles = [String]() for file in requiredFiles { let sourceUrl = input.appendingPathComponent(file) guard sourceUrl.exists else { missingFiles.append(file) continue } let data: Data do { data = try Data(contentsOf: sourceUrl) } catch { log.add(error: "Failed to read data at \(sourceUrl.path)", source: source, error: error) continue } let destinationUrl = output.appendingPathComponent(file) write(data, to: destinationUrl) } } // MARK: Writing files @discardableResult func write(_ data: Data, to url: URL) -> Bool { // Only write changed files if url.exists, let oldContent = try? Data(contentsOf: url), data == oldContent { return false } do { try data.createFolderAndWrite(to: url) return true } catch { log.add(error: "Failed to write file", source: url.path, error: error) return false } } @discardableResult func write(_ string: String, to url: URL) -> Bool { let data = string.data(using: .utf8)! return write(data, to: url) } } private extension Digest { var bytes: [UInt8] { Array(makeIterator()) } var data: Data { Data(bytes) } var hexStr: String { bytes.map { String(format: "%02X", $0) }.joined() } }