Keep library deletes when a still file is locked, and match LoRAs by engine folder.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-10-24 11:13:00 -05:00
co-authored by Cursor
parent 8602d6ed2e
commit 39aa4b65d0
19 changed files with 501 additions and 116 deletions
+47 -34
View File
@@ -398,7 +398,8 @@ function invalidateCatalogCache(owner?: string) {
function mutate<T>(owner: string, fn: (catalog: Catalog) => T): Promise<T> {
const prev = writeChains.get(owner) || Promise.resolve()
const run = prev.then(() => {
const catalog = readCatalog(owner)
// Copy first so a throw cannot leave the cached catalog diverged from disk.
const catalog = structuredClone(readCatalog(owner))
const result = fn(catalog)
writeCatalog(owner, catalog)
return result
@@ -863,15 +864,9 @@ export function deleteFolder(owner: string, id: string) {
catalog.clips = catalog.clips.filter(clip => clip.folderId !== id)
catalog.stills = catalog.stills.filter(still => still.folderId !== id)
catalog.tracks = (catalog.tracks || []).filter(track => track.folderId !== id)
for (const clip of clips) {
rmSync(clipDir(owner, clip.id), { recursive: true, force: true })
}
for (const still of stills) {
removeStillFiles(owner, still.id)
}
for (const track of tracks) {
rmSync(trackDir(owner, track.id), { recursive: true, force: true })
}
for (const clip of clips) safeRm(clipDir(owner, clip.id), true)
for (const still of stills) removeStillFiles(owner, still.id)
for (const track of tracks) safeRm(trackDir(owner, track.id), true)
return { fallbackFolderId: catalog.folders[0].id }
})
}
@@ -892,19 +887,11 @@ export function wipeFolderContents(owner: string, folderId: string) {
catalog.clips = catalog.clips.filter(clip => clip.folderId !== folderId)
catalog.stills = catalog.stills.filter(still => still.folderId !== folderId)
catalog.tracks = (catalog.tracks || []).filter(track => track.folderId !== folderId)
catalog.drafts = (catalog.drafts || []).map((draft) => {
if (!draft.stillId || !deletedIds.has(draft.stillId)) return draft
return { ...draft, stillId: undefined, stillFilename: undefined }
})
for (const clip of clips) {
rmSync(clipDir(owner, clip.id), { recursive: true, force: true })
}
for (const still of stills) {
removeStillFiles(owner, still.id)
}
for (const track of tracks) {
rmSync(trackDir(owner, track.id), { recursive: true, force: true })
}
detachDraftStills(catalog, deletedIds)
for (const clip of clips) safeRm(clipDir(owner, clip.id), true)
for (const still of stills) removeStillFiles(owner, still.id)
for (const track of tracks) safeRm(trackDir(owner, track.id), true)
pruneMissingStillMasters(owner, catalog)
return {
folderId: folder.id,
folderName: folder.name,
@@ -916,12 +903,29 @@ export function wipeFolderContents(owner: string, folderId: string) {
})
}
function detachDraftStills(catalog: Catalog, ids: Set<string>) {
if (!ids.size) return
catalog.drafts = (catalog.drafts || []).map((draft) => {
if (!draft.stillId || !ids.has(draft.stillId)) return draft
return { ...draft, stillId: undefined, stillFilename: undefined }
})
}
/** Drop catalog stills whose master file is gone — a 204 thumb is not a still. */
function pruneMissingStillMasters(owner: string, catalog: Catalog) {
const keep: LibraryStill[] = []
const droppedIds = new Set<string>()
for (const still of catalog.stills) {
if (existsSync(stillPath(owner, still.id))) {
let present = false
try {
present = existsSync(stillPath(owner, still.id))
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
console.error('[library] still master check failed', still.id, message)
keep.push(still)
continue
}
if (present) {
keep.push(still)
continue
}
@@ -930,10 +934,7 @@ function pruneMissingStillMasters(owner: string, catalog: Catalog) {
}
if (!droppedIds.size) return 0
catalog.stills = keep
catalog.drafts = (catalog.drafts || []).map((draft) => {
if (!draft.stillId || !droppedIds.has(draft.stillId)) return draft
return { ...draft, stillId: undefined, stillFilename: undefined }
})
detachDraftStills(catalog, droppedIds)
return droppedIds.size
}
@@ -1371,10 +1372,20 @@ export function existingStillPreviewPath(owner: string, id: string) {
return ''
}
/** Never throw — a locked thumb must not abort the catalog write. */
function safeRm(target: string, recursive = false) {
try {
rmSync(target, recursive ? { recursive: true, force: true } : { force: true })
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
console.error('[library] file remove failed', message)
}
}
function removeStillFiles(owner: string, id: string) {
rmSync(stillPath(owner, id), { force: true })
rmSync(stillThumbFile(owner, id), { force: true })
rmSync(stillPreviewFile(owner, id), { force: true })
safeRm(stillPath(owner, id))
safeRm(stillThumbFile(owner, id))
safeRm(stillPreviewFile(owner, id))
}
let thumbSlots = 0
@@ -1899,7 +1910,9 @@ export function deleteStill(owner: string, id: string) {
const still = catalog.stills.find(item => item.id === id)
if (!still) throw createError({ statusCode: 404, statusMessage: 'Image not found' })
catalog.stills = catalog.stills.filter(item => item.id !== id)
detachDraftStills(catalog, new Set([id]))
removeStillFiles(owner, id)
pruneMissingStillMasters(owner, catalog)
return still
})
}
@@ -1922,9 +1935,9 @@ export function deleteStillFamily(owner: string, id: string) {
const family = stillsInSameFamily(catalog, still)
const ids = new Set(family.map(item => item.id))
catalog.stills = catalog.stills.filter(item => !ids.has(item.id))
for (const item of family) {
removeStillFiles(owner, item.id)
}
detachDraftStills(catalog, ids)
for (const item of family) removeStillFiles(owner, item.id)
pruneMissingStillMasters(owner, catalog)
return family
})
}