Files

32 lines
1.2 KiB
TypeScript

import { sendBufferWithRange } from '~/server/utils/httpRange'
export default defineEventHandler(async (event) => {
const query = getQuery(event)
const filename = String(query.filename || '')
if (!filename) {
throw createError({ statusCode: 400, statusMessage: 'filename is required' })
}
const subfolder = String(query.subfolder || 'video')
const type = String(query.type || 'output')
const params = new URLSearchParams({ filename, subfolder, type })
const res = await comfyFetch(`/view?${params.toString()}`)
if (!res.ok) {
throw createError({ statusCode: res.status, statusMessage: 'Failed to fetch media from ComfyUI' })
}
const buf = Buffer.from(await res.arrayBuffer())
const lower = filename.toLowerCase()
const fallbackType = lower.endsWith('.wav')
? 'audio/wav'
: lower.endsWith('.flac')
? 'audio/flac'
: lower.endsWith('.mp3')
? 'audio/mpeg'
: lower.endsWith('.ogg') || lower.endsWith('.opus')
? 'audio/ogg'
: 'video/mp4'
return sendBufferWithRange(event, buf, res.headers.get('content-type') || fallbackType, {
'Content-Disposition': `inline; filename="${filename}"`,
'Cache-Control': 'private, max-age=0, must-revalidate'
})
})