fix: improve file URL handling and enhance image loading logic

- Added handling for file URLs using fileURLToPath for proper resolution.
- Updated logic to skip relative path resolution if ref.resolved is already absolute.
- Enhanced cap calculation for image loading to handle undefined maxBytes more gracefully.
This commit is contained in:
Tyler Yust
2026-01-17 03:20:02 -08:00
committed by Peter Steinberger
parent 8d74578ceb
commit 7bfc77db25
2 changed files with 6 additions and 2 deletions

View File

@@ -163,6 +163,8 @@ export async function loadImageFromRef(
// For file paths, resolve relative to the appropriate root:
// - When sandbox is enabled, resolve relative to sandboxRoot for security
// - Otherwise, resolve relative to workspaceDir
// Note: ref.resolved may already be absolute (e.g., after ~ expansion in detectImageReferences),
// in which case we skip relative resolution.
if (ref.type === "path" && !path.isAbsolute(targetPath)) {
const resolveRoot = options?.sandboxRoot ?? workspaceDir;
targetPath = path.resolve(resolveRoot, targetPath);

View File

@@ -1,5 +1,6 @@
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { logVerbose, shouldLogVerbose } from "../globals.js";
import { type MediaKind, maxBytesForKind, mediaKindFromMime } from "../media/constants.js";
@@ -24,8 +25,9 @@ async function loadWebMediaInternal(
options: WebMediaOptions = {},
): Promise<WebMediaResult> {
const { maxBytes, optimizeImages = true } = options;
// Use fileURLToPath for proper handling of file:// URLs (handles file://localhost/path, etc.)
if (mediaUrl.startsWith("file://")) {
mediaUrl = mediaUrl.replace("file://", "");
mediaUrl = fileURLToPath(mediaUrl);
}
const optimizeAndClampImage = async (buffer: Buffer, cap: number) => {
@@ -57,7 +59,7 @@ async function loadWebMediaInternal(
kind: MediaKind;
fileName?: string;
}): Promise<WebMediaResult> => {
const cap = Math.min(maxBytes ?? maxBytesForKind(params.kind), maxBytesForKind(params.kind));
const cap = maxBytes !== undefined ? Math.min(maxBytes, maxBytesForKind(params.kind)) : maxBytesForKind(params.kind);
if (params.kind === "image") {
const isGif = params.contentType === "image/gif";
if (isGif || !optimizeImages) {