2024-03-21 08:07:47 -05:00
|
|
|
import { CreateAssetDto } from 'src/dtos/asset-v1.dto';
|
2024-03-20 16:02:51 -05:00
|
|
|
import { ASSET_CHECKSUM_CONSTRAINT, AssetEntity, AssetType } from 'src/entities/asset.entity';
|
|
|
|
|
import { ExifEntity } from 'src/entities/exif.entity';
|
2024-03-21 08:07:47 -05:00
|
|
|
import { IAssetRepositoryV1 } from 'src/interfaces/asset-v1.interface';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IAssetRepository } from 'src/interfaces/asset.interface';
|
|
|
|
|
import { IJobRepository, JobName } from 'src/interfaces/job.interface';
|
|
|
|
|
import { ILibraryRepository } from 'src/interfaces/library.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';
|
|
|
|
|
import { IUserRepository } from 'src/interfaces/user.interface';
|
2024-03-21 08:07:47 -05:00
|
|
|
import { AssetServiceV1 } from 'src/services/asset-v1.service';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { assetStub } from 'test/fixtures/asset.stub';
|
|
|
|
|
import { authStub } from 'test/fixtures/auth.stub';
|
|
|
|
|
import { fileStub } from 'test/fixtures/file.stub';
|
|
|
|
|
import { IAccessRepositoryMock, newAccessRepositoryMock } from 'test/repositories/access.repository.mock';
|
|
|
|
|
import { newAssetRepositoryMock } from 'test/repositories/asset.repository.mock';
|
|
|
|
|
import { newJobRepositoryMock } from 'test/repositories/job.repository.mock';
|
|
|
|
|
import { newLibraryRepositoryMock } from 'test/repositories/library.repository.mock';
|
2024-04-17 03:00:31 +05:30
|
|
|
import { newLoggerRepositoryMock } from 'test/repositories/logger.repository.mock';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { newStorageRepositoryMock } from 'test/repositories/storage.repository.mock';
|
|
|
|
|
import { newUserRepositoryMock } from 'test/repositories/user.repository.mock';
|
2023-11-16 23:24:31 -05:00
|
|
|
import { QueryFailedError } from 'typeorm';
|
2024-04-16 10:44:45 -04:00
|
|
|
import { Mocked, vitest } from 'vitest';
|
2022-08-26 22:53:37 -07:00
|
|
|
|
2023-01-30 11:14:13 -05:00
|
|
|
const _getCreateAssetDto = (): CreateAssetDto => {
|
|
|
|
|
const createAssetDto = new CreateAssetDto();
|
|
|
|
|
createAssetDto.deviceAssetId = 'deviceAssetId';
|
|
|
|
|
createAssetDto.deviceId = 'deviceId';
|
2023-05-29 16:05:14 +02:00
|
|
|
createAssetDto.fileCreatedAt = new Date('2022-06-19T23:41:36.910Z');
|
|
|
|
|
createAssetDto.fileModifiedAt = new Date('2022-06-19T23:41:36.910Z');
|
2023-01-30 11:14:13 -05:00
|
|
|
createAssetDto.isFavorite = false;
|
2023-04-12 18:37:52 +03:00
|
|
|
createAssetDto.isArchived = false;
|
2023-01-30 11:14:13 -05:00
|
|
|
createAssetDto.duration = '0:00:00.000000';
|
|
|
|
|
|
|
|
|
|
return createAssetDto;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const _getAsset_1 = () => {
|
|
|
|
|
const asset_1 = new AssetEntity();
|
|
|
|
|
|
|
|
|
|
asset_1.id = 'id_1';
|
2023-02-19 16:44:53 +00:00
|
|
|
asset_1.ownerId = 'user_id_1';
|
2023-01-30 11:14:13 -05:00
|
|
|
asset_1.deviceAssetId = 'device_asset_id_1';
|
|
|
|
|
asset_1.deviceId = 'device_id_1';
|
|
|
|
|
asset_1.type = AssetType.VIDEO;
|
|
|
|
|
asset_1.originalPath = 'fake_path/asset_1.jpeg';
|
2024-04-02 00:56:56 -04:00
|
|
|
asset_1.previewPath = '';
|
2023-05-29 16:05:14 +02:00
|
|
|
asset_1.fileModifiedAt = new Date('2022-06-19T23:41:36.910Z');
|
|
|
|
|
asset_1.fileCreatedAt = new Date('2022-06-19T23:41:36.910Z');
|
|
|
|
|
asset_1.updatedAt = new Date('2022-06-19T23:41:36.910Z');
|
2023-01-30 11:14:13 -05:00
|
|
|
asset_1.isFavorite = false;
|
2023-04-12 18:37:52 +03:00
|
|
|
asset_1.isArchived = false;
|
2024-04-02 00:56:56 -04:00
|
|
|
asset_1.thumbnailPath = '';
|
2023-01-30 11:14:13 -05:00
|
|
|
asset_1.encodedVideoPath = '';
|
|
|
|
|
asset_1.duration = '0:00:00.000000';
|
2023-05-06 03:33:30 +02:00
|
|
|
asset_1.exifInfo = new ExifEntity();
|
2024-02-02 04:18:00 +01:00
|
|
|
asset_1.exifInfo.latitude = 49.533_547;
|
|
|
|
|
asset_1.exifInfo.longitude = 10.703_075;
|
2023-01-30 11:14:13 -05:00
|
|
|
return asset_1;
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-26 22:53:37 -07:00
|
|
|
describe('AssetService', () => {
|
2024-03-21 08:07:47 -05:00
|
|
|
let sut: AssetServiceV1;
|
2023-06-28 09:56:24 -04:00
|
|
|
let accessMock: IAccessRepositoryMock;
|
2024-04-16 10:44:45 -04:00
|
|
|
let assetRepositoryMockV1: Mocked<IAssetRepositoryV1>;
|
|
|
|
|
let assetMock: Mocked<IAssetRepository>;
|
|
|
|
|
let jobMock: Mocked<IJobRepository>;
|
|
|
|
|
let libraryMock: Mocked<ILibraryRepository>;
|
2024-04-17 03:00:31 +05:30
|
|
|
let loggerMock: Mocked<ILoggerRepository>;
|
2024-04-16 10:44:45 -04:00
|
|
|
let storageMock: Mocked<IStorageRepository>;
|
|
|
|
|
let userMock: Mocked<IUserRepository>;
|
2022-08-26 22:53:37 -07:00
|
|
|
|
2023-01-30 11:14:13 -05:00
|
|
|
beforeEach(() => {
|
2024-01-25 12:52:21 -05:00
|
|
|
assetRepositoryMockV1 = {
|
2024-04-16 10:44:45 -04:00
|
|
|
get: vitest.fn(),
|
|
|
|
|
getAssetsByChecksums: vitest.fn(),
|
2022-08-26 22:53:37 -07:00
|
|
|
};
|
|
|
|
|
|
2023-06-06 16:18:38 -04:00
|
|
|
accessMock = newAccessRepositoryMock();
|
2024-01-25 12:52:21 -05:00
|
|
|
assetMock = newAssetRepositoryMock();
|
2023-01-21 23:13:36 -05:00
|
|
|
jobMock = newJobRepositoryMock();
|
2023-09-20 13:16:33 +02:00
|
|
|
libraryMock = newLibraryRepositoryMock();
|
2024-04-17 03:00:31 +05:30
|
|
|
loggerMock = newLoggerRepositoryMock();
|
2024-02-11 21:40:34 -07:00
|
|
|
storageMock = newStorageRepositoryMock();
|
2024-01-12 18:43:36 -06:00
|
|
|
userMock = newUserRepositoryMock();
|
2023-01-21 23:13:36 -05:00
|
|
|
|
2024-04-17 03:00:31 +05:30
|
|
|
sut = new AssetServiceV1(
|
|
|
|
|
accessMock,
|
|
|
|
|
assetRepositoryMockV1,
|
|
|
|
|
assetMock,
|
|
|
|
|
jobMock,
|
|
|
|
|
libraryMock,
|
|
|
|
|
storageMock,
|
|
|
|
|
userMock,
|
|
|
|
|
loggerMock,
|
|
|
|
|
);
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2024-04-16 10:44:45 -04:00
|
|
|
assetRepositoryMockV1.get.mockImplementation((assetId) =>
|
|
|
|
|
Promise.resolve(
|
|
|
|
|
[assetStub.livePhotoMotionAsset, assetStub.livePhotoMotionAsset].find((asset) => asset.id === assetId) ?? null,
|
|
|
|
|
),
|
|
|
|
|
);
|
2022-08-26 22:53:37 -07:00
|
|
|
});
|
|
|
|
|
|
2023-01-30 11:14:13 -05:00
|
|
|
describe('uploadFile', () => {
|
|
|
|
|
it('should handle a file upload', async () => {
|
|
|
|
|
const assetEntity = _getAsset_1();
|
|
|
|
|
const file = {
|
2024-01-04 20:45:16 +00:00
|
|
|
uuid: 'random-uuid',
|
2023-01-30 11:14:13 -05:00
|
|
|
originalPath: 'fake_path/asset_1.jpeg',
|
|
|
|
|
mimeType: 'image/jpeg',
|
|
|
|
|
checksum: Buffer.from('file hash', 'utf8'),
|
|
|
|
|
originalName: 'asset_1.jpeg',
|
2024-01-12 18:43:36 -06:00
|
|
|
size: 42,
|
2023-01-30 11:14:13 -05:00
|
|
|
};
|
|
|
|
|
const dto = _getCreateAssetDto();
|
|
|
|
|
|
2024-02-08 16:56:06 -05:00
|
|
|
assetMock.create.mockResolvedValue(assetEntity);
|
2023-01-30 11:14:13 -05:00
|
|
|
|
|
|
|
|
await expect(sut.uploadFile(authStub.user1, dto, file)).resolves.toEqual({ duplicate: false, id: 'id_1' });
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2024-02-08 16:56:06 -05:00
|
|
|
expect(assetMock.create).toHaveBeenCalled();
|
2024-01-12 18:43:36 -06:00
|
|
|
expect(userMock.updateUsage).toHaveBeenCalledWith(authStub.user1.user.id, file.size);
|
2024-02-11 21:40:34 -07:00
|
|
|
expect(storageMock.utimes).toHaveBeenCalledWith(
|
|
|
|
|
file.originalPath,
|
|
|
|
|
expect.any(Date),
|
|
|
|
|
new Date(dto.fileModifiedAt),
|
|
|
|
|
);
|
2023-01-30 11:14:13 -05:00
|
|
|
});
|
2022-08-26 22:53:37 -07:00
|
|
|
|
2023-01-30 11:14:13 -05:00
|
|
|
it('should handle a duplicate', async () => {
|
|
|
|
|
const file = {
|
2024-01-04 20:45:16 +00:00
|
|
|
uuid: 'random-uuid',
|
2023-01-30 11:14:13 -05:00
|
|
|
originalPath: 'fake_path/asset_1.jpeg',
|
|
|
|
|
mimeType: 'image/jpeg',
|
|
|
|
|
checksum: Buffer.from('file hash', 'utf8'),
|
|
|
|
|
originalName: 'asset_1.jpeg',
|
2024-01-12 18:43:36 -06:00
|
|
|
size: 0,
|
2023-01-30 11:14:13 -05:00
|
|
|
};
|
|
|
|
|
const dto = _getCreateAssetDto();
|
2024-01-17 18:24:51 +00:00
|
|
|
const error = new QueryFailedError('', [], new Error('unique key violation'));
|
2023-09-20 13:16:33 +02:00
|
|
|
(error as any).constraint = ASSET_CHECKSUM_CONSTRAINT;
|
2023-01-30 11:14:13 -05:00
|
|
|
|
2024-02-08 16:56:06 -05:00
|
|
|
assetMock.create.mockRejectedValue(error);
|
2024-01-25 12:52:21 -05:00
|
|
|
assetRepositoryMockV1.getAssetsByChecksums.mockResolvedValue([_getAsset_1()]);
|
2023-01-30 11:14:13 -05:00
|
|
|
|
|
|
|
|
await expect(sut.uploadFile(authStub.user1, dto, file)).resolves.toEqual({ duplicate: true, id: 'id_1' });
|
|
|
|
|
|
2023-02-25 09:12:03 -05:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
|
name: JobName.DELETE_FILES,
|
feat(server): xmp sidecar metadata (#2466)
* initial commit for XMP sidecar support
* Added support for 'missing' metadata files to include those without sidecar files, now detects sidecar files in the filesystem for media already ingested but the sidecar was created afterwards
* didn't mean to commit default log level during testing
* new sidecar logic for video metadata as well
* Added xml mimetype for sidecars only
* don't need capture group for this regex
* wrong default value reverted
* simplified the move here - keep it in the same try catch since the outcome is to move the media back anyway
* simplified setter logic
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
* simplified logic per suggestions
* sidecar is now its own queue with a discover and sync, updated UI for the new job queueing
* queue a sidecar job for every asset based on discovery or sync, though the logic is almost identical aside from linking the sidecar
* now queue sidecar jobs for each assset, though logic is mostly the same between discovery and sync
* simplified logic of filename extraction and asset instantiation
* not sure how that got deleted..
* updated code per suggestions and comments in the PR
* stat was not being used, removed the variable set
* better type checking, using in-scope variables for exif getter instead of passing in every time
* removed commented out test
* ran and resolved all lints, formats, checks, and tests
* resolved suggested change in PR
* made getExifProperty more dynamic with multiple possible args for fallbacks, fixed typo, used generic in function for better type checking
* better error handling and moving files back to positions on move or save failure
* regenerated api
* format fixes
* Added XMP documentation
* documentation typo
* Merged in main
* missed merge conflict
* more changes due to a merge
* Resolving conflicts
* added icon for sidecar jobs
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-05-24 21:59:30 -04:00
|
|
|
data: { files: ['fake_path/asset_1.jpeg', undefined, undefined] },
|
2023-01-30 11:14:13 -05:00
|
|
|
});
|
2024-01-12 18:43:36 -06:00
|
|
|
expect(userMock.updateUsage).not.toHaveBeenCalled();
|
2023-01-30 11:14:13 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle a live photo', async () => {
|
|
|
|
|
const dto = _getCreateAssetDto();
|
2024-01-17 18:24:51 +00:00
|
|
|
const error = new QueryFailedError('', [], new Error('unique key violation'));
|
2023-09-20 13:16:33 +02:00
|
|
|
(error as any).constraint = ASSET_CHECKSUM_CONSTRAINT;
|
2023-01-30 11:14:13 -05:00
|
|
|
|
2024-02-08 16:56:06 -05:00
|
|
|
assetMock.create.mockResolvedValueOnce(assetStub.livePhotoMotionAsset);
|
|
|
|
|
assetMock.create.mockResolvedValueOnce(assetStub.livePhotoStillAsset);
|
2023-01-30 11:14:13 -05:00
|
|
|
|
2023-02-25 09:12:03 -05:00
|
|
|
await expect(
|
|
|
|
|
sut.uploadFile(authStub.user1, dto, fileStub.livePhotoStill, fileStub.livePhotoMotion),
|
|
|
|
|
).resolves.toEqual({
|
2023-01-30 11:14:13 -05:00
|
|
|
duplicate: false,
|
2023-02-25 09:12:03 -05:00
|
|
|
id: 'live-photo-still-asset',
|
2023-01-30 11:14:13 -05:00
|
|
|
});
|
|
|
|
|
|
2023-02-25 09:12:03 -05:00
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
2023-05-27 16:49:57 -05:00
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: JobName.METADATA_EXTRACTION,
|
2023-07-31 21:28:07 -04:00
|
|
|
data: { id: assetStub.livePhotoMotionAsset.id, source: 'upload' },
|
2023-05-27 16:49:57 -05:00
|
|
|
},
|
|
|
|
|
],
|
2023-07-31 21:28:07 -04:00
|
|
|
[{ name: JobName.METADATA_EXTRACTION, data: { id: assetStub.livePhotoStillAsset.id, source: 'upload' } }],
|
2023-01-30 11:14:13 -05:00
|
|
|
]);
|
2024-01-12 18:43:36 -06:00
|
|
|
expect(userMock.updateUsage).toHaveBeenCalledWith(authStub.user1.user.id, 111);
|
2024-02-11 21:40:34 -07:00
|
|
|
expect(storageMock.utimes).toHaveBeenCalledWith(
|
|
|
|
|
fileStub.livePhotoStill.originalPath,
|
|
|
|
|
expect.any(Date),
|
|
|
|
|
new Date(dto.fileModifiedAt),
|
|
|
|
|
);
|
|
|
|
|
expect(storageMock.utimes).toHaveBeenCalledWith(
|
|
|
|
|
fileStub.livePhotoMotion.originalPath,
|
|
|
|
|
expect.any(Date),
|
|
|
|
|
new Date(dto.fileModifiedAt),
|
|
|
|
|
);
|
2023-01-30 11:14:13 -05:00
|
|
|
});
|
2022-09-16 16:47:45 -05:00
|
|
|
});
|
2022-08-26 22:53:37 -07:00
|
|
|
});
|