diff --git a/components/studio2/LibraryModal.vue b/components/studio2/LibraryModal.vue index ad80ed9..c2cfe75 100644 --- a/components/studio2/LibraryModal.vue +++ b/components/studio2/LibraryModal.vue @@ -40,6 +40,13 @@ class="lib-modal-preview-all" @click="$emit('toggle-preview')" >{{ previewHidden ? 'Show all previews' : 'Hide all previews' }} +
+

Loading library…

@@ -174,10 +198,22 @@ const tracks = ref([]) const fetchedAt = ref(0) const nextCursor = ref(null) const liveThumbs = ref(new Set()) +const suppressedDeletedIds = ref(new Set()) +const wipeBusy = ref(false) +const pendingWipe = ref<{ folderId: string; name: string; count: number } | null>(null) let thumbObserver: IntersectionObserver | null = null let scrollLoadBound = false const activeFolderId = computed(() => props.folderId || folders.value[0]?.id || 'all') +const activeFolder = computed(() => folders.value.find(folder => folder.id === activeFolderId.value) || null) +const folderAssetCount = computed(() => { + const id = activeFolderId.value + if (!id || id === 'all') return 0 + return stills.value.filter(item => item.folderId === id).length + + clips.value.filter(item => item.folderId === id).length + + tracks.value.filter(item => item.folderId === id).length +}) +const canWipeFolder = computed(() => Boolean(activeFolder.value && activeFolderId.value !== 'all' && folderAssetCount.value > 0)) const entries = computed(() => buildLibraryEntries({ clips: clips.value, @@ -252,6 +288,9 @@ function useAndClose() { function removeLocalAsset(asset: any) { if (!asset?.id) return + const next = new Set(suppressedDeletedIds.value) + next.add(asset.id) + suppressedDeletedIds.value = next stills.value = stills.value.filter(s => s.id !== asset.id) clips.value = clips.value.filter(c => c.id !== asset.id) tracks.value = tracks.value.filter(t => t.id !== asset.id) @@ -266,17 +305,25 @@ function onDeleteClick() { } function mergeIds(prev: { id: string }[], next: { id: string }[]) { - const map = new Map(prev.map(item => [item.id, item])) - for (const item of next) map.set(item.id, item) + const drop = suppressedDeletedIds.value + const map = new Map() + for (const item of prev) { + if (!drop.has(item.id)) map.set(item.id, item) + } + for (const item of next) { + if (!drop.has(item.id)) map.set(item.id, item) + } return [...map.values()] } function applyLibraryPage(data: any, replace: boolean) { folders.value = data?.folders || folders.value + const drop = suppressedDeletedIds.value + const keep = (list: T[]) => list.filter(item => !drop.has(item.id)) if (replace) { - clips.value = data?.clips || [] - stills.value = data?.stills || [] - tracks.value = data?.tracks || [] + clips.value = keep(data?.clips || []) as LibraryClip[] + stills.value = keep(data?.stills || []) as LibraryStill[] + tracks.value = keep(data?.tracks || []) as LibraryTrack[] } else { clips.value = mergeIds(clips.value, data?.clips || []) as LibraryClip[] stills.value = mergeIds(stills.value, data?.stills || []) as LibraryStill[] @@ -286,6 +333,45 @@ function applyLibraryPage(data: any, replace: boolean) { fetchedAt.value = Date.now() } +function askWipeFolder() { + if (!activeFolder.value || activeFolderId.value === 'all' || wipeBusy.value) return + const count = folderAssetCount.value + if (!count) return + pendingWipe.value = { + folderId: activeFolderId.value, + name: activeFolder.value.name, + count + } +} + +async function confirmWipeFolder() { + const pending = pendingWipe.value + if (!pending || wipeBusy.value) return + pendingWipe.value = null + wipeBusy.value = true + try { + const gone = [ + ...stills.value.filter(item => item.folderId === pending.folderId).map(item => item.id), + ...clips.value.filter(item => item.folderId === pending.folderId).map(item => item.id), + ...tracks.value.filter(item => item.folderId === pending.folderId).map(item => item.id) + ] + const next = new Set(suppressedDeletedIds.value) + for (const id of gone) next.add(id) + suppressedDeletedIds.value = next + stills.value = stills.value.filter(item => item.folderId !== pending.folderId) + clips.value = clips.value.filter(item => item.folderId !== pending.folderId) + tracks.value = tracks.value.filter(item => item.folderId !== pending.folderId) + const result = await $fetch(`/api/library/folders/${pending.folderId}/wipe`, { method: 'POST' }) + applyLibraryPage(result, true) + await fetchLibrary(true) + } catch (e: any) { + loadError.value = e?.data?.statusMessage || e?.message || 'Could not empty that folder' + await fetchLibrary(true) + } finally { + wipeBusy.value = false + } +} + async function fetchLibraryPage(cursor: string | null) { const folder = activeFolderId.value && activeFolderId.value !== 'all' ? activeFolderId.value : '' const type = filter.value === 'all' ? 'all' : filter.value === 'images' ? 'images' : filter.value === 'videos' ? 'videos' : 'music' @@ -411,6 +497,8 @@ watch(entries, () => nextTick(() => observeThumbs())) watch([filter, activeFolderId], () => { if (!props.open) return + suppressedDeletedIds.value = new Set() + pendingWipe.value = null void fetchLibrary(true) }) @@ -534,6 +622,77 @@ onBeforeUnmount(() => { border-color: #f5a524; color: #f5a524; } +.lib-modal-wipe { + border: 1px solid #5c2a2a; + border-radius: 8px; + background: transparent; + color: #f0a0a0; + padding: 8px 10px; + font: inherit; + font-size: 12px; + cursor: pointer; + white-space: nowrap; +} +.lib-modal-wipe:hover:not(:disabled) { + border-color: #e07070; + color: #ffc0c0; +} +.lib-modal-wipe:disabled { opacity: 0.5; cursor: default; } +.lib-modal-confirm { + position: absolute; + inset: 0; + z-index: 5; + display: flex; + align-items: center; + justify-content: center; + background: #000a; + padding: 16px; +} +.lib-modal-confirm-card { + width: min(420px, 100%); + border: 1px solid #2a2a2e; + border-radius: 12px; + background: #1a1a1e; + padding: 18px; +} +.lib-modal-confirm-card h3 { + margin: 0; + font-size: 18px; +} +.lib-modal-confirm-name { + margin: 10px 0 0; + font-weight: 600; +} +.lib-modal-confirm-detail { + margin: 4px 0 0; + font-size: 12px; + color: #9a9588; +} +.lib-modal-confirm-body { + margin: 12px 0 0; + font-size: 13px; + color: #c8c4b8; +} +.lib-modal-confirm-actions { + display: flex; + justify-content: flex-end; + gap: 8px; + margin-top: 16px; +} +.lib-modal-confirm-actions button { + border: 1px solid #2a2a2e; + border-radius: 999px; + background: transparent; + color: inherit; + padding: 8px 14px; + font: inherit; + font-size: 13px; + cursor: pointer; +} +.lib-modal-confirm-actions button.danger { + border-color: #5c2a2a; + color: #f0a0a0; +} .lib-modal-filters { display: inline-flex; gap: 4px; } .lib-modal-chip { border: 1px solid #2a2a2e; diff --git a/pages/library.vue b/pages/library.vue index 9dd701e..1547f96 100644 --- a/pages/library.vue +++ b/pages/library.vue @@ -87,8 +87,8 @@ > {{ selectMode ? 'Done selecting' : 'Select' }} - +
@@ -488,6 +495,31 @@
+
@@ -601,6 +633,10 @@ const revealAll = ref(false) const hidingFocused = ref(false) const forceHiddenIds = ref([]) const pendingDelete = ref<{ title: string; name: string; detail?: string; consequence: string; keys: string[] } | null>(null) +const pendingWipe = ref<{ folderId: string; name: string; count: number } | null>(null) +const wipeBusy = ref(false) +const libraryLoading = ref(false) +const suppressedDeletedIds = ref(new Set()) const keepingInputBusy = ref(false) const toasts = ref>([]) let toastId = 0 @@ -613,6 +649,13 @@ const viewportW = ref(800) const selectedSet = computed(() => new Set(selectedKeys.value)) const browseFolder = computed(() => folders.value.find(folder => folder.id === folderId.value) || null) +const folderWipeCount = computed(() => { + if (!browseFolder.value || folderId.value === 'all') return 0 + const id = folderId.value + return stills.value.filter(item => item.folderId === id).length + + clips.value.filter(item => item.folderId === id).length + + tracks.value.filter(item => item.folderId === id).length +}) const entries = computed(() => buildLibraryEntries({ clips: clips.value, stills: stills.value, @@ -778,15 +821,21 @@ function applyLibrary(data: unknown, opts?: { replace?: boolean }) { return } if (payload.folders?.length) folders.value = payload.folders + const drop = suppressedDeletedIds.value + const keep = (list: T[]) => list.filter(item => !drop.has(item.id)) if (replace) { - clips.value = payload.clips || [] - stills.value = payload.stills || [] - tracks.value = payload.tracks || [] + clips.value = keep(payload.clips || []) + stills.value = keep(payload.stills || []) + tracks.value = keep(payload.tracks || []) } else { const merge = (prev: T[], next: T[]) => { const map = new Map() - for (const item of prev) map.set(item.id, item) - for (const item of next) map.set(item.id, item) + for (const item of prev) { + if (!drop.has(item.id)) map.set(item.id, item) + } + for (const item of next) { + if (!drop.has(item.id)) map.set(item.id, item) + } return [...map.values()] } clips.value = merge(clips.value, payload.clips || []) @@ -815,18 +864,37 @@ async function fetchLibraryPage(cursor: string | null) { async function loadLibrary() { // First page paints immediately; remaining pages append in the background const gen = ++libraryFetchGen - const first = await fetchLibraryPage(null) - if (gen !== libraryFetchGen) return - applyLibrary(first, { replace: true }) - let cursor = first?.nextCursor || null - while (cursor && gen === libraryFetchGen) { - const page = await fetchLibraryPage(cursor) + libraryLoading.value = true + try { + const first = await fetchLibraryPage(null) if (gen !== libraryFetchGen) return - applyLibrary(page, { replace: false }) - cursor = page?.nextCursor || null + applyLibrary(first, { replace: true }) + let cursor = first?.nextCursor || null + while (cursor && gen === libraryFetchGen) { + const page = await fetchLibraryPage(cursor) + if (gen !== libraryFetchGen) return + applyLibrary(page, { replace: false }) + cursor = page?.nextCursor || null + } + if (gen === libraryFetchGen) { + // Drop suppressions that the server no longer returns + const live = new Set([ + ...stills.value.map(item => item.id), + ...clips.value.map(item => item.id), + ...tracks.value.map(item => item.id) + ]) + suppressedDeletedIds.value = new Set([...suppressedDeletedIds.value].filter(id => live.has(id))) + } + } finally { + if (gen === libraryFetchGen) libraryLoading.value = false } } +async function ensureLibraryFullyLoaded() { + if (!libraryLoading.value && !libraryNextCursor.value) return + await loadLibrary() +} + function folderChipCount(folder: LibraryFolder) { // Prefer server totals so chips match the full folder before pages finish appending if (filter.value === 'videos') return folder.clipCount || 0 @@ -975,7 +1043,9 @@ function toggleSelectMode() { if (selectMode.value) clearFocus() } -function selectAll() { +async function selectAll() { + // Whole folder — not only the page already painted + await ensureLibraryFullyLoaded() selectedKeys.value = entries.value.map(entry => entry.key) lastSelectedKey.value = selectedKeys.value.at(-1) || '' } @@ -1217,6 +1287,16 @@ function askRemoveKeys(keys: string[]) { } } +function markDeletedIds(ids: string[]) { + if (!ids.length) return + const next = new Set(suppressedDeletedIds.value) + for (const id of ids) next.add(id) + suppressedDeletedIds.value = next + stills.value = stills.value.filter(item => !next.has(item.id)) + clips.value = clips.value.filter(item => !next.has(item.id)) + tracks.value = tracks.value.filter(item => !next.has(item.id)) +} + async function confirmDelete() { const pending = pendingDelete.value if (!pending || deleteBusy.value) return @@ -1224,6 +1304,22 @@ async function confirmDelete() { deleteBusy.value = true try { const items = selectedEntries(pending.keys) + const gone: string[] = [] + for (const entry of items) { + if (entry.kind === 'clip' && entry.group) { + for (const part of entry.group.parts || [entry.group.head]) gone.push(part.id) + } else if (entry.kind === 'track' && entry.track) { + gone.push(entry.track.id) + } else if (entry.stillGroup) { + for (const part of entry.stillGroup.parts || [entry.stillGroup.head]) gone.push(part.id) + } + } + markDeletedIds(gone) + if (pending.keys.includes(focusedKey.value)) { + focusedKey.value = '' + focusedPartId.value = '' + } + clearSelection() await Promise.allSettled(items.map((entry) => { if (entry.kind === 'clip') { return $fetch(`/api/library/clips/${entry.group!.head.id}`, { method: 'DELETE', query: { family: '1' } }) @@ -1233,17 +1329,53 @@ async function confirmDelete() { } return $fetch(`/api/library/stills/${entry.stillGroup!.head.id}`, { method: 'DELETE', query: { family: '1' } }) })) - if (pending.keys.includes(focusedKey.value)) { - focusedKey.value = '' - focusedPartId.value = '' - } - clearSelection() await loadLibrary() } finally { deleteBusy.value = false } } +async function askWipeFolder() { + if (!browseFolder.value || folderId.value === 'all' || wipeBusy.value) return + await ensureLibraryFullyLoaded() + const count = folderWipeCount.value + if (!count) { + toast('This folder is already empty.') + return + } + pendingWipe.value = { + folderId: folderId.value, + name: browseFolder.value.name, + count + } +} + +async function confirmWipeFolder() { + const pending = pendingWipe.value + if (!pending || wipeBusy.value) return + pendingWipe.value = null + wipeBusy.value = true + try { + const gone = [ + ...stills.value.filter(item => item.folderId === pending.folderId).map(item => item.id), + ...clips.value.filter(item => item.folderId === pending.folderId).map(item => item.id), + ...tracks.value.filter(item => item.folderId === pending.folderId).map(item => item.id) + ] + markDeletedIds(gone) + clearSelection() + clearFocus() + const result = await $fetch(`/api/library/folders/${pending.folderId}/wipe`, { method: 'POST' }) + applyLibrary(result, { replace: true }) + toast(`Deleted ${result.deleted ?? pending.count} from ${pending.name}.`) + await loadLibrary() + } catch (error: any) { + toast(error?.data?.statusMessage || 'Could not empty that folder') + await loadLibrary() + } finally { + wipeBusy.value = false + } +} + async function unlockFolder() { if (!browseFolder.value) return try { @@ -1267,7 +1399,8 @@ function onKey(event: KeyboardEvent) { const target = event.target as HTMLElement | null const typing = Boolean(target && (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.tagName === 'SELECT' || target.isContentEditable)) if (event.key === 'Escape') { - if (pendingDelete.value) pendingDelete.value = null + if (pendingWipe.value) pendingWipe.value = null + else if (pendingDelete.value) pendingDelete.value = null else if (lightboxOpen.value) lightboxOpen.value = false else if (renaming.value) cancelRename() else if (focusedKey.value) clearFocus() @@ -1276,7 +1409,7 @@ function onKey(event: KeyboardEvent) { } if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 'a' && !renaming.value) { event.preventDefault() - selectAll() + void selectAll() } if (event.key === 'Delete' && selectedKeys.value.length && !renaming.value) askRemoveSelected() if (!typing && (event.key === 'ArrowLeft' || event.key === 'ArrowRight') && focusedNavParts.value.length > 1) { @@ -1293,6 +1426,7 @@ watch(searchDraft, (value) => { }) watch([folderId, filter], () => { clearSelection() + suppressedDeletedIds.value = new Set() if (scroller.value) scroller.value.scrollTop = 0 }) watch(search, () => { diff --git a/server/api/library/folders/[id]/wipe.post.ts b/server/api/library/folders/[id]/wipe.post.ts new file mode 100644 index 0000000..3b9f21d --- /dev/null +++ b/server/api/library/folders/[id]/wipe.post.ts @@ -0,0 +1,7 @@ +export default defineEventHandler(async (event) => { + const { owner } = assertLibraryOwner(event) + const id = String(getRouterParam(event, 'id') || '') + assertFolderAccess(event, id) + const result = await wipeFolderContents(owner, id) + return { ...result, ...publicLibrary(event, { light: true }) } +}) diff --git a/server/utils/library.ts b/server/utils/library.ts index 6bb1e73..e9c3d26 100644 --- a/server/utils/library.ts +++ b/server/utils/library.ts @@ -587,7 +587,7 @@ export function publicFolder(event: H3Event, folder: LibraryFolder, clipCount = export function publicLibrary(event: H3Event, opts?: { light?: boolean }) { const light = opts?.light === true const owner = libraryOwnerKey(event) - const catalog = readCatalog(owner) + const catalog = catalogWithPrunedStills(owner) const clipIds = new Set(catalog.clips.map(clip => clip.id.toLowerCase())) const clipCounts = new Map() const imageCounts = new Map() @@ -634,7 +634,7 @@ export function publicLibrary(event: H3Event, opts?: { light?: boolean }) { } } -/** Paginated metadata page — reads catalog rows only; no existsSync / full publicLibrary map. */ +/** Paginated metadata page. Prunes stills whose master file is missing before listing. */ export function publicLibraryPage( event: H3Event, opts: { limit?: number; cursor?: string; folderId?: string; type?: string } = {} @@ -644,7 +644,7 @@ export function publicLibraryPage( const folderId = String(opts.folderId || '') const typeFilter = String(opts.type || 'all') const owner = libraryOwnerKey(event) - const catalog = readCatalog(owner) + const catalog = catalogWithPrunedStills(owner) const clipIds = new Set(catalog.clips.map(clip => clip.id.toLowerCase())) const clipCounts = new Map() const imageCounts = new Map() @@ -876,6 +876,73 @@ export function deleteFolder(owner: string, id: string) { }) } +/** 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 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) + 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 }) + } + return { + folderId: folder.id, + folderName: folder.name, + deleted: clips.length + stills.length + tracks.length, + stills: stills.length, + clips: clips.length, + tracks: tracks.length + } + }) +} + +/** 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() + for (const still of catalog.stills) { + if (existsSync(stillPath(owner, still.id))) { + keep.push(still) + continue + } + droppedIds.add(still.id) + removeStillFiles(owner, still.id) + } + 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 } + }) + return droppedIds.size +} + +function catalogWithPrunedStills(owner: string): Catalog { + const catalog = readCatalog(owner) + if (pruneMissingStillMasters(owner, catalog)) writeCatalog(owner, catalog) + return catalog +} + export async function updateFolder(owner: string, id: string, patch: { name?: string thumbnailDefault?: boolean