mirror of
https://github.com/immich-app/immich.git
synced 2025-06-02 21:24:28 -04:00
feat(server): squash folder root
This commit is contained in:
parent
376282e538
commit
dfe43aa2b6
@ -1,3 +1,4 @@
|
|||||||
|
import { buildTree, type RecursiveObject } from '$lib/utils/tree-utils';
|
||||||
import {
|
import {
|
||||||
getAssetsByOriginalPath,
|
getAssetsByOriginalPath,
|
||||||
getUniqueOriginalPaths,
|
getUniqueOriginalPaths,
|
||||||
@ -15,6 +16,8 @@ class FoldersStore {
|
|||||||
private initialized = false;
|
private initialized = false;
|
||||||
uniquePaths = $state<string[]>([]);
|
uniquePaths = $state<string[]>([]);
|
||||||
assets = $state<AssetCache>({});
|
assets = $state<AssetCache>({});
|
||||||
|
rootPath = '';
|
||||||
|
tree = $state<RecursiveObject>({});
|
||||||
|
|
||||||
async fetchUniquePaths() {
|
async fetchUniquePaths() {
|
||||||
if (this.initialized) {
|
if (this.initialized) {
|
||||||
@ -24,6 +27,24 @@ class FoldersStore {
|
|||||||
|
|
||||||
const uniquePaths = await getUniqueOriginalPaths();
|
const uniquePaths = await getUniqueOriginalPaths();
|
||||||
this.uniquePaths.push(...uniquePaths);
|
this.uniquePaths.push(...uniquePaths);
|
||||||
|
|
||||||
|
this.tree = buildTree(foldersStore.uniquePaths);
|
||||||
|
|
||||||
|
const getAssets = async () => {
|
||||||
|
await this.fetchAssetsByPath(this.rootPath);
|
||||||
|
return this.assets[this.rootPath] || null;
|
||||||
|
};
|
||||||
|
|
||||||
|
let currentFolders = Object.keys(this.tree || {}).sort();
|
||||||
|
let currentAssets = await getAssets();
|
||||||
|
|
||||||
|
while (currentFolders.length === 1 && currentAssets.length === 0) {
|
||||||
|
const folder = currentFolders[0];
|
||||||
|
this.rootPath += `/${folder}`;
|
||||||
|
this.tree = this.tree[folder];
|
||||||
|
currentFolders = Object.keys(this.tree || {}).sort();
|
||||||
|
currentAssets = await getAssets();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bustAssetCache() {
|
bustAssetCache() {
|
||||||
@ -34,7 +55,7 @@ class FoldersStore {
|
|||||||
if (!path) {
|
if (!path) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.assets[path] = await getAssetsByOriginalPath({ path });
|
this.assets[path] = await getAssetsByOriginalPath({ path: this.getEffectivePath(path) });
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchAssetsByPath(path: string) {
|
async fetchAssetsByPath(path: string) {
|
||||||
@ -42,7 +63,11 @@ class FoldersStore {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.assets[path] = await getAssetsByOriginalPath({ path });
|
this.assets[path] = await getAssetsByOriginalPath({ path: this.getEffectivePath(path) });
|
||||||
|
}
|
||||||
|
|
||||||
|
private getEffectivePath(path: string) {
|
||||||
|
return this.rootPath ? this.rootPath + '/' + path : path;
|
||||||
}
|
}
|
||||||
|
|
||||||
clearCache() {
|
clearCache() {
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
import { AppRoute, QueryParameter } from '$lib/constants';
|
import { AppRoute, QueryParameter } from '$lib/constants';
|
||||||
import type { Viewport } from '$lib/stores/assets.store';
|
import type { Viewport } from '$lib/stores/assets.store';
|
||||||
import { foldersStore } from '$lib/stores/folders.svelte';
|
import { foldersStore } from '$lib/stores/folders.svelte';
|
||||||
import { buildTree, normalizeTreePath } from '$lib/utils/tree-utils';
|
import { normalizeTreePath } from '$lib/utils/tree-utils';
|
||||||
import { mdiDotsVertical, mdiFolder, mdiFolderHome, mdiFolderOutline, mdiPlus, mdiSelectAll } from '@mdi/js';
|
import { mdiDotsVertical, mdiFolder, mdiFolderHome, mdiFolderOutline, mdiPlus, mdiSelectAll } from '@mdi/js';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { t } from 'svelte-i18n';
|
import { t } from 'svelte-i18n';
|
||||||
@ -42,7 +42,7 @@
|
|||||||
const viewport: Viewport = $state({ width: 0, height: 0 });
|
const viewport: Viewport = $state({ width: 0, height: 0 });
|
||||||
|
|
||||||
let pathSegments = $derived(data.path ? data.path.split('/') : []);
|
let pathSegments = $derived(data.path ? data.path.split('/') : []);
|
||||||
let tree = $derived(buildTree(foldersStore.uniquePaths));
|
let tree = $derived(data.tree);
|
||||||
let currentPath = $derived($page.url.searchParams.get(QueryParameter.PATH) || '');
|
let currentPath = $derived($page.url.searchParams.get(QueryParameter.PATH) || '');
|
||||||
let currentTreeItems = $derived(currentPath ? data.currentFolders : Object.keys(tree).sort());
|
let currentTreeItems = $derived(currentPath ? data.currentFolders : Object.keys(tree).sort());
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import { foldersStore } from '$lib/stores/folders.svelte';
|
|||||||
import { authenticate } from '$lib/utils/auth';
|
import { authenticate } from '$lib/utils/auth';
|
||||||
import { getFormatter } from '$lib/utils/i18n';
|
import { getFormatter } from '$lib/utils/i18n';
|
||||||
import { getAssetInfoFromParam } from '$lib/utils/navigation';
|
import { getAssetInfoFromParam } from '$lib/utils/navigation';
|
||||||
import { buildTree, normalizeTreePath } from '$lib/utils/tree-utils';
|
import { normalizeTreePath } from '$lib/utils/tree-utils';
|
||||||
import type { PageLoad } from './$types';
|
import type { PageLoad } from './$types';
|
||||||
|
|
||||||
export const load = (async ({ params, url }) => {
|
export const load = (async ({ params, url }) => {
|
||||||
@ -24,16 +24,19 @@ export const load = (async ({ params, url }) => {
|
|||||||
// We should bust the asset cache of the folder store, to make sure we don't show stale data
|
// We should bust the asset cache of the folder store, to make sure we don't show stale data
|
||||||
foldersStore.bustAssetCache();
|
foldersStore.bustAssetCache();
|
||||||
}
|
}
|
||||||
|
let tree = foldersStore.tree;
|
||||||
let tree = buildTree(foldersStore.uniquePaths);
|
|
||||||
const parts = normalizeTreePath(path || '').split('/');
|
const parts = normalizeTreePath(path || '').split('/');
|
||||||
|
|
||||||
for (const part of parts) {
|
for (const part of parts) {
|
||||||
tree = tree?.[part];
|
if (part) {
|
||||||
|
tree = tree?.[part];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
asset,
|
asset,
|
||||||
path,
|
path,
|
||||||
|
tree: foldersStore.tree,
|
||||||
currentFolders: Object.keys(tree || {}).sort(),
|
currentFolders: Object.keys(tree || {}).sort(),
|
||||||
pathAssets,
|
pathAssets,
|
||||||
meta: {
|
meta: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user