Skip to content

Open source file-system and knowledge based agent template. Build AI agents that stay up to date with your knowledge base

feat: add img compressing (#8)

Hugob817f09e8029
diff --git a/apps/chat/server/api/sources/ocr.post.ts b/apps/chat/server/api/sources/ocr.post.ts
index ce5f874..1930183 100644
--- a/apps/chat/server/api/sources/ocr.post.ts
+++ b/apps/chat/server/api/sources/ocr.post.ts
@@ -1,6 +1,8 @@
import { generateText, Output } from 'ai'
import { z } from 'zod'
import type { SourceOcrItem } from '~~/shared/utils/source-ocr'
+import { IMAGE_OPTIMIZATION_CONFIG } from '~~/shared/utils/file'
+import { optimizeImage } from '~~/server/utils/image/optimize'
const bodySchema = z.object({
images: z.array(z.string()).optional().default([]),
@@ -116,6 +118,17 @@ function sanitizeSource(source: SourceOcrItem): SourceOcrItem | null {
}
async function extractFromImage(image: string) {
+ const { ocr: ocrConfig } = IMAGE_OPTIMIZATION_CONFIG
+ let optimizedImage: Buffer | string
+
+ try {
+ const result = await optimizeImage(image, ocrConfig)
+ optimizedImage = result.buffer
+ } catch (error) {
+ console.error('Image optimization for OCR failed, using original', { error })
+ optimizedImage = image
+ }
+
const { output } = await generateText({
model: 'google/gemini-3-flash',
output: Output.object({ schema: sourceOcrSchema }),
@@ -124,7 +137,7 @@ async function extractFromImage(image: string) {
role: 'user',
content: [
{ type: 'text', text: systemPrompt },
- { type: 'image', image },
+ { type: 'image', image: optimizedImage },
],
},
],
diff --git a/apps/chat/server/api/upload/[chatId].put.ts b/apps/chat/server/api/upload/[chatId].put.ts
index 57d6fde..c2d8100 100644
--- a/apps/chat/server/api/upload/[chatId].put.ts
+++ b/apps/chat/server/api/upload/[chatId].put.ts
@@ -2,39 +2,91 @@ import { blob } from 'hub:blob'
import { db, schema } from '@nuxthub/db'
import { eq } from 'drizzle-orm'
import { z } from 'zod'
+import { IMAGE_OPTIMIZATION_CONFIG } from '~~/shared/utils/file'
+import { isOptimizableImage, optimizeImage } from '~~/server/utils/image/optimize'
-export default defineEventHandler(async (event) => {
- const { user } = await requireUserSession(event)
+interface FileData {
+ buffer: Buffer
+ type: string
+ filename: string
+}
+
+function isAllowedType(type: string, filename: string): boolean {
+ return type.startsWith('image/')
+ || type === 'application/pdf'
+ || type === 'text/csv'
+ || filename.endsWith('.csv')
+}
- const { chatId } = await getValidatedRouterParams(event, z.object({
- chatId: z.string()
- }).parse)
+function generatePathname(username: string, chatId: string, filename: string): string {
+ const suffix = crypto.randomUUID().slice(0, 8)
+ const ext = filename.match(/(\.[^.]+)$/)?.[1] || ''
+ const base = filename.replace(/\.[^.]+$/, '') || 'file'
+ return `${username}/${chatId}/${base}-${suffix}${ext}`
+}
- const userId = user.id
+async function processFile(file: FileData): Promise<FileData> {
+ if (!isOptimizableImage(file.type)) return file
+
+ try {
+ const config = IMAGE_OPTIMIZATION_CONFIG.storage
+ const result = await optimizeImage(file.buffer, config)
+ const ext = result.mimeType.includes('/') ? result.mimeType.split('/')[1] : 'webp'
+ const base = file.filename.replace(/\.[^.]+$/, '') || 'image'
+
+ return {
+ buffer: result.buffer,
+ type: result.mimeType,
+ filename: `${base}.${ext}`,
+ }
+ } catch (error) {
+ console.error('Image optimization failed, using original file', { filename: file.filename, error })
+ return file
+ }
+}
+
+export default defineEventHandler(async (event) => {
+ const { user } = await requireUserSession(event)
+ const { chatId } = await getValidatedRouterParams(event, z.object({ chatId: z.string() }).parse)
const chat = await db.query.chats.findFirst({
- where: () => eq(schema.chats.id, chatId)
+ where: () => eq(schema.chats.id, chatId),
})
- if (chat && chat.userId !== userId) {
- throw createError({
- statusCode: 403,
- statusMessage: 'You do not have permission to upload files to this chat'
- })
+ if (chat && chat.userId !== user.id) {
+ throw createError({ statusCode: 403, statusMessage: 'You do not have permission to upload files to this chat' })
}
- const { username } = user
-
- return blob.handleUpload(event, {
- formKey: 'files',
- multiple: false,
- ensure: {
- maxSize: FILE_UPLOAD_CONFIG.maxSize,
- types: [...FILE_UPLOAD_CONFIG.types]
- },
- put: {
- addRandomSuffix: true,
- prefix: `${username}/${chatId}`
- }
+ const formData = await readMultipartFormData(event)
+ const field = formData?.find(f => f.name === 'files')
+
+ if (!field?.data || !field?.filename) {
+ throw createError({ statusCode: 400, statusMessage: 'No file provided' })
+ }
+
+ const originalType = field.type || 'application/octet-stream'
+
+ if (!isAllowedType(originalType, field.filename)) {
+ throw createError({ statusCode: 400, statusMessage: `File type ${originalType} is not allowed` })
+ }
+
+ if (field.data.length > 8 * 1024 * 1024) {
+ throw createError({ statusCode: 400, statusMessage: 'File size exceeds 8MB limit' })
+ }
+
+ const processed = await processFile({
+ buffer: field.data,
+ type: originalType,
+ filename: field.filename,
})
+
+ const pathname = generatePathname(user.username, chatId, processed.filename)
+ const result = await blob.put(pathname, processed.buffer, { contentType: processed.type })
+
+ return {
+ pathname: result.pathname,
+ url: result.url || undefined,
+ contentType: processed.type,
+ size: processed.buffer.length,
+ }
})
diff --git a/apps/chat/server/utils/image/optimize.ts b/apps/chat/server/utils/image/optimize.ts
new file mode 100644
index 0000000..c217ee8
--- /dev/null
+++ b/apps/chat/server/utils/image/optimize.ts
@@ -0,0 +1,80 @@
+import sharp from 'sharp'
+
+export interface OptimizeOptions {
+ maxWidth?: number
+ maxHeight?: number
+ quality?: number
+ format?: 'webp' | 'jpeg' | 'png' | 'original'
+}
+
+export interface OptimizeResult {
+ buffer: Buffer
+ mimeType: string
+}
+
+const MIME_TYPES = {
+ webp: 'image/webp',
+ jpeg: 'image/jpeg',
+ png: 'image/png',
+} as const
+
+function parseBase64(input: string): { buffer: Buffer, format: string | null } {
+ const match = input.match(/^data:image\/([^;]+);base64,(.+)$/)
+ if (match) {
+ return { buffer: Buffer.from(match[2]!, 'base64'), format: match[1]! }
+ }
+ return { buffer: Buffer.from(input, 'base64'), format: null }
+}
+
+function toSharpFormat(format: string): 'webp' | 'jpeg' | 'png' {
+ if (format.includes('webp')) return 'webp'
+ if (format.includes('png')) return 'png'
+ return 'jpeg'
+}
+
+function applyCompression(pipeline: sharp.Sharp, format: 'webp' | 'jpeg' | 'png', quality: number) {
+ const q = Math.max(1, Math.min(quality, 100))
+ switch (format) {
+ case 'webp': return pipeline.webp({ quality: q })
+ case 'png': {
+ const compressionLevel = Math.round(((100 - q) / 100) * 9)
+ return pipeline.png({ quality: q, compressionLevel, palette: q < 90 })
+ }
+ default: return pipeline.jpeg({ quality: q, mozjpeg: true })
+ }
+}
+
+export async function optimizeImage(
+ input: Buffer | string,
+ options: OptimizeOptions = {},
+): Promise<OptimizeResult> {
+ const { maxWidth = 2048, maxHeight = 2048, quality = 80, format = 'original' } = options
+
+ const { buffer: inputBuffer, format: detectedFormat } = typeof input === 'string'
+ ? parseBase64(input)
+ : { buffer: input, format: null }
+
+ const metadata = await sharp(inputBuffer).metadata()
+ const sourceFormat = detectedFormat || metadata.format || 'jpeg'
+ const outputFormat = format === 'original' ? toSharpFormat(sourceFormat) : format
+
+ let pipeline = sharp(inputBuffer)
+
+ const needsResize = (metadata.width && metadata.width > maxWidth)
+ || (metadata.height && metadata.height > maxHeight)
+
+ if (needsResize) {
+ pipeline = pipeline.resize(maxWidth, maxHeight, { fit: 'inside', withoutEnlargement: true })
+ }
+
+ pipeline = applyCompression(pipeline, outputFormat, quality)
+
+ return {
+ buffer: await pipeline.toBuffer(),
+ mimeType: MIME_TYPES[outputFormat],
+ }
+}
+
+export function isOptimizableImage(mimeType: string): boolean {
+ return mimeType.startsWith('image/') && !mimeType.includes('svg')
+}
diff --git a/apps/chat/shared/utils/file.ts b/apps/chat/shared/utils/file.ts
index f7934f5..51d84d7 100644
--- a/apps/chat/shared/utils/file.ts
+++ b/apps/chat/shared/utils/file.ts
@@ -14,6 +14,11 @@ export const FILE_UPLOAD_CONFIG = {
acceptPattern: 'image/*,application/pdf,.csv,text/csv'
} as const
+export const IMAGE_OPTIMIZATION_CONFIG = {
+ storage: { maxWidth: 2048, maxHeight: 2048, quality: 80, format: 'webp' as const },
+ ocr: { maxWidth: 1536, maxHeight: 1536, quality: 85, format: 'jpeg' as const },
+} as const
+
export function getFileIcon(mimeType: string, fileName?: string): string {
if (mimeType.startsWith('image/')) return 'i-lucide-image'
if (mimeType === 'application/pdf') return 'i-lucide-file-text'