// Putting a shared book on the shelf, and handing a Goodreads library to the// object — the two things the share drain's book branches ask of the core.//// A seam (`BookShelving`) for `MediaCapturing`'s reason: a drain test drives// both branches with no core, no file on disk and no object.importFoundation/// What the drain's book branches need.publicprotocolBookShelving: Sendable {/// The book file at `url` read for what it says about itself, kept, and/// written as a new `book` row — or, `into` a book with no file (its Add/// file), given to that book; answers that row's id. Blocking work: call/// it off the main actor.func shelve(_ url: URL, format: BookFormat, fileName: String?, into book: String?) asyncthrows -> String/// A Goodreads library export's text, handed to the object's/// `import_goodreads`: the object makes or brings up to date one book per/// row and the phone takes them by sync. Answers the book the phone lands/// on (the answer's `first`: the first the import made, else the first it/// found again); nil for an export of no books.func importGoodreads(_ text: String) asyncthrows -> String?}/// The shipping shelf: `capture.media` as a book — an EPUB with its format/// alone, the core reading what it says; a PDF read here (`BookFile`), its/// cover beside it — so the book row is made with its bytes in the store/// before the app opens it.publicstructCoreBookShelf: BookShelving {privatelet core: any CottonCallingpublicinit(core: any CottonCalling) {self.core = core }publicfunc shelve(_ url: URL, format: BookFormat, fileName: String?, into book: String?) asyncthrows -> String {// An EPUB is read by the core, its OPF and its cover, as Android's is.guard format == .pdf else {let kept = tryawait core.call(CottonMethod.captureMedia,BookCaptureRequest(epub: url.path(percentEncoded: false), filename: fileName, into: book),as: MediaCaptured.self)return kept.recordId }let facts = tryBookFile.read(url, fileName: fileName)let scratch = MediaTiers.scratchDirectory()defer { MediaTiers.discard(scratch) }var coverPath: String?iflet cover = facts.cover {let file = scratch.appendingPathComponent("cover").appendingPathExtension(facts.coverExtension ?? "jpg")try cover.write(to: file) coverPath = file.path(percentEncoded: false) }let kept = tryawait core.call(CottonMethod.captureMedia,BookCaptureRequest(path: url.path(percentEncoded: false), facts: facts, filename: fileName, coverPath: coverPath, into: book),as: MediaCaptured.self)return kept.recordId }publicfunc importGoodreads(_ text: String) asyncthrows -> String? {let request: [String: JSONValue] = ["act": .string(BookActs.importGoodreads), "args": .object(["file": .string(text)])]let answer = tryawait core.callJSON(CottonMethod.objectAct, request)return answer[BookActs.importedFirst]?.string.flatMap { $0.isEmpty ? nil : $0 } }}/// The object's acts the books ask for by name (`object.act`).enumBookActs {publicstaticlet importGoodreads = "import_goodreads"/// The key of `import_goodreads`' answer naming the book to land on.publicstaticlet importedFirst = "first"}/// `capture.media` for a book: the file, what it says about itself, and its/// cover's path in this call's scratch folder. The core keeps the file under/// the book media kind (`<database>/books/<sha256>.<ext>`), writes the cover/// as the row's picture and makes the `book` row: `file`, `format`, `source`/// file, `status` want_to_read, `added_on` today and these facts.structBookCaptureRequest: Encodable, Sendable, Equatable {publicvar path: Stringpublicvar mime: Stringpublicvar kind = "book"publicvar filename: String?publicvar book: Factspublicvar coverPath: String?/// The book this file is for (its Add file); absent, a book of its own.publicvar into: String?/// The facts as the row takes them; an absent one is left out.publicstructFacts: Encodable, Sendable, Equatable {publicvar format: String/// A PDF's; an EPUB's title is the core's to read.publicvar title: String?publicvar author: String?publicvar language: String?publicvar isbn: String?publicvar pages: Int? }publicinit(path: String, facts: BookFacts, filename: String?, coverPath: String?, into: String? = nil) {self.path = pathself.mime = facts.format.mimeself.filename = filenameself.book = Facts(format: facts.format.rawValue, title: facts.title, author: facts.author, language: facts.language, isbn: facts.isbn, pages: facts.pages)self.coverPath = coverPathself.into = into }/// An EPUB, sent with its format alone: the core reads its OPF (title,/// first creator, language, ISBN) and its cover.publicinit(epub path: String, filename: String?, into: String? = nil) {self.path = pathself.mime = BookFormat.epub.mimeself.filename = filenameself.book = Facts(format: BookFormat.epub.rawValue)self.into = into }}