2023-12-14 11:55:40 -05:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2024-09-07 13:21:25 -04:00
|
|
|
import { join } from 'node:path';
|
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-09-07 13:21:25 -04:00
|
|
|
import { SystemMetadataKey } from 'src/enum';
|
|
|
|
|
import { DatabaseLock, IDatabaseRepository } from 'src/interfaces/database.interface';
|
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';
|
2024-09-07 13:21:25 -04:00
|
|
|
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
|
|
|
|
|
import { ImmichStartupError } from 'src/utils/events';
|
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(
|
2024-09-07 13:21:25 -04:00
|
|
|
@Inject(IDatabaseRepository) private databaseRepository: IDatabaseRepository,
|
2024-04-17 03:00:31 +05:30
|
|
|
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
|
|
|
|
|
@Inject(ILoggerRepository) private logger: ILoggerRepository,
|
2024-09-07 13:21:25 -04:00
|
|
|
@Inject(ISystemMetadataRepository) private systemMetadata: ISystemMetadataRepository,
|
2024-04-17 03:00:31 +05:30
|
|
|
) {
|
|
|
|
|
this.logger.setContext(StorageService.name);
|
|
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2024-08-27 18:06:50 -04:00
|
|
|
@OnEmit({ event: 'app.bootstrap' })
|
2024-09-07 13:21:25 -04:00
|
|
|
async onBootstrap() {
|
|
|
|
|
await this.databaseRepository.withLock(DatabaseLock.SystemFileMounts, async () => {
|
|
|
|
|
const flags = (await this.systemMetadata.get(SystemMetadataKey.SYSTEM_FLAGS)) || { mountFiles: false };
|
2024-09-23 11:16:25 -04:00
|
|
|
const enabled = flags.mountFiles ?? false;
|
2024-09-07 13:21:25 -04:00
|
|
|
|
2024-09-23 11:16:25 -04:00
|
|
|
this.logger.log(`Verifying system mount folder checks (enabled=${enabled})`);
|
2024-09-07 13:21:25 -04:00
|
|
|
|
|
|
|
|
// check each folder exists and is writable
|
|
|
|
|
for (const folder of Object.values(StorageFolder)) {
|
2024-09-23 11:16:25 -04:00
|
|
|
if (!enabled) {
|
2024-09-07 13:21:25 -04:00
|
|
|
this.logger.log(`Writing initial mount file for the ${folder} folder`);
|
2024-09-21 00:16:53 +01:00
|
|
|
await this.createMountFile(folder);
|
2024-09-07 13:21:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.verifyReadAccess(folder);
|
|
|
|
|
await this.verifyWriteAccess(folder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!flags.mountFiles) {
|
|
|
|
|
flags.mountFiles = true;
|
|
|
|
|
await this.systemMetadata.set(SystemMetadataKey.SYSTEM_FLAGS, flags);
|
|
|
|
|
this.logger.log('Successfully enabled system mount folders checks');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.logger.log('Successfully verified system mount folder checks');
|
|
|
|
|
});
|
2023-05-28 21:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2024-09-07 13:21:25 -04:00
|
|
|
|
|
|
|
|
private async verifyReadAccess(folder: StorageFolder) {
|
|
|
|
|
const { filePath } = this.getMountFilePaths(folder);
|
|
|
|
|
try {
|
|
|
|
|
await this.storageRepository.readFile(filePath);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.logger.error(`Failed to read ${filePath}: ${error}`);
|
|
|
|
|
this.logger.error(
|
|
|
|
|
`The "${folder}" folder appears to be offline/missing, please make sure the volume is mounted with the correct permissions`,
|
|
|
|
|
);
|
|
|
|
|
throw new ImmichStartupError(`Failed to validate folder mount (read from "<MEDIA_LOCATION>/${folder}")`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-21 00:16:53 +01:00
|
|
|
private async createMountFile(folder: StorageFolder) {
|
2024-09-07 13:21:25 -04:00
|
|
|
const { folderPath, filePath } = this.getMountFilePaths(folder);
|
|
|
|
|
try {
|
|
|
|
|
this.storageRepository.mkdirSync(folderPath);
|
2024-09-21 00:16:53 +01:00
|
|
|
await this.storageRepository.createFile(filePath, Buffer.from(`${Date.now()}`));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.logger.error(`Failed to create ${filePath}: ${error}`);
|
|
|
|
|
this.logger.error(
|
|
|
|
|
`The "${folder}" folder cannot be written to, please make sure the volume is mounted with the correct permissions`,
|
|
|
|
|
);
|
|
|
|
|
throw new ImmichStartupError(`Failed to validate folder mount (write to "<UPLOAD_LOCATION>/${folder}")`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async verifyWriteAccess(folder: StorageFolder) {
|
|
|
|
|
const { filePath } = this.getMountFilePaths(folder);
|
|
|
|
|
try {
|
|
|
|
|
await this.storageRepository.overwriteFile(filePath, Buffer.from(`${Date.now()}`));
|
2024-09-07 13:21:25 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
this.logger.error(`Failed to write ${filePath}: ${error}`);
|
|
|
|
|
this.logger.error(
|
|
|
|
|
`The "${folder}" folder cannot be written to, please make sure the volume is mounted with the correct permissions`,
|
|
|
|
|
);
|
2024-09-21 00:16:53 +01:00
|
|
|
throw new ImmichStartupError(`Failed to validate folder mount (write to "<UPLOAD_LOCATION>/${folder}")`);
|
2024-09-07 13:21:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getMountFilePaths(folder: StorageFolder) {
|
|
|
|
|
const folderPath = StorageCore.getBaseFolder(folder);
|
|
|
|
|
const filePath = join(folderPath, '.immich');
|
|
|
|
|
|
|
|
|
|
return { folderPath, filePath };
|
|
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|