2023-03-30 21:38:55 +02:00
|
|
|
import { AssetType } from '@app/infra/entities';
|
2023-02-25 15:12:03 +01:00
|
|
|
import { Inject, Injectable, Logger } from '@nestjs/common';
|
|
|
|
import { join } from 'path';
|
2023-03-20 16:55:28 +01:00
|
|
|
import { IAssetRepository, mapAsset, WithoutProperty } from '../asset';
|
2023-02-25 15:12:03 +01:00
|
|
|
import { CommunicationEvent, ICommunicationRepository } from '../communication';
|
2023-03-20 16:55:28 +01:00
|
|
|
import { IAssetJob, IBaseJob, IJobRepository, JobName } from '../job';
|
2023-03-25 15:50:57 +01:00
|
|
|
import { IStorageRepository, StorageCore, StorageFolder } from '../storage';
|
2023-02-25 15:12:03 +01:00
|
|
|
import { IMediaRepository } from './media.repository';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class MediaService {
|
|
|
|
private logger = new Logger(MediaService.name);
|
2023-03-25 15:50:57 +01:00
|
|
|
private storageCore = new StorageCore();
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
|
|
|
|
@Inject(ICommunicationRepository) private communicationRepository: ICommunicationRepository,
|
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
|
|
|
@Inject(IMediaRepository) private mediaRepository: IMediaRepository,
|
|
|
|
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
|
|
|
|
) {}
|
|
|
|
|
2023-03-20 16:55:28 +01:00
|
|
|
async handleQueueGenerateThumbnails(job: IBaseJob): Promise<void> {
|
|
|
|
try {
|
|
|
|
const { force } = job;
|
|
|
|
|
|
|
|
const assets = force
|
|
|
|
? await this.assetRepository.getAll()
|
|
|
|
: await this.assetRepository.getWithout(WithoutProperty.THUMBNAIL);
|
|
|
|
|
|
|
|
for (const asset of assets) {
|
|
|
|
await this.jobRepository.queue({ name: JobName.GENERATE_JPEG_THUMBNAIL, data: { asset } });
|
|
|
|
}
|
|
|
|
} catch (error: any) {
|
|
|
|
this.logger.error('Failed to queue generate thumbnail jobs', error.stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-25 15:12:03 +01:00
|
|
|
async handleGenerateJpegThumbnail(data: IAssetJob): Promise<void> {
|
|
|
|
const { asset } = data;
|
|
|
|
|
2023-03-24 03:40:46 +01:00
|
|
|
try {
|
2023-03-25 15:50:57 +01:00
|
|
|
const resizePath = this.storageCore.getFolderLocation(StorageFolder.THUMBNAILS, asset.ownerId);
|
2023-03-24 03:40:46 +01:00
|
|
|
this.storageRepository.mkdirSync(resizePath);
|
2023-03-25 15:50:57 +01:00
|
|
|
const jpegThumbnailPath = join(resizePath, `${asset.id}.jpeg`);
|
2023-03-24 03:40:46 +01:00
|
|
|
|
|
|
|
if (asset.type == AssetType.IMAGE) {
|
|
|
|
try {
|
|
|
|
await this.mediaRepository.resize(asset.originalPath, jpegThumbnailPath, { size: 1440, format: 'jpeg' });
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.warn(
|
|
|
|
`Failed to generate jpeg thumbnail using sharp, trying with exiftool-vendored (asset=${asset.id})`,
|
|
|
|
);
|
|
|
|
await this.mediaRepository.extractThumbnailFromExif(asset.originalPath, jpegThumbnailPath);
|
|
|
|
}
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
|
|
|
|
2023-03-24 03:40:46 +01:00
|
|
|
if (asset.type == AssetType.VIDEO) {
|
|
|
|
this.logger.log('Start Generating Video Thumbnail');
|
|
|
|
await this.mediaRepository.extractVideoThumbnail(asset.originalPath, jpegThumbnailPath);
|
|
|
|
this.logger.log(`Generating Video Thumbnail Success ${asset.id}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.assetRepository.save({ id: asset.id, resizePath: jpegThumbnailPath });
|
|
|
|
|
2023-02-25 15:12:03 +01:00
|
|
|
asset.resizePath = jpegThumbnailPath;
|
|
|
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.GENERATE_WEBP_THUMBNAIL, data: { asset } });
|
2023-03-20 16:55:28 +01:00
|
|
|
await this.jobRepository.queue({ name: JobName.CLASSIFY_IMAGE, data: { asset } });
|
|
|
|
await this.jobRepository.queue({ name: JobName.DETECT_OBJECTS, data: { asset } });
|
2023-03-18 14:44:42 +01:00
|
|
|
await this.jobRepository.queue({ name: JobName.ENCODE_CLIP, data: { asset } });
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
this.communicationRepository.send(CommunicationEvent.UPLOAD_SUCCESS, asset.ownerId, mapAsset(asset));
|
2023-03-24 03:40:46 +01:00
|
|
|
} catch (error: any) {
|
|
|
|
this.logger.error(`Failed to generate thumbnail for asset: ${asset.id}/${asset.type}`, error.stack);
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleGenerateWepbThumbnail(data: IAssetJob): Promise<void> {
|
|
|
|
const { asset } = data;
|
|
|
|
|
|
|
|
if (!asset.resizePath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const webpPath = asset.resizePath.replace('jpeg', 'webp');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.mediaRepository.resize(asset.resizePath, webpPath, { size: 250, format: 'webp' });
|
|
|
|
await this.assetRepository.save({ id: asset.id, webpPath: webpPath });
|
|
|
|
} catch (error: any) {
|
|
|
|
this.logger.error('Failed to generate webp thumbnail for asset: ' + asset.id, error.stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|