refactor: library type (#9525)

This commit is contained in:
Jason Rasmussen
2024-05-20 18:09:10 -04:00
committed by GitHub
parent 4353153fe6
commit 84d824d6a7
66 changed files with 183 additions and 984 deletions
+11 -48
View File
@@ -12,7 +12,6 @@ import {
LibraryResponseDto,
LibraryStatsResponseDto,
ScanLibraryDto,
SearchLibraryDto,
UpdateLibraryDto,
ValidateLibraryDto,
ValidateLibraryImportPathResponseDto,
@@ -20,7 +19,7 @@ import {
mapLibrary,
} from 'src/dtos/library.dto';
import { AssetType } from 'src/entities/asset.entity';
import { LibraryEntity, LibraryType } from 'src/entities/library.entity';
import { LibraryEntity } from 'src/entities/library.entity';
import { IAssetRepository, WithProperty } from 'src/interfaces/asset.interface';
import { ICryptoRepository } from 'src/interfaces/crypto.interface';
import { DatabaseLock, IDatabaseRepository } from 'src/interfaces/database.interface';
@@ -118,10 +117,7 @@ export class LibraryService {
}
const library = await this.findOrFail(id);
if (library.type !== LibraryType.EXTERNAL) {
throw new BadRequestException('Can only watch external libraries');
} else if (library.importPaths.length === 0) {
if (library.importPaths.length === 0) {
return false;
}
@@ -212,8 +208,7 @@ export class LibraryService {
return false;
}
const libraries = await this.repository.getAll(false, LibraryType.EXTERNAL);
const libraries = await this.repository.getAll(false);
for (const library of libraries) {
await this.watch(library.id);
}
@@ -229,8 +224,8 @@ export class LibraryService {
return mapLibrary(library);
}
async getAll(dto: SearchLibraryDto): Promise<LibraryResponseDto[]> {
const libraries = await this.repository.getAll(false, dto.type);
async getAll(): Promise<LibraryResponseDto[]> {
const libraries = await this.repository.getAll(false);
return libraries.map((library) => mapLibrary(library));
}
@@ -244,37 +239,12 @@ export class LibraryService {
}
async create(dto: CreateLibraryDto): Promise<LibraryResponseDto> {
switch (dto.type) {
case LibraryType.EXTERNAL: {
if (!dto.name) {
dto.name = 'New External Library';
}
break;
}
case LibraryType.UPLOAD: {
if (!dto.name) {
dto.name = 'New Upload Library';
}
if (dto.importPaths && dto.importPaths.length > 0) {
throw new BadRequestException('Upload libraries cannot have import paths');
}
if (dto.exclusionPatterns && dto.exclusionPatterns.length > 0) {
throw new BadRequestException('Upload libraries cannot have exclusion patterns');
}
break;
}
}
const library = await this.repository.create({
ownerId: dto.ownerId,
name: dto.name,
type: dto.type,
name: dto.name ?? 'New External Library',
importPaths: dto.importPaths ?? [],
exclusionPatterns: dto.exclusionPatterns ?? [],
});
this.logger.log(`Creating ${dto.type} library for ${dto.ownerId}}`);
return mapLibrary(library);
}
@@ -362,11 +332,7 @@ export class LibraryService {
}
async delete(id: string) {
const library = await this.findOrFail(id);
const uploadCount = await this.repository.getUploadLibraryCount(library.ownerId);
if (library.type === LibraryType.UPLOAD && uploadCount <= 1) {
throw new BadRequestException('Cannot delete the last upload library');
}
await this.findOrFail(id);
if (this.watchLibraries) {
await this.unwatch(id);
@@ -529,10 +495,7 @@ export class LibraryService {
}
async queueScan(id: string, dto: ScanLibraryDto) {
const library = await this.findOrFail(id);
if (library.type !== LibraryType.EXTERNAL) {
throw new BadRequestException('Can only refresh external libraries');
}
await this.findOrFail(id);
await this.jobRepository.queue({
name: JobName.LIBRARY_SCAN,
@@ -556,7 +519,7 @@ export class LibraryService {
await this.jobRepository.queue({ name: JobName.LIBRARY_QUEUE_CLEANUP, data: {} });
// Queue all library refresh
const libraries = await this.repository.getAll(true, LibraryType.EXTERNAL);
const libraries = await this.repository.getAll(true);
await this.jobRepository.queueAll(
libraries.map((library) => ({
name: JobName.LIBRARY_SCAN,
@@ -587,8 +550,8 @@ export class LibraryService {
async handleQueueAssetRefresh(job: ILibraryRefreshJob): Promise<JobStatus> {
const library = await this.repository.get(job.id);
if (!library || library.type !== LibraryType.EXTERNAL) {
this.logger.warn('Can only refresh external libraries');
if (!library) {
this.logger.warn('Library not found');
return JobStatus.FAILED;
}