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-09-27 10:28:42 -04:00
|
|
|
import { StorageCore } from 'src/cores/storage.core';
|
2024-09-30 10:35:11 -04:00
|
|
|
import { OnEvent } from 'src/decorators';
|
2024-09-27 10:28:42 -04:00
|
|
|
import { StorageFolder, SystemMetadataKey } from 'src/enum';
|
2024-10-01 13:04:37 -04:00
|
|
|
import { IConfigRepository } from 'src/interfaces/config.interface';
|
2024-09-07 13:21:25 -04:00
|
|
|
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
|
|
|
|
2024-10-01 13:04:37 -04:00
|
|
|
const docsMessage = `Please see https://immich.app/docs/administration/system-integrity#folder-checks for more information.`;
|
|
|
|
|
|
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-10-01 13:04:37 -04:00
|
|
|
@Inject(IConfigRepository) private configRepository: IConfigRepository,
|
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-09-30 10:35:11 -04:00
|
|
|
@OnEvent({ name: 'app.bootstrap' })
|
2024-09-07 13:21:25 -04:00
|
|
|
async onBootstrap() {
|
2024-10-01 13:04:37 -04:00
|
|
|
const envData = this.configRepository.getEnv();
|
|
|
|
|
|
2024-09-07 13:21:25 -04:00
|
|
|
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
|
|
|
|
2024-10-01 13:04:37 -04:00
|
|
|
try {
|
|
|
|
|
// check each folder exists and is writable
|
|
|
|
|
for (const folder of Object.values(StorageFolder)) {
|
|
|
|
|
if (!enabled) {
|
|
|
|
|
this.logger.log(`Writing initial mount file for the ${folder} folder`);
|
|
|
|
|
await this.createMountFile(folder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.verifyReadAccess(folder);
|
|
|
|
|
await this.verifyWriteAccess(folder);
|
2024-09-07 13:21:25 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 13:04:37 -04:00
|
|
|
if (!flags.mountFiles) {
|
|
|
|
|
flags.mountFiles = true;
|
|
|
|
|
await this.systemMetadata.set(SystemMetadataKey.SYSTEM_FLAGS, flags);
|
|
|
|
|
this.logger.log('Successfully enabled system mount folders checks');
|
|
|
|
|
}
|
2024-09-07 13:21:25 -04:00
|
|
|
|
2024-10-01 13:04:37 -04:00
|
|
|
this.logger.log('Successfully verified system mount folder checks');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (envData.storage.ignoreMountCheckErrors) {
|
|
|
|
|
this.logger.error(error);
|
|
|
|
|
this.logger.warn('Ignoring mount folder errors');
|
|
|
|
|
} else {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2024-09-07 13:21:25 -04:00
|
|
|
}
|
|
|
|
|
});
|
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) {
|
2024-10-01 13:04:37 -04:00
|
|
|
const { internalPath, externalPath } = this.getMountFilePaths(folder);
|
2024-09-07 13:21:25 -04:00
|
|
|
try {
|
2024-10-01 13:04:37 -04:00
|
|
|
await this.storageRepository.readFile(internalPath);
|
2024-09-07 13:21:25 -04:00
|
|
|
} catch (error) {
|
2024-10-01 13:04:37 -04:00
|
|
|
this.logger.error(`Failed to read ${internalPath}: ${error}`);
|
|
|
|
|
throw new ImmichStartupError(`Failed to read "${externalPath} - ${docsMessage}"`);
|
2024-09-07 13:21:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-21 00:16:53 +01:00
|
|
|
private async createMountFile(folder: StorageFolder) {
|
2024-10-01 13:04:37 -04:00
|
|
|
const { folderPath, internalPath, externalPath } = this.getMountFilePaths(folder);
|
2024-09-07 13:21:25 -04:00
|
|
|
try {
|
|
|
|
|
this.storageRepository.mkdirSync(folderPath);
|
2024-10-01 13:04:37 -04:00
|
|
|
await this.storageRepository.createFile(internalPath, Buffer.from(`${Date.now()}`));
|
2024-09-21 00:16:53 +01:00
|
|
|
} catch (error) {
|
2024-10-01 13:04:37 -04:00
|
|
|
if ((error as NodeJS.ErrnoException).code === 'EEXIST') {
|
|
|
|
|
this.logger.warn('Found existing mount file, skipping creation');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.logger.error(`Failed to create ${internalPath}: ${error}`);
|
|
|
|
|
throw new ImmichStartupError(`Failed to create "${externalPath} - ${docsMessage}"`);
|
2024-09-21 00:16:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async verifyWriteAccess(folder: StorageFolder) {
|
2024-10-01 13:04:37 -04:00
|
|
|
const { internalPath, externalPath } = this.getMountFilePaths(folder);
|
2024-09-21 00:16:53 +01:00
|
|
|
try {
|
2024-10-01 13:04:37 -04:00
|
|
|
await this.storageRepository.overwriteFile(internalPath, Buffer.from(`${Date.now()}`));
|
2024-09-07 13:21:25 -04:00
|
|
|
} catch (error) {
|
2024-10-01 13:04:37 -04:00
|
|
|
this.logger.error(`Failed to write ${internalPath}: ${error}`);
|
|
|
|
|
throw new ImmichStartupError(`Failed to write "${externalPath} - ${docsMessage}"`);
|
2024-09-07 13:21:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getMountFilePaths(folder: StorageFolder) {
|
|
|
|
|
const folderPath = StorageCore.getBaseFolder(folder);
|
2024-10-01 13:04:37 -04:00
|
|
|
const internalPath = join(folderPath, '.immich');
|
|
|
|
|
const externalPath = `<UPLOAD_LOCATION>/${folder}/.immich`;
|
2024-09-07 13:21:25 -04:00
|
|
|
|
2024-10-01 13:04:37 -04:00
|
|
|
return { folderPath, internalPath, externalPath };
|
2024-09-07 13:21:25 -04:00
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|