2023-12-14 11:55:40 -05:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2024-03-20 21:20:38 +01:00
|
|
|
import { StorageCore, StorageFolder } from 'src/cores/storage.core';
|
2024-08-15 16:12:41 -04:00
|
|
|
import { OnEmit } from 'src/decorators';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IDeleteFilesJob, JobStatus } from 'src/interfaces/job.interface';
|
2024-04-17 03:00:31 +05:30
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IStorageRepository } from 'src/interfaces/storage.interface';
|
2023-02-25 09:12:03 -05:00
|
|
|
|
|
|
|
|
@Injectable()
|
2024-08-15 16:12:41 -04:00
|
|
|
export class StorageService {
|
2024-04-17 03:00:31 +05:30
|
|
|
constructor(
|
|
|
|
|
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
|
|
|
|
|
@Inject(ILoggerRepository) private logger: ILoggerRepository,
|
|
|
|
|
) {
|
|
|
|
|
this.logger.setContext(StorageService.name);
|
|
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2024-08-15 16:12:41 -04:00
|
|
|
@OnEmit({ event: 'onBootstrap' })
|
|
|
|
|
onBootstrap() {
|
2023-10-14 13:12:59 -04:00
|
|
|
const libraryBase = StorageCore.getBaseFolder(StorageFolder.LIBRARY);
|
2023-05-28 21:48:07 -04:00
|
|
|
this.storageRepository.mkdirSync(libraryBase);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 09:12:03 -05:00
|
|
|
async handleDeleteFiles(job: IDeleteFilesJob) {
|
|
|
|
|
const { files } = job;
|
|
|
|
|
|
2023-05-26 15:43:24 -04:00
|
|
|
// TODO: one job per file
|
2023-02-25 09:12:03 -05:00
|
|
|
for (const file of files) {
|
|
|
|
|
if (!file) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await this.storageRepository.unlink(file);
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
this.logger.warn('Unable to remove file from disk', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-26 15:43:24 -04:00
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SUCCESS;
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|
|
|
|
|
}
|