|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | +import { createClient } from "@supabase/supabase-js"; |
| 3 | + |
| 4 | +const supabase = createClient( |
| 5 | + process.env.NEXT_PUBLIC_SUPABASE_URL!, |
| 6 | + process.env.SUPABASE_SERVICE_ROLE_KEY! |
| 7 | +); |
| 8 | + |
| 9 | +/** |
| 10 | + * Mux Webhook handler. |
| 11 | + * Listens for video lifecycle events: |
| 12 | + * - video.upload.asset_created → insert row with mux_upload_id for tracking |
| 13 | + * - video.asset.created → row created, still processing |
| 14 | + * - video.asset.ready → asset playback is live, update status + metadata |
| 15 | + * - video.asset.errored → mark as errored so admin can retry |
| 16 | + * |
| 17 | + * We do NOT verify the Mux webhook signature here for simplicity; in production, |
| 18 | + * you can add Mux webhook signature verification using the mux-node SDK. |
| 19 | + */ |
| 20 | +export async function POST(req: Request) { |
| 21 | + try { |
| 22 | + const body = await req.json(); |
| 23 | + const type = body.type as string; |
| 24 | + const data = body.data as any; |
| 25 | + |
| 26 | + console.log(`[Mux Webhook] Event: ${type}`, { assetId: data?.id }); |
| 27 | + |
| 28 | + if (type === "video.upload.asset_created") { |
| 29 | + // Upload has been received by Mux, asset creation started |
| 30 | + const uploadId = body.object?.id || data?.upload_id; |
| 31 | + const assetId = data?.asset_id || data?.id; |
| 32 | + |
| 33 | + if (assetId && uploadId) { |
| 34 | + // Update the row that was pre-inserted by the admin UI via upload ID |
| 35 | + const { error } = await supabase |
| 36 | + .from("videos") |
| 37 | + .update({ mux_asset_id: assetId, status: "processing" }) |
| 38 | + .eq("mux_upload_id", uploadId); |
| 39 | + |
| 40 | + if (error) console.error("[Mux Webhook] upload.asset_created update error:", error); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (type === "video.asset.created") { |
| 45 | + // Asset record created in Mux — may not have playback_id yet |
| 46 | + const playbackId = data?.playback_ids?.[0]?.id ?? null; |
| 47 | + const uploadId = data?.upload_id ?? null; |
| 48 | + |
| 49 | + // Try to upsert by mux_asset_id in case a row already exists from upload phase |
| 50 | + const { error } = await supabase |
| 51 | + .from("videos") |
| 52 | + .upsert( |
| 53 | + { |
| 54 | + mux_asset_id: data.id, |
| 55 | + mux_playback_id: playbackId ?? "", |
| 56 | + mux_upload_id: uploadId, |
| 57 | + status: "processing", |
| 58 | + thumbnail_url: playbackId |
| 59 | + ? `https://image.mux.com/${playbackId}/thumbnail.jpg` |
| 60 | + : null, |
| 61 | + }, |
| 62 | + { onConflict: "mux_asset_id" } |
| 63 | + ); |
| 64 | + |
| 65 | + if (error) console.error("[Mux Webhook] asset.created upsert error:", error); |
| 66 | + } |
| 67 | + |
| 68 | + if (type === "video.asset.ready") { |
| 69 | + // Asset is live — update status, playback_id, and metadata |
| 70 | + const playbackId = data?.playback_ids?.[0]?.id ?? null; |
| 71 | + |
| 72 | + const { error } = await supabase |
| 73 | + .from("videos") |
| 74 | + .update({ |
| 75 | + status: "ready", |
| 76 | + mux_playback_id: playbackId ?? "", |
| 77 | + thumbnail_url: playbackId |
| 78 | + ? `https://image.mux.com/${playbackId}/thumbnail.jpg` |
| 79 | + : null, |
| 80 | + duration: data?.duration ?? null, |
| 81 | + aspect_ratio: data?.aspect_ratio ?? null, |
| 82 | + updated_at: new Date().toISOString(), |
| 83 | + }) |
| 84 | + .eq("mux_asset_id", data.id); |
| 85 | + |
| 86 | + if (error) console.error("[Mux Webhook] asset.ready update error:", error); |
| 87 | + else console.log(`[Mux Webhook] Asset ready: ${data.id} (playback: ${playbackId})`); |
| 88 | + } |
| 89 | + |
| 90 | + if (type === "video.asset.errored") { |
| 91 | + const { error } = await supabase |
| 92 | + .from("videos") |
| 93 | + .update({ status: "errored", updated_at: new Date().toISOString() }) |
| 94 | + .eq("mux_asset_id", data.id); |
| 95 | + |
| 96 | + if (error) console.error("[Mux Webhook] asset.errored update error:", error); |
| 97 | + else console.error(`[Mux Webhook] Asset ERRORED: ${data.id}`); |
| 98 | + } |
| 99 | + |
| 100 | + return NextResponse.json({ received: true }); |
| 101 | + } catch (err: any) { |
| 102 | + console.error("[Mux Webhook] Unhandled error:", err); |
| 103 | + return NextResponse.json({ error: err.message }, { status: 500 }); |
| 104 | + } |
| 105 | +} |
0 commit comments