From 65b66f86cd894fbe548fa3ffc4584e4b2f8858f3 Mon Sep 17 00:00:00 2001 From: Factor Date: Thu, 19 Nov 2026 12:09:32 +0100 Subject: [PATCH] fix: thumbnails via image() relative to appHome, paste dir from appHome Co-Authored-By: Ovis Fast (Claude) --- mod/ovis-composer/attachments.js | 23 +++++++++++++++++++++++ mod/ovis-composer/main.js | 24 ++++++++++++++++++------ tests/attachments.test.mjs | 19 +++++++++++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/mod/ovis-composer/attachments.js b/mod/ovis-composer/attachments.js index e7d446d..4c50e83 100644 --- a/mod/ovis-composer/attachments.js +++ b/mod/ovis-composer/attachments.js @@ -192,3 +192,26 @@ export function formatBytes(bytes) { if (bytes < 1024 * 1024 * 1024) return unit(bytes / 1024 / 1024, "MB"); return unit(bytes / 1024 / 1024 / 1024, "GB"); } + +/** + * The path of a file relative to a folder - or nothing when the file is not + * inside it. + * + * `image()` draws only files of the application's own folder, and wants their + * path relative to it (forward slashes). Windows paths compare without regard + * to case and to the kind of slash. + * + * @param {string} base - the folder (`appHome()`) + * @param {string} path - the file + * @returns {string|null} + */ +export function relativeTo(base, path) { + if (typeof base !== "string" || typeof path !== "string" || base === "" || path === "") return null; + const clean = (text) => text.replace(/\\/g, "/").replace(/\/+$/, ""); + const root = clean(base); + const file = clean(path); + if (file.length <= root.length + 1) return null; + if (file.slice(0, root.length).toLowerCase() !== root.toLowerCase()) return null; + if (file[root.length] !== "/") return null; + return file.slice(root.length + 1); +} diff --git a/mod/ovis-composer/main.js b/mod/ovis-composer/main.js index 5b95a51..16f1817 100644 --- a/mod/ovis-composer/main.js +++ b/mod/ovis-composer/main.js @@ -27,7 +27,7 @@ import { theme, exec, hc, - home, + appHome, run, settings, shared, @@ -42,7 +42,7 @@ import { keyIntent } from "./keys.js"; import { decide, shouldDrain, shouldStartCountdown, parseSendArgs, sendNow as sendToTerminal } from "./delivery.js"; import { detect as detectTrigger, rank, merge, startsLine } from "./suggest.js"; import * as catalog from "./catalog.js"; -import { LIMITS, validate, mention, removeMention, kindOf, formatBytes } from "./attachments.js"; +import { LIMITS, validate, mention, removeMention, kindOf, formatBytes, relativeTo } from "./attachments.js"; import { clipboardScript, parseClipboardResult, @@ -876,7 +876,9 @@ export default class Composer extends View { // --- the preview of an image ---------------------------------------------- openPreview(file, cx) { - this.preview = { path: file.path, caption: `${file.name} · ${formatBytes(file.bytes) || "?"} · Esc zamyka` }; + const path = this.imageSource(file); + if (!path) return; + this.preview = { path, caption: `${file.name} · ${formatBytes(file.bytes) || "?"} · Esc zamyka` }; // The overlay is drawn on the next frame; it takes the keyboard then. cx.timer.after(30, () => { try { @@ -888,6 +890,15 @@ export default class Composer extends View { cx.notify(); } + /** + * What `image()` is given for a file: its path relative to the app's folder + * (the pasted images are there) - or nothing for a file outside it, which + * `image()` cannot draw. + */ + imageSource(file) { + return file.kind === "image" ? relativeTo(appHome(), file.path) : null; + } + closePreview(cx) { this.preview = null; cx.notify(); @@ -1879,7 +1890,7 @@ export default class Composer extends View { /** Where pasted images are saved before they go to the agent. */ pasteDir() { - return [home(), ".config", "herdr-app", "data", "ovis-composer", "paste"].join("\\"); + return [appHome(), "data", "ovis-composer", "paste"].join("\\"); } /** @@ -2412,8 +2423,9 @@ export default class Composer extends View { const adding = file.state === "adding"; const extension = (file.name.split(".").pop() || "").slice(0, 4); + const source = this.imageSource(file); const thumb = - file.kind === "image" + source ? div() .id(`file-thumb-${file.id}`) .size(36) @@ -2423,7 +2435,7 @@ export default class Composer extends View { .cursor_pointer() .when(adding, (el) => el.opacity(0.35)) .on_click((event, cx) => this.openPreview(file, cx)) - .child(image(file.path).size(36)) + .child(image(source).size(36)) : div() .size(36) .flex_none() diff --git a/tests/attachments.test.mjs b/tests/attachments.test.mjs index fb3a145..7db69b4 100644 --- a/tests/attachments.test.mjs +++ b/tests/attachments.test.mjs @@ -70,3 +70,22 @@ test("formatBytes", () => { assert.equal(formatBytes(undefined), ""); assert.equal(formatBytes(NaN), ""); }); + +import { relativeTo } from "../mod/ovis-composer/attachments.js"; + +test("relativeTo: a file inside the folder, forward slashes, any case", () => { + assert.equal( + relativeTo("C:\\Users\\Factor\\.config\\herdr-app", "C:\\Users\\Factor\\.config\\herdr-app\\data\\ovis-composer\\paste\\paste-1.png"), + "data/ovis-composer/paste/paste-1.png", + ); + assert.equal(relativeTo("c:/users/factor/.config/herdr-app/", "C:\\Users\\Factor\\.config\\herdr-app\\a b\\ę.png"), "a b/ę.png"); +}); + +test("relativeTo: outside the folder, the folder itself, or rubbish is nothing", () => { + assert.equal(relativeTo("C:\\app", "D:\\pics\\a.png"), null); + assert.equal(relativeTo("C:\\app", "C:\\application\\a.png"), null); + assert.equal(relativeTo("C:\\app", "C:\\app"), null); + assert.equal(relativeTo("C:\\app", "C:\\app\\"), null); + assert.equal(relativeTo("", "C:\\a.png"), null); + assert.equal(relativeTo("C:\\app", undefined), null); +});