Sources/OrgApp/CaptureInbox.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Sources/OrgApp/CaptureInbox.swift history · blame · raw

67 lines · 2675 bytes

8 symbols in this file
 1import Foundation
 2
 3/// Captures shared from other apps, waiting for the app to file them: `org-protocol` capture
 4/// links in the app group's container, which the share extension writes. The app keeps its
 5/// template list there for the extension to offer.
 6public struct CaptureInbox: Sendable {
 7    public static let group = "group.sh.krz.orgstar"
 8
 9    public struct Template: Codable, Sendable, Hashable {
10        public let key: String
11        public let name: String
12
13        public init(key: String, name: String) {
14            self.key = key
15            self.name = name
16        }
17    }
18
19    public let folder: URL
20
21    /// The app group's inbox; nil when the app isn't entitled to the group.
22    public init?(group: String = CaptureInbox.group) {
23        guard let container = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: group) else { return nil }
24        folder = container.appendingPathComponent("Captures")
25    }
26
27    public init(folder: URL) {
28        self.folder = folder
29    }
30
31    private var templatesFile: URL { folder.appendingPathComponent("templates.json") }
32
33    public func add(_ link: String) throws {
34        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
35        let name = String(format: "%020.6f", Date().timeIntervalSince1970) + "-" + UUID().uuidString + ".link"
36        try Data(link.utf8).write(to: folder.appendingPathComponent(name), options: .atomic)
37    }
38
39    /// A waiting link and the file that holds it.
40    public struct Link: Sendable, Hashable {
41        public let file: URL
42        public let text: String
43    }
44
45    /// The waiting links, oldest first. Each stays until `remove`; a file that can't be read
46    /// is left out and kept.
47    public func links() -> [Link] {
48        let names = ((try? FileManager.default.contentsOfDirectory(atPath: folder.path)) ?? []).filter { $0.hasSuffix(".link") }.sorted()
49        return names.compactMap { name in
50            let file = folder.appendingPathComponent(name)
51            return (try? Data(contentsOf: file)).flatMap { String(data: $0, encoding: .utf8) }.map { Link(file: file, text: $0) }
52        }
53    }
54
55    public func remove(_ link: Link) {
56        try? FileManager.default.removeItem(at: link.file)
57    }
58
59    public func setTemplates(_ templates: [Template]) throws {
60        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
61        try JSONEncoder().encode(templates).write(to: templatesFile, options: .atomic)
62    }
63
64    public func templates() -> [Template] {
65        (try? JSONDecoder().decode([Template].self, from: Data(contentsOf: templatesFile))) ?? []
66    }
67}