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:
@@ -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'])
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync, readdirSync, statSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const root = fileURLToPath(new URL('../server/api/library/', import.meta.url))
|
||||
|
||||
function walk(dir) {
|
||||
const files = []
|
||||
for (const name of readdirSync(dir)) {
|
||||
const path = join(dir, name)
|
||||
if (statSync(path).isDirectory()) files.push(...walk(path))
|
||||
else if (name.endsWith('.ts')) files.push(path)
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
const media = walk(root).filter((path) => (
|
||||
/[/\\](thumb|preview|video|audio|first-frame|last-frame)\.get\.ts$/.test(path)
|
||||
|| /[/\\]stills[/\\]\[id\]\.get\.ts$/.test(path)
|
||||
|| /[/\\]drafts[/\\]\[id\][/\\]still\.get\.ts$/.test(path)
|
||||
))
|
||||
|
||||
test('library media routes are uncached and do not answer 304', () => {
|
||||
const helper = readFileSync(new URL('../server/utils/library.ts', import.meta.url), 'utf8')
|
||||
assert.match(helper, /Cache-Control', 'no-store, no-cache, must-revalidate'/)
|
||||
assert.match(helper, /Pragma', 'no-cache'/)
|
||||
assert.ok(media.length >= 8, media.join('\n'))
|
||||
for (const path of media) {
|
||||
const source = readFileSync(path, 'utf8')
|
||||
assert.match(source, /setLibraryMediaHeaders/, path)
|
||||
assert.doesNotMatch(source, /304/, path)
|
||||
assert.doesNotMatch(source, /if-none-match/i, path)
|
||||
assert.doesNotMatch(source, /max-age=\d+/, path)
|
||||
assert.doesNotMatch(source, /public, max-age/, path)
|
||||
}
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { folderImageCount, libraryAfterWipe, mergeLibraryRows, replaceLibraryRows } from '../shared/studio2/viewer.mjs'
|
||||
import { folderImageCount, libraryAfterWipe, libraryMediaUrl, mergeLibraryRows, replaceLibraryRows } from '../shared/studio2/viewer.mjs'
|
||||
|
||||
test('folderImageCount trusts a server count of 0', () => {
|
||||
assert.equal(folderImageCount({ id: 'inbox', imageCount: 0 }, 1762), 0)
|
||||
@@ -8,7 +8,14 @@ test('folderImageCount trusts a server count of 0', () => {
|
||||
assert.equal(folderImageCount(undefined, 3), 3)
|
||||
})
|
||||
|
||||
test('libraryAfterWipe replaces state and does not keep wiped folder rows from a stale list', () => {
|
||||
test('libraryMediaUrl always changes the pre-deploy cache key', () => {
|
||||
assert.equal(libraryMediaUrl('/api/library/stills/abc/thumb', 0), '/api/library/stills/abc/thumb?v=0')
|
||||
assert.equal(libraryMediaUrl('/api/library/stills/abc/preview', 4), '/api/library/stills/abc/preview?v=4')
|
||||
assert.equal(libraryMediaUrl('/api/library/clips/abc/first-frame?thumb=1', 2), '/api/library/clips/abc/first-frame?thumb=1&v=2')
|
||||
assert.notEqual(libraryMediaUrl('/api/library/stills/abc/thumb', 0), '/api/library/stills/abc/thumb')
|
||||
})
|
||||
|
||||
test('libraryAfterWipe replaces the folder from the response and does not keep a suppression list', () => {
|
||||
const previous = {
|
||||
folders: [{ id: 'inbox', imageCount: 2 }],
|
||||
stills: [{ id: 'new-1', folderId: 'inbox' }, { id: 'new-2', folderId: 'inbox' }],
|
||||
@@ -24,16 +31,16 @@ test('libraryAfterWipe replaces state and does not keep wiped folder rows from a
|
||||
{ id: 'kept', folderId: 'other' }
|
||||
],
|
||||
clips: [{ id: 'clip-ghost', folderId: 'inbox' }],
|
||||
tracks: []
|
||||
tracks: [],
|
||||
deletedIds: ['new-1', 'new-2']
|
||||
}
|
||||
const next = libraryAfterWipe(previous, stale, 'inbox')
|
||||
assert.deepEqual(next.stills.map(item => item.id), ['kept'])
|
||||
assert.deepEqual(next.clips, [])
|
||||
assert.equal(next.folders[0].imageCount, 0)
|
||||
assert.equal(next.suppressedIds.includes('new-1'), true)
|
||||
assert.equal(next.suppressedIds.includes('ghost'), true)
|
||||
assert.equal(next.suppressedIds.includes('already-gone'), true)
|
||||
const page = mergeLibraryRows(next.stills, stale.stills, next.suppressedIds)
|
||||
assert.deepEqual(page.map(item => item.id), ['kept'])
|
||||
assert.deepEqual(replaceLibraryRows(stale.stills, next.suppressedIds).map(item => item.id), ['kept'])
|
||||
assert.deepEqual(next.suppressedIds, [])
|
||||
const replaced = replaceLibraryRows(next.stills, [])
|
||||
assert.deepEqual(replaced.map(item => item.id), ['kept'])
|
||||
const page = mergeLibraryRows(replaced, [{ id: 'kept', folderId: 'other' }, { id: 'fresh', folderId: 'other' }], [])
|
||||
assert.deepEqual(page.map(item => item.id), ['kept', 'fresh'])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user