mirror of
https://github.com/immich-app/immich.git
synced 2026-03-04 09:57:33 +03:00
48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
import { Kysely } from 'kysely';
|
|
import { InjectKysely } from 'nestjs-kysely';
|
|
import { DB } from 'src/db';
|
|
import { DummyValue, GenerateSql } from 'src/decorators';
|
|
import { AssetEntity, withExif } from 'src/entities/asset.entity';
|
|
import { IViewRepository } from 'src/interfaces/view.interface';
|
|
import { asUuid } from 'src/utils/database';
|
|
|
|
export class ViewRepository implements IViewRepository {
|
|
constructor(@InjectKysely() private db: Kysely<DB>) {}
|
|
|
|
@GenerateSql({ params: [DummyValue.UUID] })
|
|
async getUniqueOriginalPaths(userId: string): Promise<string[]> {
|
|
const results = await this.db
|
|
.selectFrom('assets')
|
|
.select((eb) => eb.fn<string>('substring', ['assets.originalPath', eb.val('^(.*/)[^/]*$')]).as('directoryPath'))
|
|
.distinct()
|
|
.where('ownerId', '=', asUuid(userId))
|
|
.where('isVisible', '=', true)
|
|
.where('isArchived', '=', false)
|
|
.where('deletedAt', 'is', null)
|
|
.execute();
|
|
|
|
return results.map((row) => row.directoryPath.replaceAll(/^\/|\/$/g, ''));
|
|
}
|
|
|
|
@GenerateSql({ params: [DummyValue.UUID, DummyValue.STRING] })
|
|
async getAssetsByOriginalPath(userId: string, partialPath: string): Promise<AssetEntity[]> {
|
|
const normalizedPath = partialPath.replaceAll(/^\/|\/$/g, '');
|
|
|
|
return this.db
|
|
.selectFrom('assets')
|
|
.selectAll('assets')
|
|
.$call(withExif)
|
|
.where('ownerId', '=', asUuid(userId))
|
|
.where('isVisible', '=', true)
|
|
.where('isArchived', '=', false)
|
|
.where('deletedAt', 'is', null)
|
|
.where('originalPath', 'like', `%${normalizedPath}/%`)
|
|
.where('originalPath', 'not like', `%${normalizedPath}/%/%`)
|
|
.orderBy(
|
|
(eb) => eb.fn('regexp_replace', ['assets.originalPath', eb.val('.*/(.+)'), eb.val(String.raw`\1`)]),
|
|
'asc',
|
|
)
|
|
.execute() as any as Promise<AssetEntity[]>;
|
|
}
|
|
}
|