Delete library stills from disk before the catalog write, and stop caching their media.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-10-25 17:55:30 -05:00
co-authored by Cursor
parent 545a3d749b
commit 20359bfbd4
26 changed files with 635 additions and 247 deletions
+83 -42
View File
@@ -96,6 +96,7 @@
v-for="(card, index) in form.photos"
:key="'photo-'+index+'-'+card.stillId"
:asset="stillById(card.stillId)"
:version="library.catalogEpoch || 0"
:hidden="hideInputs"
:role="card.role"
:role-options="photoRoleOptions"
@@ -316,7 +317,7 @@
v-show="!isHidden(selected)"
ref="viewerVideo"
:key="selected.id"
:src="`/api/library/clips/${selected.id}/video`"
:src="clipVideoUrl(selected.id)"
:muted="viewerMuted"
@timeupdate="onVideoTime"
@loadedmetadata="onVideoTime"
@@ -476,7 +477,7 @@
<div class="grid grid-cols-2 gap-3 sm:grid-cols-4 lg:grid-cols-5">
<button v-for="a in pickerAssets" :key="a.id" type="button" class="rounded-[var(--radius)] border border-[var(--line)] p-2 text-left" @click="pickAsset(a)">
<span v-if="pickerThumbHidden(a)" class="flex h-24 items-center text-xs text-[var(--muted)]">Still loaded · preview hidden</span>
<img v-else :src="a.kind==='clip'?'/api/library/clips/'+a.id+'/thumb':stillThumbUrl(a.id)" class="h-24 w-full object-contain" alt="">
<img v-else :src="a.kind==='clip'?clipThumbUrl(a.id):stillThumbUrl(a.id)" class="h-24 w-full object-contain" alt="">
<span class="mt-2 block truncate text-xs">{{ assetLabel(a) }}</span>
</button>
</div>
@@ -491,7 +492,7 @@ import {createLoraMemory,loraUse} from '~/shared/studio2/lora-memory.mjs'
import {filterLorasForStudio,filterLorasForUse} from '~/utils/loras'
import {sectionsFrom} from '~/shared/studio2/contracts.mjs'
import {aspectOptions,resolveSize,bindClip,restoreAll,useAsSource,hydrateStudio2Job} from '~/shared/studio2/input.mjs'
import {folderAssets,newestOutput,assetLabel,reconcileSelection,previewHidden,folderImageCount,libraryAfterWipe} from '~/shared/studio2/viewer.mjs'
import {folderAssets,newestOutput,assetLabel,reconcileSelection,previewHidden,folderImageCount,libraryAfterWipe,libraryMediaUrl} from '~/shared/studio2/viewer.mjs'
import {queueJobs,queueEngineLabel,jobStageLabel,jobProgressView,elapsedSeconds} from '~/shared/studio2/queue-view.mjs'
import {gpuStatus} from '~/shared/studio2/status.mjs'
import {CAPTION_STYLES} from '~/shared/caption.mjs'
@@ -921,9 +922,12 @@ watch(()=>form.folderId,(id,prev)=>{
watch(()=>form.identityStillId,id=>{if(import.meta.client&&form.folderId&&form.engine!=='qwen21')localStorage.setItem(`studio2-hero:${form.folderId}`,id)})
const stills=computed(()=>library.value.stills.filter((s:any)=>s.folderId===form.folderId))
const clips=computed(()=>library.value.clips.filter((s:any)=>s.folderId===form.folderId))
const stillUrl=(id:string)=>`/api/library/stills/${id}`
const stillThumbUrl=(id:string)=>`/api/library/stills/${id}/thumb`
const stillPreviewUrl=(id:string)=>`/api/library/stills/${id}/preview`
const mediaVersion=()=>library.value.catalogEpoch ?? 0
const stillUrl=(id:string)=>libraryMediaUrl(`/api/library/stills/${id}`,mediaVersion())
const stillThumbUrl=(id:string)=>libraryMediaUrl(`/api/library/stills/${id}/thumb`,mediaVersion())
const stillPreviewUrl=(id:string)=>libraryMediaUrl(`/api/library/stills/${id}/preview`,mediaVersion())
const clipThumbUrl=(id:string)=>libraryMediaUrl(`/api/library/clips/${id}/thumb`,mediaVersion())
const clipVideoUrl=(id:string)=>libraryMediaUrl(`/api/library/clips/${id}/video`,mediaVersion())
/** Stage uses /preview (1600px). Tiles keep /thumb. Never auto-upgrade to the archive master. */
const stagePreviewNonce=ref(0)
const stagePreviewRetries=ref(0)
@@ -931,7 +935,7 @@ const stagePreviewSrc=computed(()=>{
const id=selected.value?.id
if(!id || selected.value?.kind==='clip' || selected.value?.kind==='track') return ''
const base=stillPreviewUrl(id)
return stagePreviewNonce.value?`${base}?t=${stagePreviewNonce.value}`:base
return stagePreviewNonce.value?`${base}&t=${stagePreviewNonce.value}`:base
})
function onStagePreviewError(){
if(!selected.value?.id || stagePreviewRetries.value>=8) return
@@ -982,10 +986,7 @@ const showSelectedStill=computed(()=>{
const item=selected.value
if(!item?.id || item.kind==='clip' || item.kind==='track') return false
if(forgottenStillIds.value.has(item.id)) return false
if((library.value.stills||[]).some((s:any)=>s.id===item.id)) return true
const folder=(library.value.folders||[]).find((f:any)=>f.id===(item.folderId||form.folderId))
if(folder && typeof folder.imageCount==='number' && folder.imageCount===0) return false
return true
return (library.value.stills||[]).some((s:any)=>s.id===item.id)
})
const sourceAsset=computed(()=>form.startClipId?clips.value.find((c:any)=>c.id===form.startClipId):stills.value.find((s:any)=>s.id===(form.imageAId||form.identityStillId||form.photos?.[0]?.stillId)))
const sourceSize=computed(()=>measured[sourceAsset.value?.id] || sourceAsset.value)
@@ -1133,6 +1134,30 @@ function clearFormSlots(ids:string[]){
selectionPinned.value=false
}
}
function clearRefsMissingFromLibrary(){
const ids=new Set((library.value.stills||[]).map((s:any)=>s.id))
const clipIds=new Set((library.value.clips||[]).map((c:any)=>c.id))
const trackIds=new Set((library.value.tracks||[]).map((t:any)=>t.id))
const missing=[
form.imageAId,form.imageBId,form.identityStillId,form.maskId,form.endStillId,
...(form.photos||[]).map((p:any)=>p?.stillId),
...(form.guides||[]).map((g:any)=>g?.stillId)
].filter((id:string)=>id&&!ids.has(id))
clearFormSlots(missing)
if(form.startClipId&&!clipIds.has(form.startClipId))form.startClipId=''
if(form.identityStillId&&!ids.has(form.identityStillId)){
form.identityStillId=''
try{localStorage.removeItem(`studio2-hero:${form.folderId}`)}catch{/* ignore */}
}
const sel=selected.value
if(sel?.id){
const ok=sel.kind==='clip'?clipIds.has(sel.id):sel.kind==='track'?trackIds.has(sel.id):ids.has(sel.id)
if(!ok){
selected.value=null
selectionPinned.value=false
}
}
}
/** Photos array is source of truth — never resurrect cards from leftover imageAId. */
function syncPhotosBindings(){
const filled=(form.photos||[]).filter((p:any)=>p?.stillId)
@@ -1495,6 +1520,7 @@ async function deleteOutput(explicit?:any){
return
}
}
seenOutputs.add(id)
forgottenStillIds.value=new Set([...forgottenStillIds.value,id])
clearFormSlots([id])
library.value={
@@ -1503,9 +1529,13 @@ async function deleteOutput(explicit?:any){
clips:(library.value.clips||[]).filter((c:any)=>c.id!==id),
tracks:(library.value.tracks||[]).filter((t:any)=>t.id!==id)
}
if(selected.value?.id===id)selected.value=null
if(selected.value?.id===id){
selected.value=null
selectionPinned.value=false
}
libraryRefreshKey.value+=1
try{await loadLibraryOnce()}catch{/* catalog write already landed */}
try{await loadLibraryOnce([id])}catch{/* local rows already dropped */ }
forgottenStillIds.value=new Set()
await refreshJobs()
}
function stepFamily(delta:number){
@@ -1549,19 +1579,24 @@ function onLibraryDelete(item:any){
}
function onLibraryWiped(result:any){
const folderId=String(result?.folderId||form.folderId||'')
const deleted=[
...(result?.deletedIds||[]),
...(library.value.stills||[]).filter((s:any)=>String(s.folderId||'')===folderId).map((s:any)=>s.id),
...(library.value.clips||[]).filter((s:any)=>String(s.folderId||'')===folderId).map((s:any)=>s.id),
...(library.value.tracks||[]).filter((s:any)=>String(s.folderId||'')===folderId).map((s:any)=>s.id)
]
for(const id of deleted) seenOutputs.add(id)
const next=libraryAfterWipe({
folders:library.value.folders,
stills:library.value.stills,
clips:library.value.clips,
tracks:library.value.tracks,
suppressedIds:[...forgottenStillIds.value]
suppressedIds:[]
}, result, folderId)
forgottenStillIds.value=new Set(next.suppressedIds)
clearFormSlots(next.suppressedIds)
if(selected.value && (forgottenStillIds.value.has(selected.value.id) || String(selected.value.folderId||'')===folderId)){
selected.value=null
selectionPinned.value=false
}
forgottenStillIds.value=new Set(deleted)
clearFormSlots(deleted)
selected.value=null
selectionPinned.value=false
library.value={
...library.value,
folders:next.folders,
@@ -1571,10 +1606,12 @@ function onLibraryWiped(result:any){
drafts:result?.drafts||[],
loraAliases:result?.loraAliases||library.value.loraAliases||{},
loraTriggers:result?.loraTriggers||library.value.loraTriggers||{},
keepPrompts:result?.keepPrompts||library.value.keepPrompts
keepPrompts:result?.keepPrompts||library.value.keepPrompts,
catalogEpoch:result?.catalogEpoch??library.value.catalogEpoch??0
}
clearRefsMissingFromLibrary()
libraryRefreshKey.value+=1
void loadLibraryOnce()
void loadLibraryOnce(deleted).finally(()=>{forgottenStillIds.value=new Set()})
}
function onStageHotkey(e:KeyboardEvent){
if(e.metaKey||e.ctrlKey||e.altKey)return
@@ -1714,7 +1751,7 @@ watch(()=>sourceAsset.value?.id,id=>{
media.addEventListener('loadedmetadata',()=>{
measured[asset.id]={width:media.videoWidth,height:media.videoHeight}
},{once:true})
media.src=`/api/library/clips/${asset.id}/video`
media.src=clipVideoUrl(asset.id)
}
})
watch(()=>selected.value?.id,()=>{videoTime.value=0;videoDuration.value=0})
@@ -2013,50 +2050,51 @@ async function cancel(job:any){
}
let libraryLoadGen=0
async function loadLibraryOnce(){
async function loadLibraryOnce(deletedIds?: string[]){
// Current folder page only — never the yearly Inbox JSON on /
// After delete/wipe, replace from this response. Do not merge the pre-delete list.
const replacing=Array.isArray(deletedIds)
const gone=new Set(deletedIds||[])
const gen=++libraryLoadGen
const qs=new URLSearchParams({limit:'50'})
if(form.folderId) qs.set('folderId',form.folderId)
qs.set('_',String(Date.now()))
const page=await $fetch<any>(`/api/library?${qs.toString()}`)
if(gen!==libraryLoadGen) return
const pageStills=page.stills||[]
const pageStills=(page.stills||[]).filter((s:any)=>s?.id&&!gone.has(s.id))
const forget=forgottenStillIds.value
const keepIds=[
...(form.photos||[]).map((p:any)=>p?.stillId).filter(Boolean),
form.imageAId,form.imageBId,form.identityStillId,form.maskId,form.endStillId,
...(form.guides||[]).map((g:any)=>g?.stillId).filter(Boolean)
].filter((id:string)=>id&&!forget.has(id)) as string[]
let kept=(library.value.stills||[]).filter((s:any)=>keepIds.includes(s.id)&&!pageStills.some((p:any)=>p.id===s.id)&&!forget.has(s.id))
const stillMissing=keepIds.filter(id=>!forget.has(id)&&!pageStills.some((s:any)=>s.id===id)&&!kept.some((s:any)=>s.id===id))
// Draft / upload stills must survive reload even when they fall outside the first page
].filter((id:string)=>id&&!forget.has(id)&&!gone.has(id)) as string[]
let kept=replacing?[]:(library.value.stills||[]).filter((s:any)=>keepIds.includes(s.id)&&!pageStills.some((p:any)=>p.id===s.id)&&!forget.has(s.id)&&!gone.has(s.id))
const stillMissing=keepIds.filter(id=>!pageStills.some((s:any)=>s.id===id)&&!kept.some((s:any)=>s.id===id))
let epoch=page.catalogEpoch
// Draft / upload stills must survive reload even when they fall outside the first page.
// A delete replace only accepts ids the fresh catalog still returns.
if(stillMissing.length){
try{
const full=await $fetch<any>(`/api/library?light=1&_=${Date.now()}`)
if(gen!==libraryLoadGen) return
const recovered=(full.stills||[]).filter((s:any)=>stillMissing.includes(s.id)&&!forget.has(s.id))
if(typeof full?.catalogEpoch==='number') epoch=full.catalogEpoch
const recovered=(full.stills||[]).filter((s:any)=>stillMissing.includes(s.id)&&!forget.has(s.id)&&!gone.has(s.id))
kept=[...kept,...recovered.filter((s:any)=>!kept.some((k:any)=>k.id===s.id))]
}catch{/* keep page + in-memory stills */}
}catch{if(replacing) kept=[]}
}
if(gen!==libraryLoadGen) return
library.value={
folders:page.folders||library.value.folders||[],
stills:[...pageStills,...kept].filter((s:any)=>!forget.has(s.id)),
clips:(page.clips||[]).filter((c:any)=>!forget.has(c.id)),
tracks:(page.tracks||[]).filter((t:any)=>!forget.has(t.id)),
stills:[...pageStills,...kept].filter((s:any)=>s?.id&&!forget.has(s.id)&&!gone.has(s.id)),
clips:(page.clips||[]).filter((c:any)=>c?.id&&!forget.has(c.id)&&!gone.has(c.id)),
tracks:(page.tracks||[]).filter((t:any)=>t?.id&&!forget.has(t.id)&&!gone.has(t.id)),
drafts:page.drafts||library.value.drafts||[],
loraAliases:page.loraAliases||library.value.loraAliases||{},
loraTriggers:page.loraTriggers||library.value.loraTriggers||{},
keepPrompts:page.keepPrompts||library.value.keepPrompts
keepPrompts:page.keepPrompts||library.value.keepPrompts,
catalogEpoch:typeof epoch==='number'?epoch:(library.value.catalogEpoch??0)
}
const allStills=[...pageStills,...kept]
// Drop filled photo cards only when the still is truly gone from the catalog
for(let i=(form.photos||[]).length-1;i>=0;i-=1){
const id=form.photos[i]?.stillId
if(id && !allStills.some((s:any)=>s.id===id)) form.photos[i].stillId=''
}
syncPhotosBindings()
clearRefsMissingFromLibrary()
}
async function fetchHealth(){
@@ -2098,6 +2136,7 @@ onMounted(()=>{
// Folder page in background — never blocks Generate / prompt
void loadLibraryOnce().then(()=>{
restoreBenchDraft()
clearRefsMissingFromLibrary()
if(!form.folderId)form.folderId=library.value.folders[0]?.id||''
loadRevealed(form.folderId)
void applyIncomingGenerationLoad()
@@ -2159,6 +2198,8 @@ const mobileCtx=computed(()=>({
libraryRefreshKey:libraryRefreshKey.value,
family:family.value,
familyStillCount:familyStillCount.value,
clipThumbUrl,
clipVideoUrl,
showSelectedStill:showSelectedStill.value,
libraryOpen:libraryOpen.value,
eyeOpen:eyeOpen.value,