fix(hooks): improve image resizer implementation
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -79,6 +79,10 @@ function getErrorMessage(error: unknown): string {
|
|||||||
return error instanceof Error ? error.message : String(error)
|
return error instanceof Error ? error.message : String(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function loadSharpModule(): Promise<unknown | null> {
|
||||||
|
return Function('return import("sharp").catch(() => null)')() as Promise<unknown | null>
|
||||||
|
}
|
||||||
|
|
||||||
export function calculateTargetDimensions(
|
export function calculateTargetDimensions(
|
||||||
width: number,
|
width: number,
|
||||||
height: number,
|
height: number,
|
||||||
@@ -110,10 +114,12 @@ export async function resizeImage(
|
|||||||
base64DataUrl: string,
|
base64DataUrl: string,
|
||||||
mimeType: string,
|
mimeType: string,
|
||||||
target: ImageDimensions,
|
target: ImageDimensions,
|
||||||
|
deps: {
|
||||||
|
loadSharpModule?: () => Promise<unknown | null>
|
||||||
|
} = {},
|
||||||
): Promise<ResizeResult | null> {
|
): Promise<ResizeResult | null> {
|
||||||
try {
|
try {
|
||||||
const sharpModuleName = "sharp"
|
const sharpModule = await (deps.loadSharpModule?.() ?? loadSharpModule())
|
||||||
const sharpModule = await import(sharpModuleName).catch(() => null)
|
|
||||||
if (!sharpModule) {
|
if (!sharpModule) {
|
||||||
log("[read-image-resizer] sharp unavailable, attempting pure-JS fallback")
|
log("[read-image-resizer] sharp unavailable, attempting pure-JS fallback")
|
||||||
return resizeImageFallback(base64DataUrl, mimeType, target)
|
return resizeImageFallback(base64DataUrl, mimeType, target)
|
||||||
|
|||||||
Reference in New Issue
Block a user