2022-08-26 17:40:51 +02:00
|
|
|
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<String> = []
|
|
|
|
|
2022-08-29 13:33:48 +02:00
|
|
|
/**
|
|
|
|
The files marked as external in element metadata.
|
|
|
|
|
|
|
|
Files included here are not generated, since they are assumed to be added separately.
|
|
|
|
*/
|
|
|
|
private var externalFiles: Set<String> = []
|
|
|
|
|
|
|
|
/**
|
|
|
|
The files marked as expected, i.e. they exist after the generation is completed.
|
|
|
|
|
|
|
|
The key of the dictionary is the file path, the value is the file providing the link
|
|
|
|
*/
|
|
|
|
private var expectedFiles: [String : String] = [:]
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
/**
|
|
|
|
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 {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "Failed to load file", source: path)
|
2022-08-26 17:40:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard let image = NSImage(data: data) else {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "Failed to read image", source: path)
|
2022-08-26 17:40:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return (image, changed)
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
2022-08-28 14:02:13 +02:00
|
|
|
func requireImage(source: String, destination: String, width: Int, desiredHeight: Int? = nil) -> NSSize {
|
2022-08-26 17:40:51 +02:00
|
|
|
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 {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "Missing file with size (\(width),\(desiredHeight ?? -1))",
|
|
|
|
source: source)
|
2022-08-26 17:40:51 +02:00
|
|
|
return standardSize
|
|
|
|
}
|
|
|
|
guard let imageSize = loadImage(atPath: image.source)?.image.size else {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "Unreadable image with size (\(width),\(desiredHeight ?? -1))",
|
|
|
|
source: source)
|
2022-08-26 17:40:51 +02:00
|
|
|
return standardSize
|
|
|
|
}
|
|
|
|
let scaledSize = imageSize.scaledDown(to: CGFloat(width))
|
|
|
|
|
|
|
|
guard let existing = imageTasks[destination] else {
|
|
|
|
imageTasks[destination] = image
|
|
|
|
return scaledSize
|
|
|
|
}
|
|
|
|
guard existing.source == source else {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "Multiple sources (\(existing.source),\(source))",
|
|
|
|
source: destination)
|
2022-08-26 17:40:51 +02:00
|
|
|
return scaledSize
|
|
|
|
}
|
|
|
|
guard existing.hasSimilarRatio(as: image) else {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "Multiple ratios (\(existing.ratio!),\(image.ratio!))",
|
|
|
|
source: destination)
|
2022-08-26 17:40:51 +02:00
|
|
|
return scaledSize
|
|
|
|
}
|
|
|
|
if image.width > existing.width {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(info: "Increasing size from \(existing.width) to \(width)",
|
|
|
|
source: destination)
|
2022-08-26 17:40:51 +02:00
|
|
|
imageTasks[destination] = image
|
|
|
|
}
|
|
|
|
return scaledSize
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "Failed to open file", source: image.source)
|
2022-08-26 17:40:51 +02:00
|
|
|
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()
|
2022-08-29 18:57:37 +02:00
|
|
|
guard ImageType(fileExtension: ext) != nil else {
|
|
|
|
// TODO: This should never be reached, since extensions are checked before
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(info: "Copying image", source: image.source)
|
2022-08-26 17:40:51 +02:00
|
|
|
do {
|
|
|
|
let sourceUrl = input.appendingPathComponent(image.source)
|
|
|
|
try destinationUrl.ensureParentFolderExistence()
|
|
|
|
try sourceUrl.copy(to: destinationUrl)
|
|
|
|
} catch {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "Failed to copy image", source: destination)
|
2022-08-26 17:40:51 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let sourceImage = NSImage(data: sourceImageData) else {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "Failed to read file", source: image.source)
|
2022-08-26 17:40:51 +02:00
|
|
|
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 {
|
2022-08-28 11:15:59 +02:00
|
|
|
log.add(warning: "Desired width \(desiredWidth), got \(scaledSize.width)", source: destination)
|
2022-08-26 17:40:51 +02:00
|
|
|
}
|
|
|
|
if abs(destinationSize.height - scaledImage.size.height) > 2 {
|
2022-08-28 11:15:59 +02:00
|
|
|
log.add(warning: "Desired height \(destinationSize.height), got \(scaledSize.height)", source: destination)
|
2022-08-26 17:40:51 +02:00
|
|
|
}
|
|
|
|
if let desiredHeight = desiredHeight {
|
|
|
|
let desiredRatio = desiredHeight / desiredWidth
|
|
|
|
let adjustedDesiredHeight = scaledSize.width * desiredRatio
|
|
|
|
if abs(adjustedDesiredHeight - scaledSize.height) > 5 {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(warning: "Desired height \(desiredHeight), got \(scaledSize.height)", source: destination)
|
2022-08-26 17:40:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if scaledSize.width > desiredWidth {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(warning:" Desired width \(desiredWidth), got \(scaledSize.width)", source: destination)
|
2022-08-26 17:40:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let destinationExtension = destinationUrl.pathExtension.lowercased()
|
2022-08-29 18:57:37 +02:00
|
|
|
guard let type = ImageType(fileExtension: destinationExtension)?.fileType else {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "No image type for extension \(destinationExtension)",
|
|
|
|
source: destination)
|
2022-08-26 17:40:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let tiff = scaledImage.tiffRepresentation, let tiffData = NSBitmapImageRep(data: tiff) else {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "Failed to get data", source: image.source)
|
2022-08-26 17:40:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let data = tiffData.representation(using: type, properties: [.compressionFactor: NSNumber(0.7)]) else {
|
2022-08-26 22:28:34 +02:00
|
|
|
log.add(error: "Failed to get data", source: image.source)
|
2022-08-26 17:40:51 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-08-29 13:33:48 +02:00
|
|
|
/**
|
|
|
|
Mark a file as explicitly missing.
|
|
|
|
|
|
|
|
This is done for the `externalFiles` entries in metadata,
|
|
|
|
to indicate that these files will be copied to the output folder manually.
|
|
|
|
*/
|
|
|
|
func exclude(file: String) {
|
|
|
|
externalFiles.insert(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Mark a file as expected to be present in the output folder after generation.
|
|
|
|
|
|
|
|
This is done for all links between pages, which only exist after the pages have been generated.
|
|
|
|
*/
|
|
|
|
func expect(file: String, source: String) {
|
|
|
|
expectedFiles[file] = source
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
func copyRequiredFiles() {
|
|
|
|
for file in requiredFiles {
|
2022-08-29 13:33:48 +02:00
|
|
|
let cleanPath = cleanRelativeURL(file)
|
|
|
|
let sourceUrl = input.appendingPathComponent(cleanPath)
|
|
|
|
let destinationUrl = output.appendingPathComponent(cleanPath)
|
2022-08-26 17:40:51 +02:00
|
|
|
guard sourceUrl.exists else {
|
2022-08-29 13:33:48 +02:00
|
|
|
if !externalFiles.contains(file) {
|
|
|
|
log.add(error: "Missing required file", source: cleanPath)
|
|
|
|
}
|
2022-08-26 17:40:51 +02:00
|
|
|
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
|
|
|
|
}
|
2022-08-29 13:33:48 +02:00
|
|
|
writeIfChanged(data, to: destinationUrl)
|
|
|
|
}
|
|
|
|
for (file, source) in expectedFiles {
|
|
|
|
guard !externalFiles.contains(file) else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let cleanPath = cleanRelativeURL(file)
|
|
|
|
let destinationUrl = output.appendingPathComponent(cleanPath)
|
|
|
|
if !destinationUrl.exists {
|
|
|
|
log.add(error: "Missing \(cleanPath)", source: source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func cleanRelativeURL(_ raw: String) -> String {
|
|
|
|
let raw = raw.dropAfterLast("#") // Clean links to page content
|
|
|
|
guard raw.contains("..") else {
|
|
|
|
return raw
|
|
|
|
}
|
|
|
|
var result: [String] = []
|
|
|
|
for component in raw.components(separatedBy: "/") {
|
|
|
|
if component == ".." {
|
|
|
|
_ = result.popLast()
|
|
|
|
} else {
|
|
|
|
result.append(component)
|
|
|
|
}
|
2022-08-26 17:40:51 +02:00
|
|
|
}
|
2022-08-29 13:33:48 +02:00
|
|
|
return result.joined(separator: "/")
|
2022-08-26 17:40:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Writing files
|
|
|
|
|
|
|
|
@discardableResult
|
2022-08-29 13:33:48 +02:00
|
|
|
func writeIfChanged(_ data: Data, to url: URL) -> Bool {
|
2022-08-26 17:40:51 +02:00
|
|
|
// 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)!
|
2022-08-29 13:33:48 +02:00
|
|
|
return writeIfChanged(data, to: url)
|
2022-08-26 17:40:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension Digest {
|
|
|
|
|
|
|
|
var bytes: [UInt8] { Array(makeIterator()) }
|
|
|
|
|
|
|
|
var data: Data { Data(bytes) }
|
|
|
|
|
|
|
|
var hexStr: String {
|
|
|
|
bytes.map { String(format: "%02X", $0) }.joined()
|
|
|
|
}
|
|
|
|
}
|