mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-12-08 20:29:05 +00:00
feat(web,server): user storage label (#2418)
* feat: user storage label * chore: open api * fix: checks * fix: api update validation and tests * feat: default admin storage label * fix: linting * fix: user create/update dto * fix: delete library with custom label
This commit is contained in:
@@ -4,7 +4,7 @@ import handlebar from 'handlebars';
|
||||
import * as luxon from 'luxon';
|
||||
import path from 'node:path';
|
||||
import sanitize from 'sanitize-filename';
|
||||
import { IStorageRepository, StorageCore, StorageFolder } from '../storage';
|
||||
import { IStorageRepository, StorageCore } from '../storage';
|
||||
import {
|
||||
ISystemConfigRepository,
|
||||
supportedDayTokens,
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
supportedYearTokens,
|
||||
} from '../system-config';
|
||||
import { SystemConfigCore } from '../system-config/system-config.core';
|
||||
import { MoveAssetMetadata } from './storage-template.service';
|
||||
|
||||
export class StorageTemplateCore {
|
||||
private logger = new Logger(StorageTemplateCore.name);
|
||||
@@ -33,12 +34,14 @@ export class StorageTemplateCore {
|
||||
this.configCore.config$.subscribe((config) => this.onConfig(config));
|
||||
}
|
||||
|
||||
public async getTemplatePath(asset: AssetEntity, filename: string): Promise<string> {
|
||||
public async getTemplatePath(asset: AssetEntity, metadata: MoveAssetMetadata): Promise<string> {
|
||||
const { storageLabel, filename } = metadata;
|
||||
|
||||
try {
|
||||
const source = asset.originalPath;
|
||||
const ext = path.extname(source).split('.').pop() as string;
|
||||
const sanitized = sanitize(path.basename(filename, `.${ext}`));
|
||||
const rootPath = this.storageCore.getFolderLocation(StorageFolder.LIBRARY, asset.ownerId);
|
||||
const rootPath = this.storageCore.getLibraryFolder({ id: asset.ownerId, storageLabel });
|
||||
const storagePath = this.render(this.storageTemplate, asset, sanitized, ext);
|
||||
const fullPath = path.normalize(path.join(rootPath, storagePath));
|
||||
let destination = `${fullPath}.${ext}`;
|
||||
|
||||
@@ -4,18 +4,22 @@ import {
|
||||
newAssetRepositoryMock,
|
||||
newStorageRepositoryMock,
|
||||
newSystemConfigRepositoryMock,
|
||||
newUserRepositoryMock,
|
||||
systemConfigStub,
|
||||
userEntityStub,
|
||||
} from '../../test';
|
||||
import { IAssetRepository } from '../asset';
|
||||
import { StorageTemplateService } from '../storage-template';
|
||||
import { IStorageRepository } from '../storage/storage.repository';
|
||||
import { ISystemConfigRepository } from '../system-config';
|
||||
import { IUserRepository } from '../user';
|
||||
|
||||
describe(StorageTemplateService.name, () => {
|
||||
let sut: StorageTemplateService;
|
||||
let assetMock: jest.Mocked<IAssetRepository>;
|
||||
let configMock: jest.Mocked<ISystemConfigRepository>;
|
||||
let storageMock: jest.Mocked<IStorageRepository>;
|
||||
let userMock: jest.Mocked<IUserRepository>;
|
||||
|
||||
it('should work', () => {
|
||||
expect(sut).toBeDefined();
|
||||
@@ -25,12 +29,15 @@ describe(StorageTemplateService.name, () => {
|
||||
assetMock = newAssetRepositoryMock();
|
||||
configMock = newSystemConfigRepositoryMock();
|
||||
storageMock = newStorageRepositoryMock();
|
||||
sut = new StorageTemplateService(assetMock, configMock, systemConfigStub.defaults, storageMock);
|
||||
userMock = newUserRepositoryMock();
|
||||
|
||||
sut = new StorageTemplateService(assetMock, configMock, systemConfigStub.defaults, storageMock, userMock);
|
||||
});
|
||||
|
||||
describe('handle template migration', () => {
|
||||
it('should handle no assets', async () => {
|
||||
assetMock.getAll.mockResolvedValue([]);
|
||||
userMock.getList.mockResolvedValue([]);
|
||||
|
||||
await sut.handleTemplateMigration();
|
||||
|
||||
@@ -40,6 +47,7 @@ describe(StorageTemplateService.name, () => {
|
||||
it('should handle an asset with a duplicate destination', async () => {
|
||||
assetMock.getAll.mockResolvedValue([assetEntityStub.image]);
|
||||
assetMock.save.mockResolvedValue(assetEntityStub.image);
|
||||
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
||||
|
||||
when(storageMock.checkFileExists)
|
||||
.calledWith('upload/library/user-id/2023/2023-02-23/asset-id.ext')
|
||||
@@ -57,6 +65,7 @@ describe(StorageTemplateService.name, () => {
|
||||
id: assetEntityStub.image.id,
|
||||
originalPath: 'upload/library/user-id/2023/2023-02-23/asset-id+1.ext',
|
||||
});
|
||||
expect(userMock.getList).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should skip when an asset already matches the template', async () => {
|
||||
@@ -66,6 +75,7 @@ describe(StorageTemplateService.name, () => {
|
||||
originalPath: 'upload/library/user-id/2023/2023-02-23/asset-id.ext',
|
||||
},
|
||||
]);
|
||||
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
||||
|
||||
await sut.handleTemplateMigration();
|
||||
|
||||
@@ -82,6 +92,7 @@ describe(StorageTemplateService.name, () => {
|
||||
originalPath: 'upload/library/user-id/2023/2023-02-23/asset-id+1.ext',
|
||||
},
|
||||
]);
|
||||
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
||||
|
||||
await sut.handleTemplateMigration();
|
||||
|
||||
@@ -94,6 +105,7 @@ describe(StorageTemplateService.name, () => {
|
||||
it('should move an asset', async () => {
|
||||
assetMock.getAll.mockResolvedValue([assetEntityStub.image]);
|
||||
assetMock.save.mockResolvedValue(assetEntityStub.image);
|
||||
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
||||
|
||||
await sut.handleTemplateMigration();
|
||||
|
||||
@@ -108,9 +120,28 @@ describe(StorageTemplateService.name, () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should use the user storage label', async () => {
|
||||
assetMock.getAll.mockResolvedValue([assetEntityStub.image]);
|
||||
assetMock.save.mockResolvedValue(assetEntityStub.image);
|
||||
userMock.getList.mockResolvedValue([userEntityStub.storageLabel]);
|
||||
|
||||
await sut.handleTemplateMigration();
|
||||
|
||||
expect(assetMock.getAll).toHaveBeenCalled();
|
||||
expect(storageMock.moveFile).toHaveBeenCalledWith(
|
||||
'/original/path.ext',
|
||||
'upload/library/label-1/2023/2023-02-23/asset-id.ext',
|
||||
);
|
||||
expect(assetMock.save).toHaveBeenCalledWith({
|
||||
id: assetEntityStub.image.id,
|
||||
originalPath: 'upload/library/label-1/2023/2023-02-23/asset-id.ext',
|
||||
});
|
||||
});
|
||||
|
||||
it('should not update the database if the move fails', async () => {
|
||||
assetMock.getAll.mockResolvedValue([assetEntityStub.image]);
|
||||
storageMock.moveFile.mockRejectedValue(new Error('Read only system'));
|
||||
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
||||
|
||||
await sut.handleTemplateMigration();
|
||||
|
||||
@@ -125,6 +156,7 @@ describe(StorageTemplateService.name, () => {
|
||||
it('should move the asset back if the database fails', async () => {
|
||||
assetMock.getAll.mockResolvedValue([assetEntityStub.image]);
|
||||
assetMock.save.mockRejectedValue('Connection Error!');
|
||||
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
||||
|
||||
await sut.handleTemplateMigration();
|
||||
|
||||
@@ -143,6 +175,7 @@ describe(StorageTemplateService.name, () => {
|
||||
it('should handle an error', async () => {
|
||||
assetMock.getAll.mockResolvedValue([]);
|
||||
storageMock.removeEmptyDirs.mockRejectedValue(new Error('Read only filesystem'));
|
||||
userMock.getList.mockResolvedValue([]);
|
||||
|
||||
await sut.handleTemplateMigration();
|
||||
});
|
||||
|
||||
@@ -6,8 +6,14 @@ import { getLivePhotoMotionFilename } from '../domain.util';
|
||||
import { IAssetJob } from '../job';
|
||||
import { IStorageRepository } from '../storage/storage.repository';
|
||||
import { INITIAL_SYSTEM_CONFIG, ISystemConfigRepository } from '../system-config';
|
||||
import { IUserRepository } from '../user/user.repository';
|
||||
import { StorageTemplateCore } from './storage-template.core';
|
||||
|
||||
export interface MoveAssetMetadata {
|
||||
storageLabel: string | null;
|
||||
filename: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class StorageTemplateService {
|
||||
private logger = new Logger(StorageTemplateService.name);
|
||||
@@ -18,6 +24,7 @@ export class StorageTemplateService {
|
||||
@Inject(ISystemConfigRepository) configRepository: ISystemConfigRepository,
|
||||
@Inject(INITIAL_SYSTEM_CONFIG) config: SystemConfig,
|
||||
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
|
||||
@Inject(IUserRepository) private userRepository: IUserRepository,
|
||||
) {
|
||||
this.core = new StorageTemplateCore(configRepository, config, storageRepository);
|
||||
}
|
||||
@@ -26,14 +33,16 @@ export class StorageTemplateService {
|
||||
const { asset } = data;
|
||||
|
||||
try {
|
||||
const user = await this.userRepository.get(asset.ownerId);
|
||||
const storageLabel = user?.storageLabel || null;
|
||||
const filename = asset.originalFileName || asset.id;
|
||||
await this.moveAsset(asset, filename);
|
||||
await this.moveAsset(asset, { storageLabel, filename });
|
||||
|
||||
// move motion part of live photo
|
||||
if (asset.livePhotoVideoId) {
|
||||
const [livePhotoVideo] = await this.assetRepository.getByIds([asset.livePhotoVideoId]);
|
||||
const motionFilename = getLivePhotoMotionFilename(filename, livePhotoVideo.originalPath);
|
||||
await this.moveAsset(livePhotoVideo, motionFilename);
|
||||
await this.moveAsset(livePhotoVideo, { storageLabel, filename: motionFilename });
|
||||
}
|
||||
} catch (error: any) {
|
||||
this.logger.error('Error running single template migration', error);
|
||||
@@ -44,6 +53,7 @@ export class StorageTemplateService {
|
||||
try {
|
||||
console.time('migrating-time');
|
||||
const assets = await this.assetRepository.getAll();
|
||||
const users = await this.userRepository.getList();
|
||||
|
||||
const livePhotoMap: Record<string, AssetEntity> = {};
|
||||
|
||||
@@ -56,8 +66,10 @@ export class StorageTemplateService {
|
||||
for (const asset of assets) {
|
||||
const livePhotoParentAsset = livePhotoMap[asset.id];
|
||||
// TODO: remove livePhoto specific stuff once upload is fixed
|
||||
const user = users.find((user) => user.id === asset.ownerId);
|
||||
const storageLabel = user?.storageLabel || null;
|
||||
const filename = asset.originalFileName || livePhotoParentAsset?.originalFileName || asset.id;
|
||||
await this.moveAsset(asset, filename);
|
||||
await this.moveAsset(asset, { storageLabel, filename });
|
||||
}
|
||||
|
||||
this.logger.debug('Cleaning up empty directories...');
|
||||
@@ -70,8 +82,8 @@ export class StorageTemplateService {
|
||||
}
|
||||
|
||||
// TODO: use asset core (once in domain)
|
||||
async moveAsset(asset: AssetEntity, originalName: string) {
|
||||
const destination = await this.core.getTemplatePath(asset, originalName);
|
||||
async moveAsset(asset: AssetEntity, metadata: MoveAssetMetadata) {
|
||||
const destination = await this.core.getTemplatePath(asset, metadata);
|
||||
if (asset.originalPath !== destination) {
|
||||
const source = asset.originalPath;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user