Files
lichtkasten/Sources/Lichtkasten/Services/Persistence.swift
T

82 lines
3.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Foundation
import LichtkastenCore
enum KeyScheme: String, Codable, CaseIterable {
case lightroom = "Lightroom (0–5 Sterne, 6–9 Farben)"
case photoMechanic = "Photo Mechanic (1–8 Farbklassen, ⌥1–5 Sterne)"
}
struct LightroomSettings: Codable, Hashable {
var developPreset = ""
var collection = "Lichtkasten"
var exportFolder = ""
var jpegQuality = 90
var longEdge = 0
var sharpening = "none"
var colorSpace = "sRGB"
var rewriteIPTCAfterExport = true
/// Upload targets after export: FTP destination ids and/or "immich".
var autoUploadTargets: [String] = []
}
struct AppSettings: Codable {
var exiftoolPath = ""
var dateFormat = "dd.MM.yyyy"
var keyScheme: KeyScheme = .lightroom
var thumbnailSize: Double = 200
var autoAdvance = true // after rating/tagging jump to next image (PM: "Auto-advance")
var ftpDestinations: [FTPDestination] = []
var immich = ImmichSettings()
var lightroom = LightroomSettings()
var ingest = IngestSettings()
var recentFolders: [String] = []
var favoriteFolders: [String] = []
var lastTemplateID: UUID?
init() {}
// Tolerant decoding so new settings never wipe old ones.
init(from decoder: Decoder) throws {
let c = try decoder.container(keyedBy: CodingKeys.self)
let d = AppSettings()
exiftoolPath = (try? c.decode(String.self, forKey: .exiftoolPath)) ?? d.exiftoolPath
dateFormat = (try? c.decode(String.self, forKey: .dateFormat)) ?? d.dateFormat
keyScheme = (try? c.decode(KeyScheme.self, forKey: .keyScheme)) ?? d.keyScheme
thumbnailSize = (try? c.decode(Double.self, forKey: .thumbnailSize)) ?? d.thumbnailSize
autoAdvance = (try? c.decode(Bool.self, forKey: .autoAdvance)) ?? d.autoAdvance
ftpDestinations = (try? c.decode([FTPDestination].self, forKey: .ftpDestinations)) ?? d.ftpDestinations
immich = (try? c.decode(ImmichSettings.self, forKey: .immich)) ?? d.immich
lightroom = (try? c.decode(LightroomSettings.self, forKey: .lightroom)) ?? d.lightroom
ingest = (try? c.decode(IngestSettings.self, forKey: .ingest)) ?? d.ingest
recentFolders = (try? c.decode([String].self, forKey: .recentFolders)) ?? d.recentFolders
favoriteFolders = (try? c.decode([String].self, forKey: .favoriteFolders)) ?? d.favoriteFolders
lastTemplateID = try? c.decode(UUID.self, forKey: .lastTemplateID)
}
}
enum Storage {
static var appSupport: URL {
let u = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
.appendingPathComponent("Lichtkasten", isDirectory: true)
try? FileManager.default.createDirectory(at: u, withIntermediateDirectories: true)
return u
}
static var settingsURL: URL { appSupport.appendingPathComponent("settings.json") }
static var templatesURL: URL { appSupport.appendingPathComponent("templates.json") }
static var codesURL: URL { appSupport.appendingPathComponent("codes.json") }
static func load<T: Decodable>(_ type: T.Type, from url: URL) -> T? {
guard let d = try? Data(contentsOf: url) else { return nil }
let dec = JSONDecoder()
dec.dateDecodingStrategy = .iso8601
return try? dec.decode(T.self, from: d)
}
static func save<T: Encodable>(_ value: T, to url: URL) {
let enc = JSONEncoder()
enc.outputFormatting = [.prettyPrinted, .sortedKeys]
enc.dateEncodingStrategy = .iso8601
if let d = try? enc.encode(value) { try? d.write(to: url, options: .atomic) }
}
}