mirror of
https://github.com/sakaljurgis/kodi-api-server.git
synced 2026-07-08 20:37:41 +00:00
migrations; torr search, dl, seed, delete; files scan, delete; configs
This commit is contained in:
@@ -2,11 +2,16 @@ import { Injectable } from '@nestjs/common';
|
||||
import { VideoFilesScannerService } from './Scanner/video-files-scanner.service';
|
||||
import { VideoFilesUpdateRepository } from './video-files-update.repository';
|
||||
import { FileEntityExpander } from './Expander/file-entity.expander';
|
||||
import { FileEntity } from '../Entity/file.entity';
|
||||
import { StreamProviderEnum } from '../../Streamer/ReadStreamProvider/stream-provider.enum';
|
||||
import { FileEntity } from '../../Shared/Entity/file.entity';
|
||||
import { StreamProviderEnum } from '../../Shared/Enum/stream-provider.enum';
|
||||
import { configService } from '../../config/config.service';
|
||||
import { VideoFilesConfig } from '../../config/video-files.config';
|
||||
|
||||
@Injectable()
|
||||
export class VideoFilesUpdateService {
|
||||
private readonly config: VideoFilesConfig =
|
||||
configService.getVideoFilesConfig();
|
||||
|
||||
constructor(
|
||||
private readonly scanner: VideoFilesScannerService,
|
||||
private readonly repository: VideoFilesUpdateRepository,
|
||||
@@ -20,20 +25,30 @@ export class VideoFilesUpdateService {
|
||||
|
||||
const filesInDb = await this.repository.getMatchingPaths(filesInFs);
|
||||
const filesToAddToDb = filesInFs.filter(
|
||||
(filePath) => !filesInDb.includes(filePath),
|
||||
(filePath) =>
|
||||
!filesInDb.includes(filePath) && !this.isPathToIgnore(filePath),
|
||||
);
|
||||
|
||||
const entitiesToSave = [];
|
||||
|
||||
for (const fileToAddToDb of filesToAddToDb) {
|
||||
if (this.checkPathToIgnore(fileToAddToDb)) {
|
||||
const fileEntity = await this.repository.findOrCreateFile(
|
||||
fileToAddToDb,
|
||||
false,
|
||||
);
|
||||
|
||||
//check if download is complete
|
||||
if (
|
||||
fileEntity.streamProvider === StreamProviderEnum.wt &&
|
||||
fileEntity.torrent &&
|
||||
fileEntity.torrent.stopped === false
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
fileEntity.relativePath = filesToRelativePaths[fileToAddToDb];
|
||||
fileEntity.streamProvider = StreamProviderEnum.fs;
|
||||
|
||||
const fileEntity = await this.buildFileEntity(
|
||||
fileToAddToDb,
|
||||
filesToRelativePaths[fileToAddToDb],
|
||||
);
|
||||
await this.expander.expand(fileEntity);
|
||||
|
||||
entitiesToSave.push(fileEntity);
|
||||
}
|
||||
@@ -41,19 +56,63 @@ export class VideoFilesUpdateService {
|
||||
await this.repository.saveFiles(entitiesToSave);
|
||||
}
|
||||
|
||||
private checkPathToIgnore(filePath: string): boolean {
|
||||
//todo - move to separate provider, include in settings
|
||||
return filePath.indexOf('AOE1') > -1;
|
||||
private isPathToIgnore(filePath: string): boolean {
|
||||
for (const pathPattern of this.config.getPathPatternsToIgnore()) {
|
||||
if (filePath.indexOf(pathPattern) > -1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private async buildFileEntity(
|
||||
path: string,
|
||||
relativePath: string,
|
||||
): Promise<FileEntity> {
|
||||
const fileEntity = await this.repository.findOrCreateFile(path, false);
|
||||
fileEntity.relativePath = relativePath;
|
||||
fileEntity.streamProvider = StreamProviderEnum.fs;
|
||||
async updateEntitiesByPath(filePaths: string[]) {
|
||||
const fileEntitiesToUpdate = [];
|
||||
const fileEntities = await this.repository.findFilesByPaths(filePaths);
|
||||
|
||||
return await this.expander.expand(fileEntity);
|
||||
for (const fileEntity of fileEntities) {
|
||||
if (!fileEntity.relativePath) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
fileEntity.streamProvider === StreamProviderEnum.wt &&
|
||||
fileEntity.torrent &&
|
||||
fileEntity.torrent.stopped === false
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
fileEntity.streamProvider = StreamProviderEnum.fs;
|
||||
|
||||
await this.expander.expand(fileEntity);
|
||||
fileEntitiesToUpdate.push(fileEntity);
|
||||
}
|
||||
|
||||
await this.repository.saveFiles(fileEntitiesToUpdate);
|
||||
}
|
||||
|
||||
public async buildAndSaveEntitiesFromPartial(
|
||||
partialFileEntities: Partial<FileEntity>[],
|
||||
): Promise<FileEntity[]> {
|
||||
const entitiesToSave: FileEntity[] = [];
|
||||
for (const partialFileEntity of partialFileEntities) {
|
||||
let fileEntity = await this.repository.findOrCreateFile(
|
||||
partialFileEntity.path,
|
||||
false,
|
||||
);
|
||||
|
||||
fileEntity = { ...fileEntity, ...partialFileEntity };
|
||||
|
||||
await this.expander.expand(fileEntity);
|
||||
entitiesToSave.push(fileEntity);
|
||||
}
|
||||
|
||||
await this.repository.saveFiles(entitiesToSave);
|
||||
|
||||
return this.repository.findFilesByPaths(
|
||||
entitiesToSave.map((fileEntity) => {
|
||||
return fileEntity.path;
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user