// EXPORT LIBRARY (Books, Music, Podcasts and Cinemas, 7 Nov 2026): a// person's library leaves Cotton whole. The tool's act answers `export_open`// (`tools.act`); the core lays the kit's open bundle — the Kodi, Plex, OPDS,// M3U8 and OPML trees other apps open — into a fresh folder under this app's// temporary directory (`package.exportOpen`), this zips the folder with the// system's own file coordinator, hands the zip to the share sheet (Files,// AirDrop) and, once the sheet closes, says how many files went and how many// of the library's files are not on this phone. The folder and the zip are// removed then. A refusal says the core's sentence.importCottonKitimportFoundation@MainActorfinalclassLibraryExport {staticlet shared = LibraryExport()privatevar core: (any CottonCalling)?privatevar say: ((String) -> Void)?privatevar tell: ((String) -> Void)?privatelet sharing: any FileSharingprivatelet temporary: URLprivatelet now: () -> Dateinit(sharing: any FileSharing = SystemShareSheet(), temporary: URL = FileManager.default.temporaryDirectory, now: @escaping () -> Date = Date.init) {self.sharing = sharingself.temporary = temporaryself.now = now }/// The core and the toasts, from the root once a core is open: `say` an/// error, `tell` a receipt.func attach(core: any CottonCalling, say: @escaping (String) -> Void, tell: @escaping (String) -> Void) {self.core = coreself.say = sayself.tell = tell }/// Lay `kit`'s open bundle, zip it as `Cotton <name> <date>.zip`, share/// it, and say what it held once the sheet closes.func export(kit: String, name: String) async {guardlet core else { say?(ShellLines.line("databases", "export_open.refused"))return }let work = temporary.appendingPathComponent("library-export-\(UUID().uuidString)", isDirectory: true)defer { try? FileManager.default.removeItem(at: work) }let title = Self.title(name: name, on: now())let folder = work.appendingPathComponent(title, isDirectory: true)let exported: OpenBundleExporteddo { exported = tryawaitPackages(core: core).exportOpen(kit: kit, dir: folder.path) } catch { say?(ActionRefused.words(for: error))return }guardlet zip = awaitTask.detached(operation: { Self.zipped(folder, into: work) }).value else { say?(ShellLines.line("databases", "export_open.refused"))return }await sharing.share(title: title, url: zip) tell?(Self.receipt(exported)) }/// The zip's name, and the folder's inside it: the tool's name and the day.staticfunc title(name: String, on date: Date) -> String {ShellLines.line("databases", "export_open.name", ["kit": name, "date": date.formatted(day)]) }/// What the export says: the files written, and how many of the/// library's files this phone does not keep where any are missing.staticfunc receipt(_ exported: OpenBundleExported) -> String {let written = String(exported.files)guard !exported.missingMedia.isEmpty else { returnShellLines.line("databases", "export_open.written", ["n": written]) }returnShellLines.line("databases", "export_open.written_missing", ["n": written, "missing": String(exported.missingMedia.count)]) }/// `folder` zipped beside it as `<folder's name>.zip` by the system's/// file coordinator (the zip it makes for an upload); nil when it could/// not be made. nonisolated staticfunc zipped(_ folder: URL, into directory: URL) -> URL? {let target = directory.appendingPathComponent(folder.lastPathComponent).appendingPathExtension("zip")var refused: NSError?var made = false NSFileCoordinator().coordinate(readingItemAt: folder, options: .forUploading, error: &refused) { zip in made = (try? FileManager.default.moveItem(at: zip, to: target)) != nil }return refused == nil && made ? target : nil }privatestaticlet day = Date.ISO8601FormatStyle(timeZone: .current).year().month().day()}