gongxiangp2/lib/brand-knowledge-preview.ts
2026-09-14 20:29:32 +08:00

88 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sharp from 'sharp'
import { sliceImage } from '@/lib/image-processor'
import { configureSharpRuntime } from '@/lib/sharp-runtime'
configureSharpRuntime()
export const BRAND_KNOWLEDGE_PREVIEW_MAX_WIDTH = 12_000
export const BRAND_KNOWLEDGE_PREVIEW_MAX_HEIGHT = 90_000
export const BRAND_KNOWLEDGE_PREVIEW_MAX_PIXELS = 60_000_000
export const BRAND_KNOWLEDGE_PREVIEW_SLICE_HEIGHT = 1_500
export const BRAND_KNOWLEDGE_PREVIEW_SLICE_MAX_KB = 500
export const BRAND_KNOWLEDGE_PREVIEW_OUTPUT_WIDTH = 1_600
const FORMAT_DETAILS = {
png: { extension: 'png', contentType: 'image/png' },
jpeg: { extension: 'jpg', contentType: 'image/jpeg' },
webp: { extension: 'webp', contentType: 'image/webp' },
} as const
type SupportedPreviewFormat = keyof typeof FORMAT_DETAILS
function normalizeContentType(value?: string | null) {
return String(value || '').split(';', 1)[0].trim().toLowerCase()
}
export function assertBrandKnowledgePreviewMetadata(
metadata: Pick<sharp.Metadata, 'format' | 'width' | 'height' | 'pages'>,
declaredContentType?: string | null,
) {
const format = metadata.format as SupportedPreviewFormat | undefined
const details = format ? FORMAT_DETAILS[format] : undefined
if (!details) throw new Error('预览图只支持 PNG、JPEG 或 WebP')
const width = metadata.width || 0
const height = metadata.height || 0
if (width < 1 || height < 1) throw new Error('无法获取预览图尺寸')
if (width > BRAND_KNOWLEDGE_PREVIEW_MAX_WIDTH || height > BRAND_KNOWLEDGE_PREVIEW_MAX_HEIGHT) {
throw new Error(`预览图尺寸过大,宽度需不超过 ${BRAND_KNOWLEDGE_PREVIEW_MAX_WIDTH}px,高度需不超过 ${BRAND_KNOWLEDGE_PREVIEW_MAX_HEIGHT}px`)
}
if (width * height > BRAND_KNOWLEDGE_PREVIEW_MAX_PIXELS) {
throw new Error('预览图像素总量过大,需不超过 6000 万像素')
}
if (Number(metadata.pages || 1) > 1) throw new Error('预览图不支持动画或多页图片')
const declared = normalizeContentType(declaredContentType)
if (declared && declared !== details.contentType) throw new Error('预览图内容与声明格式不一致')
return { ...details, width, height }
}
export async function prepareBrandKnowledgePreview(buffer: Buffer, declaredContentType?: string | null) {
let metadata: sharp.Metadata
try {
metadata = await sharp(buffer, {
animated: true,
failOn: 'error',
limitInputPixels: BRAND_KNOWLEDGE_PREVIEW_MAX_PIXELS,
sequentialRead: true,
}).metadata()
} catch (error) {
if (error instanceof Error && /pixel limit/i.test(error.message)) {
throw new Error('预览图像素总量过大,需不超过 6000 万像素')
}
throw new Error('预览图无法解码,请上传完整的 PNG、JPEG 或 WebP 图片')
}
const source = assertBrandKnowledgePreviewMetadata(metadata, declaredContentType)
let slices: Buffer[]
try {
slices = await sliceImage(
buffer,
BRAND_KNOWLEDGE_PREVIEW_SLICE_HEIGHT,
BRAND_KNOWLEDGE_PREVIEW_SLICE_MAX_KB,
BRAND_KNOWLEDGE_PREVIEW_OUTPUT_WIDTH,
{
maxInputWidth: BRAND_KNOWLEDGE_PREVIEW_MAX_WIDTH,
maxInputHeight: BRAND_KNOWLEDGE_PREVIEW_MAX_HEIGHT,
maxInputPixels: BRAND_KNOWLEDGE_PREVIEW_MAX_PIXELS,
},
)
} catch {
throw new Error('预览图处理失败,请确认图片完整且未超过尺寸限制')
}
if (slices.length === 0) throw new Error('预览图处理后没有可用片段')
return { source, slices }
}