2024-10-02 10:54:35 -04:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2025-07-02 09:31:20 -04:00
|
|
|
import { join, resolve } from 'node:path';
|
2024-09-27 10:28:42 -04:00
|
|
|
import { StorageCore } from 'src/cores/storage.core';
|
2024-10-31 13:42:58 -04:00
|
|
|
import { OnEvent, OnJob } from 'src/decorators';
|
2025-02-11 17:15:56 -05:00
|
|
|
import { DatabaseLock, JobName, JobStatus, QueueName, StorageFolder, SystemMetadataKey } from 'src/enum';
|
2024-10-02 10:54:35 -04:00
|
|
|
import { BaseService } from 'src/services/base.service';
|
2025-03-28 10:40:09 -04:00
|
|
|
import { JobOf, SystemFlags } from 'src/types';
|
2024-10-31 13:42:58 -04:00
|
|
|
import { ImmichStartupError } from 'src/utils/misc';
|
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-10-02 10:54:35 -04:00
|
|
|
export class StorageService extends BaseService {
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'AppBootstrap' })
|
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 () => {
|
2024-10-29 14:43:27 +00:00
|
|
|
const flags =
|
|
|
|
|
(await this.systemMetadataRepository.get(SystemMetadataKey.SYSTEM_FLAGS)) ||
|
|
|
|
|
({ mountChecks: {} } as SystemFlags);
|
2024-09-07 13:21:25 -04:00
|
|
|
|
2024-10-29 14:43:27 +00:00
|
|
|
if (!flags.mountChecks) {
|
|
|
|
|
flags.mountChecks = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let updated = false;
|
|
|
|
|
|
|
|
|
|
this.logger.log(`Verifying system mount folder checks, current state: ${JSON.stringify(flags)}`);
|
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)) {
|
2024-10-29 14:43:27 +00:00
|
|
|
if (!flags.mountChecks[folder]) {
|
2024-10-01 13:04:37 -04:00
|
|
|
this.logger.log(`Writing initial mount file for the ${folder} folder`);
|
|
|
|
|
await this.createMountFile(folder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.verifyReadAccess(folder);
|
|
|
|
|
await this.verifyWriteAccess(folder);
|
2024-10-29 14:43:27 +00:00
|
|
|
|
|
|
|
|
if (!flags.mountChecks[folder]) {
|
|
|
|
|
flags.mountChecks[folder] = true;
|
|
|
|
|
updated = true;
|
|
|
|
|
}
|
2024-09-07 13:21:25 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-29 14:43:27 +00:00
|
|
|
if (updated) {
|
2024-10-02 10:54:35 -04:00
|
|
|
await this.systemMetadataRepository.set(SystemMetadataKey.SYSTEM_FLAGS, flags);
|
2024-10-01 13:04:37 -04:00
|
|
|
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) {
|
2025-02-27 14:59:50 -05:00
|
|
|
this.logger.error(error as Error);
|
2024-10-01 13:04:37 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-10-31 13:42:58 -04:00
|
|
|
@OnJob({ name: JobName.DELETE_FILES, queue: QueueName.BACKGROUND_TASK })
|
|
|
|
|
async handleDeleteFiles(job: JobOf<JobName.DELETE_FILES>): Promise<JobStatus> {
|
2023-02-25 09:12:03 -05:00
|
|
|
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) {
|
2025-07-02 09:31:20 -04:00
|
|
|
const fullyQualifiedPath = resolve(process.cwd(), internalPath);
|
|
|
|
|
this.logger.error(`Failed to read ${fullyQualifiedPath} (${internalPath}): ${error}`);
|
|
|
|
|
throw new ImmichStartupError(`Failed to read: "${externalPath} (${fullyQualifiedPath}) - ${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
|
|
|
}
|