|
|
|
|
@ -34,6 +34,27 @@ type WebMediaOptions = {
|
|
|
|
|
readFile?: (filePath: string) => Promise<Buffer>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function resolveWebMediaOptions(params: {
|
|
|
|
|
maxBytesOrOptions?: number | WebMediaOptions;
|
|
|
|
|
options?: { ssrfPolicy?: SsrFPolicy; localRoots?: readonly string[] | "any" };
|
|
|
|
|
optimizeImages: boolean;
|
|
|
|
|
}): WebMediaOptions {
|
|
|
|
|
if (typeof params.maxBytesOrOptions === "number" || params.maxBytesOrOptions === undefined) {
|
|
|
|
|
return {
|
|
|
|
|
maxBytes: params.maxBytesOrOptions,
|
|
|
|
|
optimizeImages: params.optimizeImages,
|
|
|
|
|
ssrfPolicy: params.options?.ssrfPolicy,
|
|
|
|
|
localRoots: params.options?.localRoots,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...params.maxBytesOrOptions,
|
|
|
|
|
optimizeImages: params.optimizeImages
|
|
|
|
|
? (params.maxBytesOrOptions.optimizeImages ?? true)
|
|
|
|
|
: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type LocalMediaAccessErrorCode =
|
|
|
|
|
| "path-not-allowed"
|
|
|
|
|
| "invalid-root"
|
|
|
|
|
@ -385,18 +406,10 @@ export async function loadWebMedia(
|
|
|
|
|
maxBytesOrOptions?: number | WebMediaOptions,
|
|
|
|
|
options?: { ssrfPolicy?: SsrFPolicy; localRoots?: readonly string[] | "any" },
|
|
|
|
|
): Promise<WebMediaResult> {
|
|
|
|
|
if (typeof maxBytesOrOptions === "number" || maxBytesOrOptions === undefined) {
|
|
|
|
|
return await loadWebMediaInternal(mediaUrl, {
|
|
|
|
|
maxBytes: maxBytesOrOptions,
|
|
|
|
|
optimizeImages: true,
|
|
|
|
|
ssrfPolicy: options?.ssrfPolicy,
|
|
|
|
|
localRoots: options?.localRoots,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return await loadWebMediaInternal(mediaUrl, {
|
|
|
|
|
...maxBytesOrOptions,
|
|
|
|
|
optimizeImages: maxBytesOrOptions.optimizeImages ?? true,
|
|
|
|
|
});
|
|
|
|
|
return await loadWebMediaInternal(
|
|
|
|
|
mediaUrl,
|
|
|
|
|
resolveWebMediaOptions({ maxBytesOrOptions, options, optimizeImages: true }),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function loadWebMediaRaw(
|
|
|
|
|
@ -404,18 +417,10 @@ export async function loadWebMediaRaw(
|
|
|
|
|
maxBytesOrOptions?: number | WebMediaOptions,
|
|
|
|
|
options?: { ssrfPolicy?: SsrFPolicy; localRoots?: readonly string[] | "any" },
|
|
|
|
|
): Promise<WebMediaResult> {
|
|
|
|
|
if (typeof maxBytesOrOptions === "number" || maxBytesOrOptions === undefined) {
|
|
|
|
|
return await loadWebMediaInternal(mediaUrl, {
|
|
|
|
|
maxBytes: maxBytesOrOptions,
|
|
|
|
|
optimizeImages: false,
|
|
|
|
|
ssrfPolicy: options?.ssrfPolicy,
|
|
|
|
|
localRoots: options?.localRoots,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return await loadWebMediaInternal(mediaUrl, {
|
|
|
|
|
...maxBytesOrOptions,
|
|
|
|
|
optimizeImages: false,
|
|
|
|
|
});
|
|
|
|
|
return await loadWebMediaInternal(
|
|
|
|
|
mediaUrl,
|
|
|
|
|
resolveWebMediaOptions({ maxBytesOrOptions, options, optimizeImages: false }),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function optimizeImageToJpeg(
|
|
|
|
|
|