Reject only real handoff frames, return 400 with reason, keep the new still in the client library, and surface missing-still errors from Generate instead of 500s. Co-authored-by: Cursor <cursoragent@cursor.com>
52 lines
1.8 KiB
TypeScript
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, or WebP' })
|
|
}
|
|
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 }
|
|
})
|