Skip to content

High performance self-hosted photo and video management solution.

refactor(server): common (#2066)

Jason Rasmussen1efc74dabc19
diff --git a/server/apps/immich/src/app.module.ts b/server/apps/immich/src/app.module.ts
index bc566b1fa9..0334baa374 100644
--- a/server/apps/immich/src/app.module.ts
+++ b/server/apps/immich/src/app.module.ts
@@ -1,4 +1,4 @@
-import { immichAppConfig } from '@app/common/config';
+import { immichAppConfig } from '@app/domain';
import { Module, OnModuleInit } from '@nestjs/common';
import { AssetModule } from './api-v1/asset/asset.module';
import { ConfigModule } from '@nestjs/config';
diff --git a/server/apps/immich/src/main.ts b/server/apps/immich/src/main.ts
index 38dd0695a0..df59aef457 100644
--- a/server/apps/immich/src/main.ts
+++ b/server/apps/immich/src/main.ts
@@ -9,7 +9,7 @@ import { AppModule } from './app.module';
import { RedisIoAdapter } from './middlewares/redis-io.adapter.middleware';
import { json } from 'body-parser';
import { patchOpenAPI } from './utils/patch-open-api.util';
-import { getLogLevels, MACHINE_LEARNING_ENABLED } from '@app/common';
+import { getLogLevels, MACHINE_LEARNING_ENABLED } from '@app/domain';
import { SERVER_VERSION, IMMICH_ACCESS_COOKIE, SearchService } from '@app/domain';
const logger = new Logger('ImmichServer');
diff --git a/server/apps/immich/test/jest-e2e.json b/server/apps/immich/test/jest-e2e.json
index 1d8ef78bba..d3095fe6e2 100644
--- a/server/apps/immich/test/jest-e2e.json
+++ b/server/apps/immich/test/jest-e2e.json
@@ -8,7 +8,6 @@
"^.+\\.(t|j)s$": "ts-jest"
},
"moduleNameMapper": {
- "^@app/common": "<rootDir>../../../libs/common/src",
"^@app/infra(|/.*)$": "<rootDir>../../../libs/infra/src/$1",
"^@app/domain(|/.*)$": "<rootDir>../../../libs/domain/src/$1"
}
diff --git a/server/apps/microservices/src/main.ts b/server/apps/microservices/src/main.ts
index 715376bd88..bccf01115d 100644
--- a/server/apps/microservices/src/main.ts
+++ b/server/apps/microservices/src/main.ts
@@ -1,7 +1,7 @@
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { SERVER_VERSION } from '@app/domain';
-import { getLogLevels } from '@app/common';
+import { getLogLevels } from '@app/domain';
import { RedisIoAdapter } from '../../immich/src/middlewares/redis-io.adapter.middleware';
import { MicroservicesModule } from './microservices.module';
diff --git a/server/apps/microservices/src/microservices.module.ts b/server/apps/microservices/src/microservices.module.ts
index 4530a139c7..d315a87776 100644
--- a/server/apps/microservices/src/microservices.module.ts
+++ b/server/apps/microservices/src/microservices.module.ts
@@ -1,4 +1,4 @@
-import { immichAppConfig } from '@app/common/config';
+import { immichAppConfig } from '@app/domain';
import { DomainModule } from '@app/domain';
import { ExifEntity, InfraModule } from '@app/infra';
import { Module } from '@nestjs/common';
diff --git a/server/libs/common/src/config/index.ts b/server/libs/common/src/config/index.ts
deleted file mode 100644
index 41e7ed15eb..0000000000
--- a/server/libs/common/src/config/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from './app.config';
diff --git a/server/libs/common/src/constants/index.ts b/server/libs/common/src/constants/index.ts
deleted file mode 100644
index 92313a895f..0000000000
--- a/server/libs/common/src/constants/index.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { BadRequestException } from '@nestjs/common';
-
-export const MACHINE_LEARNING_URL = process.env.IMMICH_MACHINE_LEARNING_URL || 'http://immich-machine-learning:3003';
-export const MACHINE_LEARNING_ENABLED = MACHINE_LEARNING_URL !== 'false';
-
-export function assertMachineLearningEnabled() {
- if (!MACHINE_LEARNING_ENABLED) {
- throw new BadRequestException('Machine learning is not enabled.');
- }
-}
diff --git a/server/libs/common/src/index.ts b/server/libs/common/src/index.ts
deleted file mode 100644
index 9128d1af46..0000000000
--- a/server/libs/common/src/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './config';
-export * from './constants';
-export * from './utils';
diff --git a/server/libs/common/src/utils/asset-utils.spec.ts b/server/libs/common/src/utils/asset-utils.spec.ts
deleted file mode 100644
index 9fa8f04c92..0000000000
--- a/server/libs/common/src/utils/asset-utils.spec.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { assetUtils } from './asset-utils';
-
-describe('Asset Utilities', () => {
- describe('isWebPlayable', () => {
- it('Check that it returns true with mimetype webm', () => {
- const result = assetUtils.isWebPlayable('video/webm');
- expect(result).toBeTruthy();
- });
-
- it('Check that returns true with mimetype mp4', () => {
- const result = assetUtils.isWebPlayable('video/mp4');
- expect(result).toBeTruthy();
- });
-
- it('Check that returns false with mimetype quicktime', () => {
- const result = assetUtils.isWebPlayable('video/quicktime');
- expect(result).toBeFalsy();
- });
- });
-});
diff --git a/server/libs/common/src/utils/asset-utils.ts b/server/libs/common/src/utils/asset-utils.ts
deleted file mode 100644
index 5ec65bfe2f..0000000000
--- a/server/libs/common/src/utils/asset-utils.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-import { AssetEntity } from '@app/infra/db/entities';
-import { AssetResponseDto } from '@app/domain';
-import fs from 'fs';
-
-const deleteFiles = (asset: AssetEntity | AssetResponseDto) => {
- fs.unlink(asset.originalPath, (err) => {
- if (err) {
- console.log('error deleting ', asset.originalPath);
- }
- });
-
- // TODO: what if there is no asset.resizePath. Should fail the Job?
- // => panoti report: Job not fail
- if (asset.resizePath) {
- fs.unlink(asset.resizePath, (err) => {
- if (err) {
- console.log('error deleting ', asset.resizePath);
- }
- });
- }
-
- if (asset.webpPath) {
- fs.unlink(asset.webpPath, (err) => {
- if (err) {
- console.log('error deleting ', asset.webpPath);
- }
- });
- }
-
- if (asset.encodedVideoPath) {
- fs.unlink(asset.encodedVideoPath, (err) => {
- if (err) {
- console.log('error deleting ', asset.encodedVideoPath);
- }
- });
- }
-};
-
-const isWebPlayable = (mimeType: string | null): boolean => {
- const WEB_PLAYABLE = ['video/webm', 'video/mp4'];
-
- if (mimeType !== null) {
- return WEB_PLAYABLE.includes(mimeType);
- }
- return false;
-};
-
-export const assetUtils = { deleteFiles, isWebPlayable };
diff --git a/server/libs/common/src/utils/index.ts b/server/libs/common/src/utils/index.ts
deleted file mode 100644
index 35d65fa7e1..0000000000
--- a/server/libs/common/src/utils/index.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { LogLevel } from '@nestjs/common';
-
-export * from './time-utils';
-export * from './asset-utils';
-
-export function getLogLevels() {
- const LOG_LEVELS: LogLevel[] = ['verbose', 'debug', 'log', 'warn', 'error'];
- let logLevel = process.env.LOG_LEVEL || 'log';
- if (logLevel === 'simple') {
- logLevel = 'log';
- }
- const logLevelIndex = LOG_LEVELS.indexOf(logLevel as LogLevel);
- return logLevelIndex === -1 ? [] : LOG_LEVELS.slice(logLevelIndex);
-}
diff --git a/server/libs/common/src/utils/time-utils.spec.ts b/server/libs/common/src/utils/time-utils.spec.ts
deleted file mode 100644
index 31cd4b6537..0000000000
--- a/server/libs/common/src/utils/time-utils.spec.ts
+++ /dev/null
@@ -1,43 +0,0 @@
-// create unit test for time utils
-
-import { timeUtils } from './time-utils';
-
-describe('Time Utilities', () => {
- describe('timezone', () => {
- it('should always be UTC', () => {
- expect(new Date().getTimezoneOffset()).toBe(0);
- });
- });
-
- describe('checkValidTimestamp', () => {
- it('check for year 0000', () => {
- const result = timeUtils.checkValidTimestamp('0000-00-00T00:00:00.000Z');
- expect(result).toBeFalsy();
- });
-
- it('check for 6-digits year with plus sign', () => {
- const result = timeUtils.checkValidTimestamp('+12345-00-00T00:00:00.000Z');
- expect(result).toBeFalsy();
- });
-
- it('check for 6-digits year with negative sign', () => {
- const result = timeUtils.checkValidTimestamp('-12345-00-00T00:00:00.000Z');
- expect(result).toBeFalsy();
- });
-
- it('check for current date', () => {
- const result = timeUtils.checkValidTimestamp(new Date().toISOString());
- expect(result).toBeTruthy();
- });
-
- it('check for year before 1583', () => {
- const result = timeUtils.checkValidTimestamp('1582-12-31T23:59:59.999Z');
- expect(result).toBeFalsy();
- });
-
- it('check for year after 9999', () => {
- const result = timeUtils.checkValidTimestamp('10000-00-00T00:00:00.000Z');
- expect(result).toBeFalsy();
- });
- });
-});
diff --git a/server/libs/common/src/utils/time-utils.ts b/server/libs/common/src/utils/time-utils.ts
deleted file mode 100644
index 1f75f5c29d..0000000000
--- a/server/libs/common/src/utils/time-utils.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-function createTimeUtils() {
- const checkValidTimestamp = (timestamp: string): boolean => {
- const parsedTimestamp = Date.parse(timestamp);
-
- if (isNaN(parsedTimestamp)) {
- return false;
- }
-
- const date = new Date(parsedTimestamp);
-
- if (date.getFullYear() < 1583 || date.getFullYear() > 9999) {
- return false;
- }
-
- return date.getFullYear() > 0;
- };
-
- return { checkValidTimestamp };
-}
-
-export const timeUtils = createTimeUtils();
diff --git a/server/libs/common/tsconfig.lib.json b/server/libs/common/tsconfig.lib.json
deleted file mode 100644
index 8fdbf52b4a..0000000000
--- a/server/libs/common/tsconfig.lib.json
+++ /dev/null
@@ -1,9 +0,0 @@
-{
- "extends": "../../tsconfig.json",
- "compilerOptions": {
- "declaration": true,
- "outDir": "../../dist/libs/common"
- },
- "include": ["src/**/*"],
- "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
-}
diff --git a/server/libs/common/src/config/app.config.ts b/server/libs/domain/src/domain.config.ts
similarity index 72%
rename from server/libs/common/src/config/app.config.ts
rename to server/libs/domain/src/domain.config.ts
index a32d90f9f2..bb26f4bcc7 100644
--- a/server/libs/common/src/config/app.config.ts
+++ b/server/libs/domain/src/domain.config.ts
@@ -1,3 +1,5 @@
+// TODO: remove nestjs references from domain
+import { LogLevel } from '@nestjs/common';
import { ConfigModuleOptions } from '@nestjs/config';
import Joi from 'joi';
@@ -29,3 +31,13 @@ export const immichAppConfig: ConfigModuleOptions = {
SERVER_PORT: Joi.number().optional(),
}),
};
+
+export function getLogLevels() {
+ const LOG_LEVELS: LogLevel[] = ['verbose', 'debug', 'log', 'warn', 'error'];
+ let logLevel = process.env.LOG_LEVEL || 'log';
+ if (logLevel === 'simple') {
+ logLevel = 'log';
+ }
+ const logLevelIndex = LOG_LEVELS.indexOf(logLevel as LogLevel);
+ return logLevelIndex === -1 ? [] : LOG_LEVELS.slice(logLevelIndex);
+}
diff --git a/server/libs/domain/src/domain.constant.ts b/server/libs/domain/src/domain.constant.ts
index afe62a17b8..2dd0c1e578 100644
--- a/server/libs/domain/src/domain.constant.ts
+++ b/server/libs/domain/src/domain.constant.ts
@@ -1,3 +1,4 @@
+import { BadRequestException } from '@nestjs/common';
import pkg from '../../../package.json';
const [major, minor, patch] = pkg.version.split('.');
@@ -17,3 +18,12 @@ export const serverVersion: IServerVersion = {
export const SERVER_VERSION = `${serverVersion.major}.${serverVersion.minor}.${serverVersion.patch}`;
export const APP_UPLOAD_LOCATION = './upload';
+
+export const MACHINE_LEARNING_URL = process.env.IMMICH_MACHINE_LEARNING_URL || 'http://immich-machine-learning:3003';
+export const MACHINE_LEARNING_ENABLED = MACHINE_LEARNING_URL !== 'false';
+
+export function assertMachineLearningEnabled() {
+ if (!MACHINE_LEARNING_ENABLED) {
+ throw new BadRequestException('Machine learning is not enabled.');
+ }
+}
diff --git a/server/libs/domain/src/index.ts b/server/libs/domain/src/index.ts
index a73ad0e280..2008d1a89a 100644
--- a/server/libs/domain/src/index.ts
+++ b/server/libs/domain/src/index.ts
@@ -5,6 +5,7 @@ export * from './auth';
export * from './communication';
export * from './crypto';
export * from './device-info';
+export * from './domain.config';
export * from './domain.constant';
export * from './domain.module';
export * from './domain.util';
diff --git a/server/libs/domain/src/job/job.service.ts b/server/libs/domain/src/job/job.service.ts
index fd89573ec6..2ca036280a 100644
--- a/server/libs/domain/src/job/job.service.ts
+++ b/server/libs/domain/src/job/job.service.ts
@@ -1,5 +1,5 @@
-import { assertMachineLearningEnabled } from '@app/common';
import { BadRequestException, Inject, Injectable, Logger } from '@nestjs/common';
+import { assertMachineLearningEnabled } from '../domain.constant';
import { JobCommandDto } from './dto';
import { JobCommand, JobName, QueueName } from './job.constants';
import { IJobRepository } from './job.repository';
diff --git a/server/libs/domain/src/search/search.service.ts b/server/libs/domain/src/search/search.service.ts
index fbf7083ed8..744657f375 100644
--- a/server/libs/domain/src/search/search.service.ts
+++ b/server/libs/domain/src/search/search.service.ts
@@ -1,4 +1,3 @@
-import { MACHINE_LEARNING_ENABLED } from '@app/common';
import { AlbumEntity, AssetEntity } from '@app/infra/db/entities';
import { BadRequestException, Inject, Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@@ -7,6 +6,7 @@ import { IAlbumRepository } from '../album/album.repository';
import { mapAsset } from '../asset';
import { IAssetRepository } from '../asset/asset.repository';
import { AuthUserDto } from '../auth';
+import { MACHINE_LEARNING_ENABLED } from '../domain.constant';
import { IBulkEntityJob, IJobRepository, JobName } from '../job';
import { IMachineLearningRepository } from '../smart-info';
import { SearchDto } from './dto';
diff --git a/server/libs/domain/src/smart-info/smart-info.service.ts b/server/libs/domain/src/smart-info/smart-info.service.ts
index 8e7cd014ab..dddfc26609 100644
--- a/server/libs/domain/src/smart-info/smart-info.service.ts
+++ b/server/libs/domain/src/smart-info/smart-info.service.ts
@@ -1,6 +1,6 @@
-import { MACHINE_LEARNING_ENABLED } from '@app/common';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { IAssetRepository, WithoutProperty } from '../asset';
+import { MACHINE_LEARNING_ENABLED } from '../domain.constant';
import { IAssetJob, IBaseJob, IJobRepository, JobName } from '../job';
import { IMachineLearningRepository } from './machine-learning.interface';
import { ISmartInfoRepository } from './smart-info.repository';
diff --git a/server/libs/infra/src/machine-learning/machine-learning.repository.ts b/server/libs/infra/src/machine-learning/machine-learning.repository.ts
index 78fc809779..7fe90db99d 100644
--- a/server/libs/infra/src/machine-learning/machine-learning.repository.ts
+++ b/server/libs/infra/src/machine-learning/machine-learning.repository.ts
@@ -1,5 +1,4 @@
-import { MACHINE_LEARNING_URL } from '@app/common';
-import { IMachineLearningRepository, MachineLearningInput } from '@app/domain';
+import { IMachineLearningRepository, MachineLearningInput, MACHINE_LEARNING_URL } from '@app/domain';
import { Injectable } from '@nestjs/common';
import axios from 'axios';
diff --git a/server/package.json b/server/package.json
index 2064417e8c..ef16490881 100644
--- a/server/package.json
+++ b/server/package.json
@@ -153,7 +153,6 @@
"<rootDir>/libs/"
],
"moduleNameMapper": {
- "@app/common": "<rootDir>/libs/common/src",
"^@app/infra(|/.*)$": "<rootDir>/libs/infra/src/$1",
"^@app/domain(|/.*)$": "<rootDir>/libs/domain/src/$1"
},
diff --git a/server/tsconfig.json b/server/tsconfig.json
index 3647f64a0d..ede4fd120e 100644
--- a/server/tsconfig.json
+++ b/server/tsconfig.json
@@ -16,8 +16,6 @@
"esModuleInterop": true,
"baseUrl": "./",
"paths": {
- "@app/common": ["libs/common/src"],
- "@app/common/*": ["libs/common/src/*"],
"@app/infra": ["libs/infra/src"],
"@app/infra/*": ["libs/infra/src/*"],
"@app/domain": ["libs/domain/src"],