All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m5s
- /api/streams/list: proxy a streams.nucleoriofrio.com/api/streams - /api/frigate/event: proxy a camaras.nucleoriofrio.com/api/events - Actualizar composables para usar los proxies del backend - Los iframes de streaming siguen usando URLs directas (sesion propia)
100 lines
2.4 KiB
TypeScript
100 lines
2.4 KiB
TypeScript
/**
|
|
* Proxy endpoint para crear eventos en Frigate
|
|
* POST /api/frigate/event
|
|
* Body: { camera: string, label: string, sub_label?: string, duration?: number, include_recording?: boolean }
|
|
*/
|
|
|
|
interface EventRequestBody {
|
|
camera: string
|
|
label: string
|
|
sub_label?: string
|
|
duration?: number
|
|
include_recording?: boolean
|
|
}
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
// Verificar autenticación via headers de Authentik
|
|
const headers = getRequestHeaders(event)
|
|
const username = headers['x-authentik-username']
|
|
|
|
if (!username) {
|
|
throw createError({
|
|
statusCode: 401,
|
|
message: 'No autenticado'
|
|
})
|
|
}
|
|
|
|
// Leer body
|
|
const body = await readBody<EventRequestBody>(event)
|
|
|
|
if (!body.camera || !body.label) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: 'Se requiere camera y label'
|
|
})
|
|
}
|
|
|
|
// Extraer nombre base de la cámara (sin _main o _sub)
|
|
const cameraName = body.camera.replace(/_main$|_sub$/, '')
|
|
|
|
// Preparar payload para Frigate
|
|
const frigatePayload: Record<string, unknown> = {
|
|
label: body.label
|
|
}
|
|
|
|
if (body.sub_label) {
|
|
frigatePayload.sub_label = body.sub_label
|
|
}
|
|
|
|
if (body.duration) {
|
|
frigatePayload.duration = body.duration
|
|
}
|
|
|
|
if (body.include_recording !== undefined) {
|
|
frigatePayload.include_recording = body.include_recording
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(
|
|
`https://camaras.nucleoriofrio.com/api/events/${cameraName}/create`,
|
|
{
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json'
|
|
},
|
|
body: JSON.stringify(frigatePayload)
|
|
}
|
|
)
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text()
|
|
console.error('[Frigate Proxy] Error response:', errorText)
|
|
throw createError({
|
|
statusCode: response.status,
|
|
message: `Error al crear evento: ${response.statusText}`
|
|
})
|
|
}
|
|
|
|
const data = await response.json()
|
|
|
|
console.log(`[Frigate Proxy] Evento creado por ${username}: ${body.label} en ${cameraName}`)
|
|
|
|
return {
|
|
success: true,
|
|
...data
|
|
}
|
|
} catch (error: unknown) {
|
|
console.error('[Frigate Proxy] Error:', error)
|
|
|
|
if ((error as any).statusCode) {
|
|
throw error
|
|
}
|
|
|
|
throw createError({
|
|
statusCode: 500,
|
|
message: (error as Error)?.message || 'Error al crear evento'
|
|
})
|
|
}
|
|
})
|