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:
committed by
Peter Steinberger
parent
8d74578ceb
commit
7bfc77db25
@@ -163,6 +163,8 @@ export async function loadImageFromRef(
|
|||||||
// For file paths, resolve relative to the appropriate root:
|
// For file paths, resolve relative to the appropriate root:
|
||||||
// - When sandbox is enabled, resolve relative to sandboxRoot for security
|
// - When sandbox is enabled, resolve relative to sandboxRoot for security
|
||||||
// - Otherwise, resolve relative to workspaceDir
|
// - 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)) {
|
if (ref.type === "path" && !path.isAbsolute(targetPath)) {
|
||||||
const resolveRoot = options?.sandboxRoot ?? workspaceDir;
|
const resolveRoot = options?.sandboxRoot ?? workspaceDir;
|
||||||
targetPath = path.resolve(resolveRoot, targetPath);
|
targetPath = path.resolve(resolveRoot, targetPath);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
import { fileURLToPath } from "node:url";
|
||||||
|
|
||||||
import { logVerbose, shouldLogVerbose } from "../globals.js";
|
import { logVerbose, shouldLogVerbose } from "../globals.js";
|
||||||
import { type MediaKind, maxBytesForKind, mediaKindFromMime } from "../media/constants.js";
|
import { type MediaKind, maxBytesForKind, mediaKindFromMime } from "../media/constants.js";
|
||||||
@@ -24,8 +25,9 @@ async function loadWebMediaInternal(
|
|||||||
options: WebMediaOptions = {},
|
options: WebMediaOptions = {},
|
||||||
): Promise<WebMediaResult> {
|
): Promise<WebMediaResult> {
|
||||||
const { maxBytes, optimizeImages = true } = options;
|
const { maxBytes, optimizeImages = true } = options;
|
||||||
|
// Use fileURLToPath for proper handling of file:// URLs (handles file://localhost/path, etc.)
|
||||||
if (mediaUrl.startsWith("file://")) {
|
if (mediaUrl.startsWith("file://")) {
|
||||||
mediaUrl = mediaUrl.replace("file://", "");
|
mediaUrl = fileURLToPath(mediaUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
const optimizeAndClampImage = async (buffer: Buffer, cap: number) => {
|
const optimizeAndClampImage = async (buffer: Buffer, cap: number) => {
|
||||||
@@ -57,7 +59,7 @@ async function loadWebMediaInternal(
|
|||||||
kind: MediaKind;
|
kind: MediaKind;
|
||||||
fileName?: string;
|
fileName?: string;
|
||||||
}): Promise<WebMediaResult> => {
|
}): 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") {
|
if (params.kind === "image") {
|
||||||
const isGif = params.contentType === "image/gif";
|
const isGif = params.contentType === "image/gif";
|
||||||
if (isGif || !optimizeImages) {
|
if (isGif || !optimizeImages) {
|
||||||
|
|||||||
Reference in New Issue
Block a user