fix: thumbnails via image() relative to appHome, paste dir from appHome

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 12:09:32 +01:00
1 parent dd95bf2c68
commit 65b66f86cd
3 files changed
+60 -6

No files matched your search

+23
View File
@@ -192,3 +192,26 @@ export function formatBytes(bytes) {
if (bytes < 1024 * 1024 * 1024) return unit(bytes / 1024 / 1024, "MB"); if (bytes < 1024 * 1024 * 1024) return unit(bytes / 1024 / 1024, "MB");
return unit(bytes / 1024 / 1024 / 1024, "GB"); 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);
}
+18 -6
View File
@@ -27,7 +27,7 @@ import {
theme, theme,
exec, exec,
hc, hc,
home, appHome,
run, run,
settings, settings,
shared, shared,
@@ -42,7 +42,7 @@ import { keyIntent } from "./keys.js";
import { decide, shouldDrain, shouldStartCountdown, parseSendArgs, sendNow as sendToTerminal } from "./delivery.js"; import { decide, shouldDrain, shouldStartCountdown, parseSendArgs, sendNow as sendToTerminal } from "./delivery.js";
import { detect as detectTrigger, rank, merge, startsLine } from "./suggest.js"; import { detect as detectTrigger, rank, merge, startsLine } from "./suggest.js";
import * as catalog from "./catalog.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 { import {
clipboardScript, clipboardScript,
parseClipboardResult, parseClipboardResult,
@@ -876,7 +876,9 @@ export default class Composer extends View {
// --- the preview of an image ---------------------------------------------- // --- the preview of an image ----------------------------------------------
openPreview(file, cx) { 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. // The overlay is drawn on the next frame; it takes the keyboard then.
cx.timer.after(30, () => { cx.timer.after(30, () => {
try { try {
@@ -888,6 +890,15 @@ export default class Composer extends View {
cx.notify(); 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) { closePreview(cx) {
this.preview = null; this.preview = null;
cx.notify(); cx.notify();
@@ -1879,7 +1890,7 @@ export default class Composer extends View {
/** Where pasted images are saved before they go to the agent. */ /** Where pasted images are saved before they go to the agent. */
pasteDir() { 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 adding = file.state === "adding";
const extension = (file.name.split(".").pop() || "").slice(0, 4); const extension = (file.name.split(".").pop() || "").slice(0, 4);
const source = this.imageSource(file);
const thumb = const thumb =
file.kind === "image" source
? div() ? div()
.id(`file-thumb-${file.id}`) .id(`file-thumb-${file.id}`)
.size(36) .size(36)
@@ -2423,7 +2435,7 @@ export default class Composer extends View {
.cursor_pointer() .cursor_pointer()
.when(adding, (el) => el.opacity(0.35)) .when(adding, (el) => el.opacity(0.35))
.on_click((event, cx) => this.openPreview(file, cx)) .on_click((event, cx) => this.openPreview(file, cx))
.child(image(file.path).size(36)) .child(image(source).size(36))
: div() : div()
.size(36) .size(36)
.flex_none() .flex_none()
+19
View File
@@ -70,3 +70,22 @@ test("formatBytes", () => {
assert.equal(formatBytes(undefined), ""); assert.equal(formatBytes(undefined), "");
assert.equal(formatBytes(NaN), ""); 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);
});