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).
This commit is contained in:
juancarlos.cavero 2026-07-16 19:44:24 +02:00
parent 3f6ee50b86
commit 687412a860
3 changed files with 41 additions and 1 deletions

View file

@ -62,6 +62,7 @@ async def history(request: Request):
items.append({ items.append({
"id": str(v.id), "id": str(v.id),
"job_id": v.job_id, "job_id": v.job_id,
"clip_index": v.clip_index,
"title": v.title, "title": v.title,
"created_at": v.created_at.isoformat() if v.created_at else None, "created_at": v.created_at.isoformat() if v.created_at else None,
"size_bytes": v.size_bytes, "size_bytes": v.size_bytes,

View file

@ -150,6 +150,9 @@ function App() {
const [showTopUp, setShowTopUp] = useState(false); const [showTopUp, setShowTopUp] = useState(false);
const [showTrialUpgrade, setShowTrialUpgrade] = useState(false); const [showTrialUpgrade, setShowTrialUpgrade] = useState(false);
const [topUpInfo, setTopUpInfo] = useState({}); 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') || ''); const [apiKey, setApiKey] = useState(localStorage.getItem('gemini_key') || '');
// Social API State - Load encrypted or plain // Social API State - Load encrypted or plain
@ -280,6 +283,24 @@ function App() {
} }
}, [uploadPostKey]); }, [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(() => { useEffect(() => {
let interval; let interval;
if ((status === 'processing' || status === 'completed') && jobId) { if ((status === 'processing' || status === 'completed') && jobId) {
@ -1141,6 +1162,7 @@ function App() {
clip={clip} clip={clip}
index={i} index={i}
jobId={jobId} jobId={jobId}
durableUrl={durableClips[i]}
uploadPostKey={uploadPostKey} uploadPostKey={uploadPostKey}
uploadUserId={uploadUserId} uploadUserId={uploadUserId}
geminiApiKey={apiKey} geminiApiKey={apiKey}

View file

@ -7,12 +7,22 @@ import HookModal from './HookModal';
import TranslateModal from './TranslateModal'; import TranslateModal from './TranslateModal';
import { renderInBrowser } from '../lib/renderInBrowser'; 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 [showModal, setShowModal] = useState(false);
const [showSubtitleModal, setShowSubtitleModal] = useState(false); const [showSubtitleModal, setShowSubtitleModal] = useState(false);
const videoRef = React.useRef(null); const videoRef = React.useRef(null);
const originalVideoUrl = getApiUrl(clip.video_url); // Never changes — used for Remotion previews const originalVideoUrl = getApiUrl(clip.video_url); // Never changes — used for Remotion previews
const [currentVideoUrl, setCurrentVideoUrl] = useState(originalVideoUrl); 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({ const [platforms, setPlatforms] = useState({
tiktok: true, tiktok: true,
@ -393,6 +403,13 @@ export default function ResultCard({ clip, index, jobId, uploadPostKey, uploadUs
controls controls
className="w-full h-full object-cover" className="w-full h-full object-cover"
playsInline 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={() => { onPlay={() => {
const currentTime = videoRef.current ? videoRef.current.currentTime : 0; const currentTime = videoRef.current ? videoRef.current.currentTime : 0;
onPlay && onPlay(clip.start + currentTime); onPlay && onPlay(clip.start + currentTime);