2026-06-07 21:52:49 -07:00
|
|
|
import Foundation
|
|
|
|
|
|
2026-06-08 22:04:22 -07:00
|
|
|
/// JSON snapshot cache of the library at `~/.local/state/tv-anarchy/library.json`.
|
2026-06-07 21:52:49 -07:00
|
|
|
/// A plain Codable file (a few-hundred-show library fits in memory trivially) —
|
|
|
|
|
/// the offline-browsable source of truth when ~/media / black are unreachable.
|
|
|
|
|
public enum LibraryStore {
|
2026-06-09 05:34:39 -07:00
|
|
|
public static func snapshotURL() -> URL { StatePaths.url("library.json") }
|
2026-06-07 21:52:49 -07:00
|
|
|
|
|
|
|
|
public static func load() -> LibrarySnapshot? {
|
|
|
|
|
guard let data = try? Data(contentsOf: snapshotURL()) else { return nil }
|
|
|
|
|
let dec = JSONDecoder()
|
|
|
|
|
dec.dateDecodingStrategy = .iso8601
|
|
|
|
|
return try? dec.decode(LibrarySnapshot.self, from: data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static func save(_ snapshot: LibrarySnapshot) {
|
|
|
|
|
let url = snapshotURL()
|
|
|
|
|
try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(),
|
|
|
|
|
withIntermediateDirectories: true)
|
|
|
|
|
let enc = JSONEncoder()
|
|
|
|
|
enc.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
|
|
|
|
|
enc.dateEncodingStrategy = .iso8601
|
|
|
|
|
guard let data = try? enc.encode(snapshot) else { return }
|
|
|
|
|
try? data.write(to: url, options: .atomic)
|
|
|
|
|
}
|
|
|
|
|
}
|