perf(server): use queries to refresh library assets (#7685)
* use queries instead of js * missing await * add mock methods * fix test * update sql * linting
This commit is contained in:
@@ -144,6 +144,7 @@ describe(LibraryService.name, () => {
|
||||
|
||||
libraryMock.get.mockResolvedValue(libraryStub.externalLibrary1);
|
||||
storageMock.crawl.mockResolvedValue(['/data/user1/photo.jpg']);
|
||||
assetMock.getPathsNotInLibrary.mockResolvedValue(['/data/user1/photo.jpg']);
|
||||
assetMock.getByLibraryId.mockResolvedValue([]);
|
||||
userMock.get.mockResolvedValue(userStub.admin);
|
||||
|
||||
|
||||
@@ -621,29 +621,18 @@ export class LibraryService extends EventEmitter {
|
||||
pathsToCrawl: validImportPaths,
|
||||
exclusionPatterns: library.exclusionPatterns,
|
||||
});
|
||||
|
||||
const crawledAssetPaths = rawPaths.map((filePath) => path.normalize(filePath));
|
||||
|
||||
this.logger.debug(`Found ${crawledAssetPaths.length} asset(s) when crawling import paths ${library.importPaths}`);
|
||||
const assetsInLibrary = await this.assetRepository.getByLibraryId([job.id]);
|
||||
const onlineFiles = new Set(crawledAssetPaths);
|
||||
const offlineAssetIds = assetsInLibrary
|
||||
.filter((asset) => !onlineFiles.has(asset.originalPath))
|
||||
.filter((asset) => !asset.isOffline)
|
||||
.map((asset) => asset.id);
|
||||
this.logger.debug(`Marking ${offlineAssetIds.length} assets as offline`);
|
||||
|
||||
await this.assetRepository.updateAll(offlineAssetIds, { isOffline: true });
|
||||
await this.assetRepository.updateOfflineLibraryAssets(library.id, crawledAssetPaths);
|
||||
|
||||
if (crawledAssetPaths.length > 0) {
|
||||
let filteredPaths: string[] = [];
|
||||
if (job.refreshAllFiles || job.refreshModifiedFiles) {
|
||||
filteredPaths = crawledAssetPaths;
|
||||
} else {
|
||||
const onlinePathsInLibrary = new Set(
|
||||
assetsInLibrary.filter((asset) => !asset.isOffline).map((asset) => asset.originalPath),
|
||||
);
|
||||
filteredPaths = crawledAssetPaths.filter((assetPath) => !onlinePathsInLibrary.has(assetPath));
|
||||
filteredPaths = await this.assetRepository.getPathsNotInLibrary(library.id, crawledAssetPaths);
|
||||
|
||||
this.logger.debug(`Will import ${filteredPaths.length} new asset(s)`);
|
||||
}
|
||||
|
||||
@@ -136,6 +136,8 @@ export interface IAssetRepository {
|
||||
getLastUpdatedAssetForAlbumId(albumId: string): Promise<AssetEntity | null>;
|
||||
getByLibraryId(libraryIds: string[]): Promise<AssetEntity[]>;
|
||||
getByLibraryIdAndOriginalPath(libraryId: string, originalPath: string): Promise<AssetEntity | null>;
|
||||
getPathsNotInLibrary(libraryId: string, originalPaths: string[]): Promise<string[]>;
|
||||
updateOfflineLibraryAssets(libraryId: string, originalPaths: string[]): Promise<void>;
|
||||
deleteAll(ownerId: string): Promise<void>;
|
||||
getAll(pagination: PaginationOptions, options?: AssetSearchOptions): Paginated<AssetEntity>;
|
||||
getAllByFileCreationDate(
|
||||
|
||||
Reference in New Issue
Block a user