//// An image's bytes, from whichever source the document came from. //// //// The model *names* images, with digest, byte size and pixel size; it does //// not hold them. That is right - an abstraction of a document is not a //// bundle of its assets - but a writer of any format that shows images needs //// the bytes, so something has to know where they are. This is that //// something, and it is in the library rather than in a writer because the //// answer depends on the *input form* and not on the output. //// //// | `.ocda.db` | the `files` table, role 'image', by name | //// | `.zip` | the archive entry whose path ends in `/image/` | //// | `.sisupod` | the same, if the frame can be unwrapped, which on the BEAM it cannot | //// | `.ssp` | beside it: a pod puts the abstraction in `media/abstraction/` and its images in `media/image/` | import gleam/bit_array import gleam/dynamic/decode import gleam/list import gleam/result import gleam/string import ocda/load import ocda/zip import simplifile import sqlight /// The bytes, or an error saying why not. pub fn image_bytes(source: String, name: String) -> Result(BitArray, String) { case load.source_of(source) { load.OcdaDb -> from_db(source, name) load.Pod -> from_archive(source, name) load.Ssp -> from_beside(source, name) load.Unknown -> Error(source <> ": not a source this library reads") } } /// Can this source give images at all? /// /// A writer asks before it decides whether to emit them, because an `` /// pointing at a file it did not include is worse than no image. pub fn can_give_images(source: String) -> Bool { case load.source_of(source) { load.OcdaDb -> True load.Pod -> zip_readable(source) load.Ssp -> image_dir_beside(source) != "" load.Unknown -> False } } fn zip_readable(path: String) -> Bool { case simplifile.read_bits(path) { Error(_) -> False Ok(bytes) -> case zip.entries(bytes) { Ok(_) -> True Error(_) -> False } } } fn from_db(path: String, name: String) -> Result(BitArray, String) { case sqlight.open("file:" <> path <> "?mode=ro") { Error(e) -> Error(path <> ": " <> e.message) Ok(conn) -> { let out = sqlight.query( "select data from files where role = 'image' and name = ?1", on: conn, with: [sqlight.text(name)], expecting: blob(), ) |> result.replace_error(path <> ": could not read " <> name) let _ = sqlight.close(conn) case out { Ok([data, ..]) -> Ok(data) Ok([]) -> Error(name <> ": not in this database") Error(e) -> Error(e) } } } } /// A blob column, read as bytes rather than as text: an image read as text /// stops at the first zero byte, and a png's ninth byte is zero. fn blob() -> decode.Decoder(BitArray) { use v <- decode.field(0, decode.bit_array) decode.success(v) } fn from_archive(path: String, name: String) -> Result(BitArray, String) { use bytes <- result.try( simplifile.read_bits(path) |> result.replace_error(path <> ": cannot read"), ) use entries <- result.try(zip.entries(bytes)) case list.find(entries, fn(e) { string.ends_with(e.name, "/image/" <> name) }) { Error(_) -> Error(name <> ": not in this archive") Ok(entry) -> zip.read(bytes, entry) } } fn from_beside(path: String, name: String) -> Result(BitArray, String) { case image_dir_beside(path) { "" -> Error(name <> ": a .ssp outside a pod has no image directory beside it") dir -> simplifile.read_bits(dir <> "/" <> name) |> result.replace_error(dir <> "/" <> name <> ": cannot read") } } /// `media/abstraction/.ssp` -> `media/image/` fn image_dir_beside(path: String) -> String { let parts = string.split(path, on: "/") let dir = case list.reverse(parts) { [_file, ..rest] -> string.join(list.reverse(rest), "/") [] -> "" } case string.ends_with(dir, "abstraction") { False -> "" True -> { let parent = case list.reverse(string.split(dir, on: "/")) { [_, ..rest] -> string.join(list.reverse(rest), "/") [] -> "" } let image_dir = parent <> "/image" case simplifile.is_directory(image_dir) { Ok(True) -> image_dir _ -> "" } } } } /// Unused here, kept so that a caller can size a read before making it. pub fn byte_size(b: BitArray) -> Int { bit_array.byte_size(b) }