Stop a folder wipe from restoring the pre-wipe library catalog.
The in-memory catalog could outlive the write, so delete-all came back as thousands of empty tiles and a stale badge. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+94
-20
@@ -187,7 +187,8 @@ interface Catalog {
|
||||
}
|
||||
|
||||
const writeChains = new Map<string, Promise<unknown>>()
|
||||
const catalogCache = new Map<string, { catalog: Catalog; mtimeMs: number }>()
|
||||
const catalogCache = new Map<string, { catalog: Catalog; mtimeMs: number; epoch: number }>()
|
||||
const catalogEpoch = new Map<string, number>()
|
||||
|
||||
export function libraryRoot() {
|
||||
const config = useRuntimeConfig()
|
||||
@@ -356,12 +357,19 @@ function ensureOwner(owner: string) {
|
||||
function readCatalog(owner: string): Catalog {
|
||||
ensureOwner(owner)
|
||||
const path = catalogPath(owner)
|
||||
let mtimeMs = 0
|
||||
try {
|
||||
mtimeMs = existsSync(path) ? statSync(path).mtimeMs : 0
|
||||
} catch { mtimeMs = 0 }
|
||||
const epoch = catalogEpoch.get(owner) || 0
|
||||
const hit = catalogCache.get(owner)
|
||||
if (hit && hit.mtimeMs === mtimeMs && mtimeMs > 0) return hit.catalog
|
||||
if (hit && hit.epoch === epoch) {
|
||||
let mtimeMs = 0
|
||||
try { mtimeMs = existsSync(path) ? statSync(path).mtimeMs : 0 } catch { mtimeMs = 0 }
|
||||
// Coolify volumes and Windows can return a stale mtime after rename.
|
||||
// An unchanged or older mtime must not throw away the catalog we just wrote.
|
||||
if (mtimeMs <= hit.mtimeMs) return hit.catalog
|
||||
}
|
||||
|
||||
const epochAtStart = catalogEpoch.get(owner) || 0
|
||||
let mtimeMs = 0
|
||||
try { mtimeMs = existsSync(path) ? statSync(path).mtimeMs : 0 } catch { mtimeMs = 0 }
|
||||
|
||||
let catalog: Catalog
|
||||
try {
|
||||
@@ -373,10 +381,21 @@ function readCatalog(owner: string): Catalog {
|
||||
catalog = emptyCatalog()
|
||||
writeFileSync(path, JSON.stringify(catalog, null, 2))
|
||||
try { mtimeMs = statSync(path).mtimeMs } catch { mtimeMs = Date.now() }
|
||||
catalogCache.set(owner, { catalog, mtimeMs })
|
||||
if ((catalogEpoch.get(owner) || 0) !== epochAtStart) {
|
||||
const newer = catalogCache.get(owner)
|
||||
if (newer && newer.epoch === (catalogEpoch.get(owner) || 0)) return newer.catalog
|
||||
}
|
||||
catalogCache.set(owner, { catalog, mtimeMs, epoch: catalogEpoch.get(owner) || 0 })
|
||||
return catalog
|
||||
}
|
||||
catalogCache.set(owner, { catalog, mtimeMs })
|
||||
const epochNow = catalogEpoch.get(owner) || 0
|
||||
if (epochNow !== epochAtStart) {
|
||||
const newer = catalogCache.get(owner)
|
||||
if (newer && newer.epoch === epochNow) return newer.catalog
|
||||
// A write landed while this parse was in flight. Never cache those bytes.
|
||||
return readCatalog(owner)
|
||||
}
|
||||
catalogCache.set(owner, { catalog, mtimeMs, epoch: epochNow })
|
||||
return catalog
|
||||
}
|
||||
|
||||
@@ -385,12 +404,18 @@ function writeCatalog(owner: string, catalog: Catalog) {
|
||||
const tmp = catalogPath(owner) + '.tmp'
|
||||
writeFileSync(tmp, JSON.stringify(catalog, null, 2))
|
||||
renameSync(tmp, catalogPath(owner))
|
||||
const epoch = (catalogEpoch.get(owner) || 0) + 1
|
||||
catalogEpoch.set(owner, epoch)
|
||||
// Floor at Date.now() so a stale older mtime cannot look "newer" than this write.
|
||||
let mtimeMs = Date.now()
|
||||
try { mtimeMs = statSync(catalogPath(owner)).mtimeMs } catch { /* ignore */ }
|
||||
catalogCache.set(owner, { catalog, mtimeMs })
|
||||
try {
|
||||
const stamped = statSync(catalogPath(owner)).mtimeMs
|
||||
if (stamped > mtimeMs) mtimeMs = stamped
|
||||
} catch { /* keep Date.now() */ }
|
||||
catalogCache.set(owner, { catalog, mtimeMs, epoch })
|
||||
}
|
||||
|
||||
function invalidateCatalogCache(owner?: string) {
|
||||
export function invalidateCatalogCache(owner?: string) {
|
||||
if (owner) catalogCache.delete(owner)
|
||||
else catalogCache.clear()
|
||||
}
|
||||
@@ -871,35 +896,77 @@ export function deleteFolder(owner: string, id: string) {
|
||||
})
|
||||
}
|
||||
|
||||
function sameFolder(folderId: string | undefined, target: string) {
|
||||
return String(folderId || '') === String(target)
|
||||
}
|
||||
|
||||
/** Remove every row for folderId. A second pass drops anything the first filter left. */
|
||||
function emptyFolderRows(catalog: Catalog, folderId: string) {
|
||||
const same = (item: { folderId?: string }) => sameFolder(item.folderId, folderId)
|
||||
const strip = () => {
|
||||
catalog.clips = (catalog.clips || []).filter(item => !same(item))
|
||||
catalog.stills = (catalog.stills || []).filter(item => !same(item))
|
||||
catalog.tracks = (catalog.tracks || []).filter(item => !same(item))
|
||||
}
|
||||
const left = () => [
|
||||
...(catalog.clips || []).filter(same),
|
||||
...(catalog.stills || []).filter(same),
|
||||
...(catalog.tracks || []).filter(same)
|
||||
]
|
||||
strip()
|
||||
let survivors = left()
|
||||
if (!survivors.length) return
|
||||
console.error('[library] wipe left folder rows', folderId, survivors.map(item => item.id))
|
||||
const ids = new Set(survivors.map(item => item.id))
|
||||
catalog.clips = (catalog.clips || []).filter(item => !ids.has(item.id))
|
||||
catalog.stills = (catalog.stills || []).filter(item => !ids.has(item.id))
|
||||
catalog.tracks = (catalog.tracks || []).filter(item => !ids.has(item.id))
|
||||
survivors = left()
|
||||
if (!survivors.length) return
|
||||
console.error('[library] wipe still left folder rows', folderId, survivors.map(item => item.id))
|
||||
strip()
|
||||
}
|
||||
|
||||
/** Delete every still, clip, and track in a folder — catalog rows and on-disk files. Keeps the folder. */
|
||||
export function wipeFolderContents(owner: string, folderId: string) {
|
||||
return mutate(owner, (catalog) => {
|
||||
const folder = catalog.folders.find(item => item.id === folderId)
|
||||
if (!folder) throw createError({ statusCode: 404, statusMessage: 'Folder not found' })
|
||||
const clips = catalog.clips.filter(clip => clip.folderId === folderId)
|
||||
const stills = catalog.stills.filter(still => still.folderId === folderId)
|
||||
const tracks = (catalog.tracks || []).filter(track => track.folderId === folderId)
|
||||
const clips = catalog.clips.filter(clip => sameFolder(clip.folderId, folderId))
|
||||
const stills = catalog.stills.filter(still => sameFolder(still.folderId, folderId))
|
||||
const tracks = (catalog.tracks || []).filter(track => sameFolder(track.folderId, folderId))
|
||||
const deletedIds = new Set([
|
||||
...clips.map(item => item.id),
|
||||
...stills.map(item => item.id),
|
||||
...tracks.map(item => item.id)
|
||||
])
|
||||
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)
|
||||
emptyFolderRows(catalog, folderId)
|
||||
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)
|
||||
const remain = [
|
||||
...catalog.clips.filter(item => sameFolder(item.folderId, folderId)),
|
||||
...catalog.stills.filter(item => sameFolder(item.folderId, folderId)),
|
||||
...(catalog.tracks || []).filter(item => sameFolder(item.folderId, folderId))
|
||||
]
|
||||
if (remain.length) {
|
||||
console.error('[library] wipe left folder rows', folderId, remain.map(item => item.id))
|
||||
emptyFolderRows(catalog, folderId)
|
||||
}
|
||||
return {
|
||||
folderId: folder.id,
|
||||
folderName: folder.name,
|
||||
deleted: clips.length + stills.length + tracks.length,
|
||||
deletedIds: [...deletedIds],
|
||||
stills: stills.length,
|
||||
clips: clips.length,
|
||||
tracks: tracks.length
|
||||
}
|
||||
}).then((result) => {
|
||||
invalidateCatalogCache(owner)
|
||||
return result
|
||||
})
|
||||
}
|
||||
|
||||
@@ -911,11 +978,13 @@ function detachDraftStills(catalog: Catalog, ids: Set<string>) {
|
||||
})
|
||||
}
|
||||
|
||||
/** Drop catalog stills whose master file is gone — a 204 thumb is not a still. */
|
||||
/** Drop catalog stills whose master file is gone — a 204 thumb is not a still. Never adds a still. */
|
||||
function pruneMissingStillMasters(owner: string, catalog: Catalog) {
|
||||
const beforeIds = new Set(catalog.stills.map(still => still.id))
|
||||
const keep: LibraryStill[] = []
|
||||
const droppedIds = new Set<string>()
|
||||
for (const still of catalog.stills) {
|
||||
if (!beforeIds.has(still.id)) continue
|
||||
let present = false
|
||||
try {
|
||||
present = existsSync(stillPath(owner, still.id))
|
||||
@@ -932,15 +1001,20 @@ function pruneMissingStillMasters(owner: string, catalog: Catalog) {
|
||||
droppedIds.add(still.id)
|
||||
removeStillFiles(owner, still.id)
|
||||
}
|
||||
catalog.stills = keep.filter(still => beforeIds.has(still.id))
|
||||
if (!droppedIds.size) return 0
|
||||
catalog.stills = keep
|
||||
detachDraftStills(catalog, droppedIds)
|
||||
return droppedIds.size
|
||||
}
|
||||
|
||||
function catalogWithPrunedStills(owner: string): Catalog {
|
||||
const epoch = catalogEpoch.get(owner) || 0
|
||||
const catalog = readCatalog(owner)
|
||||
if (pruneMissingStillMasters(owner, catalog)) writeCatalog(owner, catalog)
|
||||
const before = new Set(catalog.stills.map(still => still.id))
|
||||
const dropped = pruneMissingStillMasters(owner, catalog)
|
||||
catalog.stills = catalog.stills.filter(still => before.has(still.id))
|
||||
if ((catalogEpoch.get(owner) || 0) !== epoch) return readCatalog(owner)
|
||||
if (dropped) writeCatalog(owner, catalog)
|
||||
return catalog
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user