2023-05-26 21:43:24 +02:00
|
|
|
import { Inject } from '@nestjs/common';
|
2023-05-26 14:52:52 +02:00
|
|
|
import { constants } from 'fs/promises';
|
2023-05-26 21:43:24 +02:00
|
|
|
import { IAssetRepository, WithoutProperty, WithProperty } from '../asset';
|
2023-05-26 14:52:52 +02:00
|
|
|
import { usePagination } from '../domain.util';
|
2023-05-26 21:43:24 +02:00
|
|
|
import { IBaseJob, IEntityJob, IJobRepository, JobName, JOBS_ASSET_PAGINATION_SIZE } from '../job';
|
2023-05-26 14:52:52 +02:00
|
|
|
import { IStorageRepository } from '../storage';
|
|
|
|
|
|
|
|
export class MetadataService {
|
|
|
|
constructor(
|
|
|
|
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
|
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
|
|
|
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
|
2023-05-26 21:43:24 +02:00
|
|
|
) {}
|
2023-05-26 14:52:52 +02:00
|
|
|
|
|
|
|
async handleQueueSidecar(job: IBaseJob) {
|
2023-05-26 21:43:24 +02:00
|
|
|
const { force } = job;
|
|
|
|
const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) => {
|
|
|
|
return force
|
|
|
|
? this.assetRepository.getWith(pagination, WithProperty.SIDECAR)
|
|
|
|
: this.assetRepository.getWithout(pagination, WithoutProperty.SIDECAR);
|
|
|
|
});
|
|
|
|
|
|
|
|
for await (const assets of assetPagination) {
|
|
|
|
for (const asset of assets) {
|
|
|
|
const name = force ? JobName.SIDECAR_SYNC : JobName.SIDECAR_DISCOVERY;
|
|
|
|
await this.jobRepository.queue({ name, data: { id: asset.id } });
|
2023-05-26 14:52:52 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-26 21:43:24 +02:00
|
|
|
|
|
|
|
return true;
|
2023-05-26 14:52:52 +02:00
|
|
|
}
|
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
async handleSidecarSync() {
|
|
|
|
// TODO: optimize to only queue assets with recent xmp changes
|
|
|
|
return true;
|
|
|
|
}
|
2023-05-26 14:52:52 +02:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
async handleSidecarDiscovery({ id }: IEntityJob) {
|
|
|
|
const [asset] = await this.assetRepository.getByIds([id]);
|
|
|
|
if (!asset || !asset.isVisible || asset.sidecarPath) {
|
|
|
|
return false;
|
2023-05-26 14:52:52 +02:00
|
|
|
}
|
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
const sidecarPath = `${asset.originalPath}.xmp`;
|
|
|
|
const exists = await this.storageRepository.checkFileExists(sidecarPath, constants.W_OK);
|
|
|
|
if (!exists) {
|
|
|
|
return false;
|
2023-05-26 14:52:52 +02:00
|
|
|
}
|
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
await this.assetRepository.save({ id: asset.id, sidecarPath });
|
2023-05-26 14:52:52 +02:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
return true;
|
2023-05-26 14:52:52 +02:00
|
|
|
}
|
|
|
|
}
|