|
| 1 | +/** |
| 2 | + * 浏览器端视频上传分析(PWA 视频转笔记主力通道) |
| 3 | + * |
| 4 | + * @ai-context: 移动端 PWA 的"视频 → AI 笔记"链路(Spec §3.2):文件选择 → |
| 5 | + * fetch multipart 上传到网关 `/api/v1/multimodal/analyze-video`(Gemini 原生 |
| 6 | + * 视频 → Qwen-VL 抽帧降级,300s 超时)→ 结果持久化 classroomNoteStore。 |
| 7 | + * 桌面端(Electron)走 IPC ai_video_analyze(主进程读文件),本函数仅供 |
| 8 | + * PWA/浏览器环境直接上传 File 对象。附抖音链接检测:粘贴分享链接时引导 |
| 9 | + * 用户"保存到相册再导入"(方案 A,不做服务端自动解析)。 |
| 10 | + * @ai-context EN: mobile PWA video→note pipeline — file picker → multipart |
| 11 | + * upload to gateway analyze-video (Gemini native video with Qwen-VL frame |
| 12 | + * fallback, 300s timeout) → persist via classroomNoteStore. Desktop uses the |
| 13 | + * IPC ai_video_analyze path; this function only serves PWA/browser File upload. |
| 14 | + * Includes Douyin link detection to guide "save to album then import" (plan A). |
| 15 | + */ |
| 16 | +import { supabase } from '@/lib/auth/supabaseClient'; |
| 17 | +import { requireGatewayUrl } from '@/lib/ai/config'; |
| 18 | +import { classroomNoteStore } from '@/lib/storage/classroomNoteStore'; |
| 19 | +import type { AnalyzeResult } from '@/lib/ai/sessionAnalyzer'; |
| 20 | + |
| 21 | +/** 抖音分享链接域名(v.douyin.com 短链 + www.douyin.com 长链) */ |
| 22 | +const DOUYIN_HOST_RE = /(^|\.)douyin\.com$/; |
| 23 | + |
| 24 | +/** 判断 URL 是否为抖音分享链接(用于粘贴框引导) */ |
| 25 | +export function isDouyinUrl(url: string): boolean { |
| 26 | + try { |
| 27 | + const host = new URL(url.trim()).hostname.toLowerCase(); |
| 28 | + return DOUYIN_HOST_RE.test(host); |
| 29 | + } catch { |
| 30 | + return false; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * 上传视频文件到网关做多模态分析并持久化笔记 |
| 36 | + * @param file - 用户选择的视频文件(手机录屏/网课下载/抖音保存的视频) |
| 37 | + * @param options - 可选时长(秒)与语言 |
| 38 | + */ |
| 39 | +export async function analyzeVideoFile( |
| 40 | + file: File, |
| 41 | + options?: { duration?: number; language?: string }, |
| 42 | +): Promise<AnalyzeResult> { |
| 43 | + const { data: { session } } = await supabase.auth.getSession(); |
| 44 | + const token = session?.access_token; |
| 45 | + const baseUrl = requireGatewayUrl(); |
| 46 | + |
| 47 | + const formData = new FormData(); |
| 48 | + formData.append('video_file', file, file.name); |
| 49 | + if (options?.duration !== undefined) formData.append('duration', String(options.duration)); |
| 50 | + if (options?.language) formData.append('language', options.language); |
| 51 | + |
| 52 | + const headers = new Headers(); |
| 53 | + if (token) headers.set('Authorization', `Bearer ${token}`); |
| 54 | + headers.set('X-Request-ID', crypto.randomUUID()); |
| 55 | + // 不显式设置 Content-Type:由浏览器为 FormData 自动生成 multipart boundary |
| 56 | + |
| 57 | + const controller = new AbortController(); |
| 58 | + const timeoutId = setTimeout(() => controller.abort(), 300000); // 与网关 300s 超时对齐 |
| 59 | + |
| 60 | + try { |
| 61 | + const resp = await fetch(`${baseUrl}/api/v1/multimodal/analyze-video`, { |
| 62 | + method: 'POST', |
| 63 | + headers, |
| 64 | + body: formData, |
| 65 | + signal: controller.signal, |
| 66 | + }); |
| 67 | + if (!resp.ok) throw new Error(`HTTP ${resp.status}`); |
| 68 | + const data = await resp.json() as { |
| 69 | + content: string; |
| 70 | + keyframes_analyzed: number; |
| 71 | + model_used: string; |
| 72 | + }; |
| 73 | + |
| 74 | + const analyzeResult: AnalyzeResult = { |
| 75 | + content: data.content, |
| 76 | + keyframesAnalyzed: data.keyframes_analyzed, |
| 77 | + modelUsed: data.model_used, |
| 78 | + source: 'remote', |
| 79 | + }; |
| 80 | + |
| 81 | + // 自动持久化(Dexie,PWA/Electron 通用) |
| 82 | + try { |
| 83 | + await classroomNoteStore.create({ |
| 84 | + sessionId: crypto.randomUUID(), |
| 85 | + title: `视频笔记 ${new Date().toLocaleString('zh-CN')}`, |
| 86 | + content: analyzeResult.content, |
| 87 | + keyframesAnalyzed: analyzeResult.keyframesAnalyzed, |
| 88 | + modelUsed: analyzeResult.modelUsed, |
| 89 | + sourceType: 'video', |
| 90 | + duration: options?.duration ?? 0, |
| 91 | + }); |
| 92 | + } catch (e) { |
| 93 | + console.warn('[uploadVideoAnalysis] 视频笔记持久化失败:', e); |
| 94 | + } |
| 95 | + |
| 96 | + return analyzeResult; |
| 97 | + } finally { |
| 98 | + clearTimeout(timeoutId); |
| 99 | + } |
| 100 | +} |
0 commit comments