/+ - Name: SisuDoc Spine, Doc Reform [a part of] - Description: documents, structuring, processing, publishing, search - static content generator - Author: Ralph Amissah [ralph.amissah@gmail.com] - Copyright: (C) 2015 (continuously updated, current 2026) Ralph Amissah, All Rights Reserved. - License: AGPL 3 or later: Spine (SiSU), a framework for document structuring, publishing and search Copyright (C) Ralph Amissah This program is free software: you can redistribute it and/or modify it under the terms of the GNU AFERO General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see [https://www.gnu.org/licenses/]. If you have Internet connection, the latest version of the AGPL should be available at these locations: [https://www.fsf.org/licensing/licenses/agpl.html] [https://www.gnu.org/licenses/agpl.html] - Spine (by Doc Reform, related to SiSU) uses standard: - docReform markup syntax - standard SiSU markup syntax with modified headers and minor modifications - docReform object numbering - standard SiSU object citation numbering & system - Homepages: [https://www.sisudoc.org] [https://www.doc-reform.org] - Git [https://git.sisudoc.org/] +/ module ocda.images; /+ ↓ 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. . Three answers, and one refusal: . .ocda.db the files table, role 'image', by name .sisupod the archive entry whose path ends in /image/ .zip the same .ssp beside it: a pod puts the abstraction in media/abstraction/ and its images in media/image/, so the path is documented rather than guessed . A .ssp that is not in a pod has nowhere to look, and that is reported as absent rather than as an error: a reader that cannot find an image should say so and carry on, because the document is still readable. +/ import std.algorithm : endsWith; import std.array : array; import std.file : exists, isFile, read; import std.path : buildPath, dirName; import ocda.load; import ocda.sqlite; import ocda.zip; import ocda.zstd; /+ ↓ the bytes, or an empty slice when the source has no such image +/ ubyte[] imageBytes(string source, string name) { final switch (sourceOf(source)) { case Source.ocda_db: return fromDb(source, name); case Source.pod: return fromArchive(source, name); case Source.ssp: return fromBeside(source, name); case Source.unknown: return []; } } /+ ↓ 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. +/ bool canGiveImages(string source) { final switch (sourceOf(source)) { case Source.ocda_db: return true; case Source.pod: return true; case Source.ssp: return imageDirBeside(source).length > 0; case Source.unknown: return false; } } private ubyte[] fromDb(string path, string name) { auto db = Db.open(path); scope(exit) { db.close(); } if (!db.has("files")) { return []; } /+ ↓ the name is from the abstraction, not from a caller, and the quote doubling is what keeps it a value rather than syntax +/ return db.blob("SELECT data FROM files WHERE role = 'image' AND name = '" ~ sqlQuote(name) ~ "'"); } private ubyte[] fromArchive(string path, string name) { auto bytes = cast(ubyte[]) read(path); if (zstdIsFrame(bytes)) { bytes = zstdDecompress(bytes, 512 * 1024 * 1024); } auto entries = zipEntries(bytes); foreach (e; entries) { if (e.name.endsWith("/image/" ~ name)) { return zipRead(bytes, e); } } return []; } private ubyte[] fromBeside(string path, string name) { string dir = imageDirBeside(path); if (dir.length == 0) { return []; } string file = buildPath(dir, name); return file.exists && file.isFile ? cast(ubyte[]) read(file) : []; } /+ ↓ media/abstraction/.ssp -> media/image/ +/ private string imageDirBeside(string path) { string dir = path.dirName; if (!dir.endsWith("abstraction")) { return ""; } string image_dir = buildPath(dir.dirName, "image"); return image_dir.exists ? image_dir : ""; } /+ ↓ a single quote doubled is how sql says a single quote +/ private string sqlQuote(string s) { string out_; foreach (c; s) { out_ ~= (c == '\'') ? "''" : [c]; } return out_; }