Files
aigen/server/api/library/stills.post.ts
T
TowstyandCursor 97c7547da9 Let saved library stills queue Edit without a format rejection.
Use catalog width/height or aspect fallback in resolveRequestSize, restore GIF/BMP upload messaging, and keep Photo to change still ids across reload so Klein/Qwen Edit can bind imageAId.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-12 12:45:50 -05:00

52 lines
1.8 KiB
TypeScript

import { imageDimensions } from '../../utils/resolution'
export default defineEventHandler(async (event) => {
const form = await readMultipartFormData(event)
if (!form?.length) {
throw createError({ statusCode: 400, statusMessage: 'Expected an image upload' })
}
const fields: Record<string, string> = {}
let image: { filename: string; data: Buffer; type?: string } | null = null
for (const part of form) {
if (part.name === 'image' && part.filename && part.data?.length) {
image = { filename: part.filename, data: part.data, type: part.type }
} else if (part.name && part.data) {
fields[part.name] = part.data.toString('utf8')
}
}
if (!image) {
throw createError({ statusCode: 400, statusMessage: 'An image is required' })
}
const size = imageDimensions(image.data)
if (!size) {
throw createError({ statusCode: 400, statusMessage: 'Use a PNG, JPEG, WebP, GIF, or BMP image.' })
}
const { owner } = assertLibraryOwner(event)
const library = publicLibrary(event)
const folderId = library.folders.some(folder => folder.id === fields.folderId)
? fields.folderId
: library.folders[0]?.id
if (!folderId) {
throw createError({ statusCode: 400, statusMessage: 'Create a library folder first' })
}
assertFolderExists(event, folderId)
const still = await saveStill({
ownerKey: owner,
folderId,
filename: image.filename,
data: image.data,
width: size.width,
height: size.height,
hideInput: fields.hideInput === 'true',
role: 'input'
})
if (!still?.id) {
throw createError({ statusCode: 400, statusMessage: 'Could not save that image to the library.' })
}
const next = publicLibrary(event)
const stills = next.stills.some(item => item.id === still.id)
? next.stills
: [{ ...still, role: 'input' as const }, ...next.stills]
return { still, ...next, stills }
})