|
| 1 | +import type { ImageMetadata } from "openclaw/plugin-sdk/media-runtime"; |
| 2 | +import type sharpImport from "sharp"; |
| 3 | + |
| 4 | +type SharpFactory = typeof sharpImport; |
| 5 | + |
| 6 | +type ResizeToJpegParams = { |
| 7 | + buffer: Buffer; |
| 8 | + maxSide: number; |
| 9 | + quality: number; |
| 10 | + withoutEnlargement?: boolean; |
| 11 | +}; |
| 12 | + |
| 13 | +type ResizeToPngParams = { |
| 14 | + buffer: Buffer; |
| 15 | + maxSide: number; |
| 16 | + compressionLevel?: number; |
| 17 | + withoutEnlargement?: boolean; |
| 18 | +}; |
| 19 | + |
| 20 | +type MediaUnderstandingImageOpsOptions = { |
| 21 | + maxInputPixels: number; |
| 22 | +}; |
| 23 | + |
| 24 | +const SHARP_MODULE = "sharp"; |
| 25 | + |
| 26 | +let sharpFactoryPromise: Promise<SharpFactory> | null = null; |
| 27 | + |
| 28 | +function normalizeSharpFactory(mod: unknown): SharpFactory { |
| 29 | + const candidates = [ |
| 30 | + (mod as { default?: unknown }).default, |
| 31 | + ((mod as { default?: { default?: unknown } }).default ?? {})?.default, |
| 32 | + mod, |
| 33 | + ]; |
| 34 | + const sharp = candidates.find( |
| 35 | + (candidate): candidate is SharpFactory => typeof candidate === "function", |
| 36 | + ); |
| 37 | + if (!sharp) { |
| 38 | + throw new Error("Optional dependency sharp did not expose an image processor"); |
| 39 | + } |
| 40 | + return sharp; |
| 41 | +} |
| 42 | + |
| 43 | +async function loadSharp(maxInputPixels: number): Promise<SharpFactory> { |
| 44 | + if (!sharpFactoryPromise) { |
| 45 | + sharpFactoryPromise = import(SHARP_MODULE) |
| 46 | + .then((mod) => { |
| 47 | + const sharp = normalizeSharpFactory(mod); |
| 48 | + return ((buffer, options) => |
| 49 | + sharp(buffer, { |
| 50 | + ...options, |
| 51 | + failOnError: false, |
| 52 | + limitInputPixels: maxInputPixels, |
| 53 | + })) as SharpFactory; |
| 54 | + }) |
| 55 | + .catch((err) => { |
| 56 | + sharpFactoryPromise = null; |
| 57 | + throw new Error("Optional dependency sharp is required for image attachment processing", { |
| 58 | + cause: err, |
| 59 | + }); |
| 60 | + }); |
| 61 | + } |
| 62 | + return await sharpFactoryPromise; |
| 63 | +} |
| 64 | + |
| 65 | +function normalizeMaxInputPixels(value: number): number { |
| 66 | + if (!Number.isSafeInteger(value) || value <= 0) { |
| 67 | + throw new Error("Media attachment image ops require a positive maxInputPixels budget"); |
| 68 | + } |
| 69 | + return value; |
| 70 | +} |
| 71 | + |
| 72 | +function normalizeMetadata(meta: { width?: number; height?: number }): ImageMetadata | null { |
| 73 | + const width = meta.width ?? 0; |
| 74 | + const height = meta.height ?? 0; |
| 75 | + if (!Number.isFinite(width) || !Number.isFinite(height)) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + if (width <= 0 || height <= 0) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + return { width, height }; |
| 82 | +} |
| 83 | + |
| 84 | +export function createMediaAttachmentImageOps(options: MediaUnderstandingImageOpsOptions) { |
| 85 | + const maxInputPixels = normalizeMaxInputPixels(options.maxInputPixels); |
| 86 | + return { |
| 87 | + async getImageMetadata(buffer: Buffer): Promise<ImageMetadata | null> { |
| 88 | + const sharp = await loadSharp(maxInputPixels); |
| 89 | + return normalizeMetadata(await sharp(buffer).metadata()); |
| 90 | + }, |
| 91 | + |
| 92 | + async normalizeExifOrientation(buffer: Buffer): Promise<Buffer> { |
| 93 | + const sharp = await loadSharp(maxInputPixels); |
| 94 | + return await sharp(buffer).rotate().toBuffer(); |
| 95 | + }, |
| 96 | + |
| 97 | + async resizeToJpeg(params: ResizeToJpegParams): Promise<Buffer> { |
| 98 | + const sharp = await loadSharp(maxInputPixels); |
| 99 | + return await sharp(params.buffer) |
| 100 | + .rotate() |
| 101 | + .resize({ |
| 102 | + width: params.maxSide, |
| 103 | + height: params.maxSide, |
| 104 | + fit: "inside", |
| 105 | + withoutEnlargement: params.withoutEnlargement !== false, |
| 106 | + }) |
| 107 | + .jpeg({ quality: params.quality, mozjpeg: true }) |
| 108 | + .toBuffer(); |
| 109 | + }, |
| 110 | + |
| 111 | + async convertHeicToJpeg(buffer: Buffer): Promise<Buffer> { |
| 112 | + const sharp = await loadSharp(maxInputPixels); |
| 113 | + return await sharp(buffer).jpeg({ quality: 90, mozjpeg: true }).toBuffer(); |
| 114 | + }, |
| 115 | + |
| 116 | + async hasAlphaChannel(buffer: Buffer): Promise<boolean> { |
| 117 | + const sharp = await loadSharp(maxInputPixels); |
| 118 | + const meta = await sharp(buffer).metadata(); |
| 119 | + return meta.hasAlpha || meta.channels === 4; |
| 120 | + }, |
| 121 | + |
| 122 | + async resizeToPng(params: ResizeToPngParams): Promise<Buffer> { |
| 123 | + const sharp = await loadSharp(maxInputPixels); |
| 124 | + const compressionLevel = params.compressionLevel ?? 6; |
| 125 | + return await sharp(params.buffer) |
| 126 | + .rotate() |
| 127 | + .resize({ |
| 128 | + width: params.maxSide, |
| 129 | + height: params.maxSide, |
| 130 | + fit: "inside", |
| 131 | + withoutEnlargement: params.withoutEnlargement !== false, |
| 132 | + }) |
| 133 | + .png({ compressionLevel }) |
| 134 | + .toBuffer(); |
| 135 | + }, |
| 136 | + }; |
| 137 | +} |
0 commit comments