Cotton code
42 lines · 2.1 KB · SwiftRaw
// A tool act whose answer names a document the object wrote (Podcasts'// Export OPML, `{ document, shows, said }`; the coordinator, 7 Nov 2026): its// words are said at once, and the document comes by sync. Once its row is// here, its file is asked of this phone's store — `media.share`, a photo's// own Share, which fetches the file from the object when it is not here and// answers `share_file` — and handed to the share sheet under the name the// object gave it, never its key's.import CottonKitimport CottonValuesimport Foundation@MainActorenum DocumentShare {    /// The table a document is, and the columns read off it.    static let table = "document"    static let fileColumn = "storage_path"    static let nameColumn = "original_name"    /// The file of document `id`, once its row has come and its file is on    /// this phone; nil when the row never came (the words already said stand).    static func file(_ id: String, store: (any Records)?, core: any CottonCalling) async throws -> URL? {        guard await RecordLanding.arrived(RecordLink(table: table, id: id), in: store),              let row = try await store?.get(table: table, id: id),              let key = row.values[fileColumn].flatMap(AttachmentValue.read)?.ref        else { return nil }        let shared = try await core.call(CottonMethod.mediaShare, ["key": key], as: MediaShared.self)        let kept = URL(fileURLWithPath: shared.path)        guard let name = row.values[nameColumn]?.text, name != kept.lastPathComponent else { return kept }        // Named as the object named it, in a folder of its own.        let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString, isDirectory: true)        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)        let named = folder.appendingPathComponent(name)        try FileManager.default.copyItem(at: kept, to: named)        return named    }    /// `media.share`'s answer: where the file is on this phone.    private struct MediaShared: Decodable {        let path: String    }}