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-03-20 22:15:09 -05:00
|
|
|
import { IDeleteFilesJob, JobStatus } from 'src/interfaces/job.repository';
|
2024-03-20 21:42:58 +01:00
|
|
|
import { IStorageRepository } from 'src/interfaces/storage.repository';
|
2024-03-20 22:15:09 -05:00
|
|
|
import { ImmichLogger } from 'src/utils/logger';
|
2023-02-25 09:12:03 -05:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class StorageService {
|
2023-12-14 11:55:40 -05:00
|
|
|
private logger = new ImmichLogger(StorageService.name);
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-10-14 13:12:59 -04:00
|
|
|
constructor(@Inject(IStorageRepository) private storageRepository: IStorageRepository) {}
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-05-28 21:48:07 -04:00
|
|
|
init() {
|
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
|
|
|
}
|
|
|
|
|
}
|