// Books' Add a folder (the coordinator, 7 Nov 2026): a picked tree — a// Calibre library, or a folder of EPUBs and PDFs — handed WHOLE to the core's// `books.folder`, which reads it (a Calibre book from its metadata.opf and// cover.jpg first, its EPUB second; a bare PDF a book by its name) and lands// each book through capture.media's one writer. The shell only lists the// files: every one in the tree, by where it is staged and its path below the// folder; what is a book, and every word said of it, is the core's.importFoundation/// One file of the picked tree, as `books.folder` takes it.publicstructBookFolderFile: Encodable, Equatable, Sendable {/// Where the file is staged on this phone.publicvar path: String/// Its path below the picked folder, `/` between parts.publicvar name: Stringpublicinit(path: String, name: String) {self.path = pathself.name = name }}/// What `books.folder` answered: each book and how it came in, and its words.publicstructBookFolderAnswer: Decodable, Equatable, Sendable {publicstructBook: Decodable, Equatable, Sendable {publicvar id: String?publicvar title: String/// `added`, `already` (the same file is a book here) or `refused`.publicvar outcome: String/// Why a refused book was refused, in the core's words.publicvar why: String? }publicvar books: [Book]publicvar words: String/// The book the app stands on: the first that is a book here now.publicvar landing: String? { books.lazy.compactMap(\.id).first { !$0.isEmpty } }/// What is said: the core's words, and each refused book's own why.publicvar said: String { ([words] + books.compactMap { book in book.why.map { "\(book.title): \($0)" } }).joined(separator: "\n") }}publicenumBookFolder {/// Every file below `folder`, hidden ones aside, named by its path below/// it; in name order.publicstaticfunc files(in folder: URL) -> [BookFolderFile] {let root = folder.standardizedFileURL.path(percentEncoded: false)guardlet walk = FileManager.default.enumerator(at: folder, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) else { return [] }var found: [BookFolderFile] = []forcaselet url as URL in walk where (try? url.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile) == true {let full = url.standardizedFileURL.path(percentEncoded: false)let below = full.hasPrefix(root) ? String(full.dropFirst(root.count)) : url.lastPathComponent found.append(BookFolderFile(path: full, name: below.trimmingCharacters(in: CharacterSet(charactersIn: "/")))) }return found.sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending } }/// The tree through `books.folder`.publicstaticfunc bring(_ files: [BookFolderFile], core: any CottonCalling) asyncthrows -> BookFolderAnswer {structRequest: Encodable { let files: [BookFolderFile] }returntryawait core.call(CottonMethod.booksFolder, Request(files: files), as: BookFolderAnswer.self) }}