import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { FileEntity } from '../../Shared/Entity/file.entity'; import { In, Not, Repository, UpdateResult } from 'typeorm'; import { StreamProviderEnum } from '../../Shared/Enum/stream-provider.enum'; import { TitleEntity } from '../../Shared/Entity/title.entity'; import { TitleTypeEnum } from '../../Shared/Enum/title-type.enum'; @Injectable() export class VideoFilesUpdateRepository { constructor( @InjectRepository(FileEntity) private readonly fileRepository: Repository, @InjectRepository(TitleEntity) private readonly titleRepository: Repository, ) {} markRemovedFilesAsDeleted(filePaths: string[]): Promise { return this.fileRepository .createQueryBuilder() .update('files') .set({ deleted: true }) .where({ path: Not(In(filePaths)), deleted: false, streamProvider: StreamProviderEnum.fs, }) .execute(); } getMatchingPaths(filePaths: string[]): Promise { return new Promise((resolve) => { this.fileRepository .find({ select: ['path'], where: { path: In(filePaths), deleted: false, streamProvider: StreamProviderEnum.fs, }, }) .then((fileEntitiesInDb) => { resolve(fileEntitiesInDb.map((obj) => obj.path)); }) .catch(() => { resolve([]); }); }); } public async findOrCreateTitle( title: string, type: TitleTypeEnum, saveIfCreated = true, ): Promise { let titleEntity = await this.titleRepository.findOne({ where: { title: title, type: type }, }); if (titleEntity === null) { titleEntity = new TitleEntity(); titleEntity.title = title; titleEntity.type = type; if (saveIfCreated) { await this.titleRepository.save(titleEntity); titleEntity = await this.titleRepository.findOne({ where: { title: title, type: type }, }); } } return titleEntity; } public async findOrCreateFile( path: string, saveIfCreated = true, ): Promise { let fileEntity = await this.fileRepository.findOne({ where: { path: path }, relations: { torrent: true }, }); if (fileEntity === null) { fileEntity = new FileEntity(); fileEntity.path = path; if (saveIfCreated) { await this.fileRepository.save(fileEntity); fileEntity = await this.fileRepository.findOne({ where: { path: path }, relations: { torrent: true }, }); } } return fileEntity; } public saveFiles(fileEntities: FileEntity[]) { return this.fileRepository.save(fileEntities); } public findFilesByPaths(paths: string[]): Promise { return this.fileRepository.find({ where: { path: In(paths) }, relations: { torrent: true }, }); } }