From 687412a860af40553ce9878b08a80d3da132052c Mon Sep 17 00:00:00 2001 From: "juancarlos.cavero" Date: Thu, 16 Jul 2026 19:44:24 +0200 Subject: [PATCH] fix(cloud): preview falls back to durable R2 url when local clip is gone After a reload the ephemeral /videos/ files may be cleaned up (1h retention), leaving black previews on the results screen. For managed users, fetch the job's clips from /api/history (now includes clip_index) and fall back to the durable R2 url on video error (with a retry for the load race). --- cloud/videos.py | 1 + dashboard/src/App.jsx | 22 ++++++++++++++++++++++ dashboard/src/components/ResultCard.jsx | 19 ++++++++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/cloud/videos.py b/cloud/videos.py index 45e81fd..44f0d7d 100644 --- a/cloud/videos.py +++ b/cloud/videos.py @@ -62,6 +62,7 @@ async def history(request: Request): items.append({ "id": str(v.id), "job_id": v.job_id, + "clip_index": v.clip_index, "title": v.title, "created_at": v.created_at.isoformat() if v.created_at else None, "size_bytes": v.size_bytes, diff --git a/dashboard/src/App.jsx b/dashboard/src/App.jsx index 031232d..680808c 100644 --- a/dashboard/src/App.jsx +++ b/dashboard/src/App.jsx @@ -150,6 +150,9 @@ function App() { const [showTopUp, setShowTopUp] = useState(false); const [showTrialUpgrade, setShowTrialUpgrade] = useState(false); const [topUpInfo, setTopUpInfo] = useState({}); + // Durable R2 URLs (per clip index) for the current job — used as a fallback when + // the ephemeral local /videos/ files have been cleaned up (e.g. after a reload). + const [durableClips, setDurableClips] = useState({}); const [apiKey, setApiKey] = useState(localStorage.getItem('gemini_key') || ''); // Social API State - Load encrypted or plain @@ -280,6 +283,24 @@ function App() { } }, [uploadPostKey]); + // For managed users, fetch the durable R2 URLs of the current job's clips so the + // preview can fall back to them when the local files have been cleaned up. + useEffect(() => { + if (!isManaged || !jobId || !(results?.clips?.length)) { setDurableClips({}); return; } + let cancelled = false; + apiJson('/api/history') + .then((d) => { + if (cancelled) return; + const map = {}; + for (const v of (d.videos || [])) { + if (v.job_id === jobId && v.clip_index != null) map[v.clip_index] = v.view_url; + } + setDurableClips(map); + }) + .catch(() => {}); + return () => { cancelled = true; }; + }, [isManaged, jobId, results]); + useEffect(() => { let interval; if ((status === 'processing' || status === 'completed') && jobId) { @@ -1141,6 +1162,7 @@ function App() { clip={clip} index={i} jobId={jobId} + durableUrl={durableClips[i]} uploadPostKey={uploadPostKey} uploadUserId={uploadUserId} geminiApiKey={apiKey} diff --git a/dashboard/src/components/ResultCard.jsx b/dashboard/src/components/ResultCard.jsx index a314880..1d43bb7 100644 --- a/dashboard/src/components/ResultCard.jsx +++ b/dashboard/src/components/ResultCard.jsx @@ -7,12 +7,22 @@ import HookModal from './HookModal'; import TranslateModal from './TranslateModal'; import { renderInBrowser } from '../lib/renderInBrowser'; -export default function ResultCard({ clip, index, jobId, uploadPostKey, uploadUserId, geminiApiKey, elevenLabsKey, onPlay, onPause }) { +export default function ResultCard({ clip, index, jobId, durableUrl, uploadPostKey, uploadUserId, geminiApiKey, elevenLabsKey, onPlay, onPause }) { const [showModal, setShowModal] = useState(false); const [showSubtitleModal, setShowSubtitleModal] = useState(false); const videoRef = React.useRef(null); const originalVideoUrl = getApiUrl(clip.video_url); // Never changes — used for Remotion previews const [currentVideoUrl, setCurrentVideoUrl] = useState(originalVideoUrl); + const [videoErrored, setVideoErrored] = useState(false); + + // If the local video failed and a durable R2 URL is (now) available, use it. + // Handles the race where the video errors before the durable URL has loaded. + useEffect(() => { + if (videoErrored && durableUrl && currentVideoUrl !== durableUrl) { + setCurrentVideoUrl(durableUrl); + setVideoErrored(false); + } + }, [videoErrored, durableUrl, currentVideoUrl]); const [platforms, setPlatforms] = useState({ tiktok: true, @@ -393,6 +403,13 @@ export default function ResultCard({ clip, index, jobId, uploadPostKey, uploadUs controls className="w-full h-full object-cover" playsInline + onError={() => { + // Local /videos/ file gone (e.g. cleaned up after a reload) → + // fall back to the durable R2 copy for managed users. If the + // durable URL hasn't loaded yet, the effect above retries. + if (durableUrl && currentVideoUrl !== durableUrl) setCurrentVideoUrl(durableUrl); + else setVideoErrored(true); + }} onPlay={() => { const currentTime = videoRef.current ? videoRef.current.currentTime : 0; onPlay && onPlay(clip.start + currentTime);