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
+146 -15
View File
@@ -6,7 +6,15 @@ import ts from 'typescript'
const source = readFileSync(new URL('../server/utils/library.ts', import.meta.url), 'utf8')
const ast = ts.createSourceFile('library.ts', source, ts.ScriptTarget.Latest, true)
const names = [
'safeRm',
'removedEntries',
'pathRemains',
'stillFileRemains',
'unlinkVerified',
'stillIdAllowed',
'stillArtifactPaths',
'entryPaths',
'assertRemovedEntries',
'assertStillFilesGone',
'removeStillFiles',
'detachDraftStills',
'sameFolder',
@@ -30,6 +38,36 @@ function harness() {
const catalogEpoch = new Map()
let writes = 0
let stored = null
function existsSync(path) {
const value = String(path)
if (files.has(value)) return true
const prefix = value.endsWith('/') ? value : `${value}/`
for (const file of files) {
if (String(file).startsWith(prefix)) return true
}
return false
}
function statSync(path) {
if (!existsSync(path)) {
const error = new Error('ENOENT')
error.code = 'ENOENT'
throw error
}
if (!files.has(String(path))) return { isFile: () => false, isDirectory: () => true }
return { isFile: () => true, isDirectory: () => false }
}
function readdirSync(dir) {
const prefix = String(dir).endsWith('/') ? String(dir) : `${dir}/`
const names = []
for (const file of files) {
const value = String(file)
if (!value.startsWith(prefix)) continue
const rest = value.slice(prefix.length)
if (!rest || rest.includes('/')) continue
names.push(rest)
}
return names
}
function readCatalog() { return stored }
function writeCatalog(_owner, catalog) {
stored = catalog === undefined ? _owner : catalog
@@ -39,6 +77,11 @@ function harness() {
const api = new Function(
'rmSync',
'existsSync',
'statSync',
'readdirSync',
'join',
'ownerRoot',
'libraryRoot',
'console',
'stillPath',
'stillThumbFile',
@@ -52,13 +95,22 @@ function harness() {
'writeCatalog',
'catalogEpoch',
'invalidateCatalogCache',
`${js}; return { catalogWithPrunedStills, deleteStill, deleteStillFamily, wipeFolderContents, emptyFolderRows };`
`${js}; return { catalogWithPrunedStills, deleteStill, deleteStillFamily, wipeFolderContents, emptyFolderRows, removeStillFiles };`
)(
(path) => {
if ([...thrown].some(id => String(path).includes(id))) throw new Error('EPERM')
if ([...thrown].some(id => String(path).includes(id))) {
const error = new Error('EPERM')
error.code = 'EPERM'
throw error
}
files.delete(path)
},
(path) => files.has(path),
existsSync,
statSync,
readdirSync,
(...parts) => parts.map(part => String(part).replace(/^\/+|\/+$/g, '')).filter(Boolean).join('/'),
(owner) => owner,
() => 'legacy',
console,
(owner, id) => `${owner}/stills/${id}`,
(owner, id) => `${owner}/stills/${id}.thumb.jpg`,
@@ -89,10 +141,11 @@ function harness() {
}
}
test('deleteStill drops the catalog row when the file remove fails', async () => {
test('deleteStill does not write the catalog when the master file survives', async () => {
const h = harness()
h.thrown.add('locked')
h.files.add('owner/stills/locked')
h.files.add('owner/stills/locked.thumb.jpg')
const catalog = {
stills: [{ id: 'locked', folderId: 'inbox' }, { id: 'keep', folderId: 'inbox' }],
drafts: [{ id: 'd', stillId: 'locked', stillFilename: 'locked.png' }],
@@ -102,14 +155,78 @@ test('deleteStill drops the catalog row when the file remove fails', async () =>
}
h.load(catalog)
h.files.add('owner/stills/keep')
await h.api.deleteStill('owner', 'locked')
await assert.rejects(() => h.api.deleteStill('owner', 'locked'), /locked/)
assert.equal(h.writes, 0)
assert.deepEqual(catalog.stills.map(item => item.id), ['locked', 'keep'])
assert.equal(catalog.drafts[0].stillId, 'locked')
assert.equal(h.files.has('owner/stills/locked'), true)
assert.equal(h.files.has('owner/stills/keep'), true)
assert.deepEqual(h.invalidated, [])
})
test('deleteStill removes master, thumb, preview, siblings, and legacy names', async () => {
const h = harness()
const catalog = {
stills: [{ id: 'a', folderId: 'inbox' }, { id: 'keep', folderId: 'inbox' }],
drafts: [{ id: 'd', stillId: 'a', stillFilename: 'a.png' }],
clips: [],
tracks: [],
folders: [{ id: 'inbox' }]
}
h.load(catalog)
for (const path of [
'owner/stills/a',
'owner/stills/a.thumb.jpg',
'owner/stills/a.preview.jpg',
'owner/stills/a.extra.png',
'legacy/files/a',
'legacy/stills/a.thumb.jpg',
'owner/stills/keep',
'owner/stills/keep.thumb.jpg'
]) h.files.add(path)
await h.api.deleteStill('owner', 'a')
assert.equal(h.writes, 1)
assert.deepEqual(catalog.stills.map(item => item.id), ['keep'])
assert.equal(catalog.drafts[0].stillId, undefined)
for (const path of [
'owner/stills/a',
'owner/stills/a.thumb.jpg',
'owner/stills/a.preview.jpg',
'owner/stills/a.extra.png',
'legacy/files/a',
'legacy/stills/a.thumb.jpg'
]) assert.equal(h.files.has(path), false, path)
assert.equal(h.files.has('owner/stills/keep'), true)
assert.equal(h.files.has('owner/stills/keep.thumb.jpg'), true)
assert.deepEqual(h.invalidated, ['owner'])
})
test('deleteStill unlinks leftover files when the catalog row is already gone', async () => {
const h = harness()
const catalog = {
stills: [{ id: 'keep', folderId: 'inbox' }],
drafts: [],
clips: [],
tracks: [],
folders: [{ id: 'inbox' }]
}
h.load(catalog)
h.files.add('owner/stills/orphan')
h.files.add('owner/stills/orphan.thumb.jpg')
h.files.add('owner/stills/orphan.preview.jpg')
h.files.add('legacy/stills/orphan')
h.files.add('owner/stills/keep')
await assert.rejects(() => h.api.deleteStill('owner', 'orphan'), /Image not found/)
assert.equal(h.writes, 0)
assert.deepEqual(catalog.stills.map(item => item.id), ['keep'])
assert.equal(h.files.has('owner/stills/orphan'), false)
assert.equal(h.files.has('owner/stills/orphan.thumb.jpg'), false)
assert.equal(h.files.has('owner/stills/orphan.preview.jpg'), false)
assert.equal(h.files.has('legacy/stills/orphan'), false)
assert.equal(h.files.has('owner/stills/keep'), true)
})
test('pruneMissingStillMasters runs on delete and on catalog read', async () => {
test('pruneMissingStillMasters drops the row and unlinks leftover files', async () => {
const h = harness()
const catalog = {
stills: [{ id: 'gone', folderId: 'inbox' }, { id: 'keep', folderId: 'inbox' }],
@@ -120,13 +237,18 @@ test('pruneMissingStillMasters runs on delete and on catalog read', async () =>
}
h.load(structuredClone(catalog))
h.files.add('owner/stills/keep')
h.files.add('owner/stills/gone.thumb.jpg')
h.files.add('owner/stills/gone.preview.jpg')
const page = h.api.catalogWithPrunedStills('owner')
assert.equal(h.writes, 1)
assert.deepEqual(page.stills.map(item => item.id), ['keep'])
assert.equal(page.drafts[0].stillId, undefined)
assert.equal(h.files.has('owner/stills/gone.thumb.jpg'), false)
assert.equal(h.files.has('owner/stills/gone.preview.jpg'), false)
assert.equal(h.files.has('owner/stills/keep'), true)
})
test('wipeFolderContents still clears the folder when a file remove throws', async () => {
test('wipeFolderContents throws and keeps the catalog when a file survives', async () => {
const h = harness()
h.thrown.add('locked')
const catalog = {
@@ -137,15 +259,16 @@ test('wipeFolderContents still clears the folder when a file remove throws', asy
folders: [{ id: 'inbox', name: 'Inbox' }]
}
h.load(catalog)
const result = await h.api.wipeFolderContents('owner', 'inbox')
assert.equal(result.deleted, 2)
assert.equal(catalog.stills.length, 0)
assert.equal(catalog.clips.length, 0)
assert.equal(h.writes, 1)
assert.deepEqual(h.invalidated, ['owner'])
h.files.add('owner/stills/locked')
await assert.rejects(() => h.api.wipeFolderContents('owner', 'inbox'), /locked/)
assert.equal(h.writes, 0)
assert.equal(catalog.stills.length, 1)
assert.equal(catalog.clips.length, 1)
assert.equal(h.files.has('owner/stills/locked'), true)
assert.deepEqual(h.invalidated, [])
})
test('wipeFolderContents leaves zero rows for that folder and does not touch another folder', async () => {
test('wipeFolderContents leaves zero rows for that folder and zero files for those ids', async () => {
const h = harness()
const catalog = {
stills: [
@@ -162,12 +285,20 @@ test('wipeFolderContents leaves zero rows for that folder and does not touch ano
}
h.load(catalog)
h.files.add('owner/stills/a')
h.files.add('owner/stills/a.thumb.jpg')
h.files.add('owner/stills/b')
h.files.add('owner/clips/c')
h.files.add('owner/tracks/t')
const result = await h.api.wipeFolderContents('owner', 'inbox')
assert.equal(result.deleted, 3)
assert.deepEqual(catalog.stills.map(item => item.id), ['b'])
assert.deepEqual(catalog.clips, [])
assert.deepEqual(catalog.tracks.map(item => item.id), ['u'])
assert.equal(h.files.has('owner/stills/a'), false)
assert.equal(h.files.has('owner/stills/a.thumb.jpg'), false)
assert.equal(h.files.has('owner/clips/c'), false)
assert.equal(h.files.has('owner/tracks/t'), false)
assert.equal(h.files.has('owner/stills/b'), true)
assert.deepEqual(h.invalidated, ['owner'])
})