| import { verifyOwnerToken } from '../_owner-auth.js'; |
| import { getConfig } from '../../lib/config.js'; |
| import { getVideoByKey } from '../../services/archive/get-video.js'; |
| import { |
| buildTelegramResponsePayload, |
| buildUserFacingArtifact, |
| } from '../../services/archive/user-facing-artifacts.js'; |
|
|
| function setCors(res) { |
| res.setHeader('Access-Control-Allow-Credentials', 'true'); |
| res.setHeader('Access-Control-Allow-Origin', '*'); |
| res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS'); |
| res.setHeader('Access-Control-Allow-Headers', 'Authorization, Content-Type'); |
| } |
|
|
| function requireOwner(req) { |
| const authHeader = req.headers.authorization || ''; |
| const token = authHeader.startsWith('Bearer ') ? authHeader.slice(7) : ''; |
| const cfg = getConfig(); |
| const auth = verifyOwnerToken(token, cfg.ownerLoginPassword); |
|
|
| if (!auth.valid) { |
| const error = new Error('Unauthorized'); |
| error.status = 401; |
| throw error; |
| } |
| } |
|
|
| export default async function handler(req, res) { |
| setCors(res); |
|
|
| if (req.method === 'OPTIONS') { |
| return res.status(200).end(); |
| } |
|
|
| if (req.method !== 'GET') { |
| return res.status(405).json({ message: 'Method not allowed' }); |
| } |
|
|
| try { |
| requireOwner(req); |
|
|
| const videoKey = String(req.query?.video_key || '').trim(); |
| const view = String(req.query?.view || 'full').trim().toLowerCase(); |
| const includeEditedRaw = req.query?.include_edited; |
| const hasIncludeEdited = typeof includeEditedRaw !== 'undefined'; |
| const includeEdited = ['1', 'true', 'yes'].includes( |
| String(includeEditedRaw || '').trim().toLowerCase() |
| ); |
| if (!videoKey) { |
| return res.status(400).json({ message: 'Missing required query param: video_key' }); |
| } |
|
|
| const result = await getVideoByKey(videoKey); |
| if (!result) { |
| return res.status(404).json({ message: 'Archive video not found' }); |
| } |
|
|
| if (view === 'telegram') { |
| return res.status(200).json( |
| buildTelegramResponsePayload( |
| { |
| video: result.video, |
| videoText: result.video_text, |
| }, |
| { |
| includeEdited, |
| editedCommand: `/get_full ${result.video.video_key}`, |
| } |
| ) |
| ); |
| } |
|
|
| if (view === 'artifact') { |
| return res.status(200).json( |
| buildUserFacingArtifact( |
| { |
| video: result.video, |
| videoText: result.video_text, |
| }, |
| { |
| includeEdited: hasIncludeEdited ? includeEdited : true, |
| } |
| ) |
| ); |
| } |
|
|
| return res.status(200).json(result); |
| } catch (error) { |
| return res.status(Number(error?.status) || 500).json({ |
| message: error?.message || 'Failed to fetch archive video', |
| }); |
| } |
| } |
|
|