2022-04-15 12:52:53 +02:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
|
import {
|
2022-08-27 14:24:26 +02:00
|
|
|
FileType,
|
|
|
|
|
ImageFileType,
|
|
|
|
|
SupportedFileTypeCategory
|
2022-04-16 16:35:28 +02:00
|
|
|
} from 'picsur-shared/dist/dto/mimes.dto';
|
2022-08-27 14:24:26 +02:00
|
|
|
|
|
|
|
|
import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types';
|
|
|
|
|
import { ParseFileType } from 'picsur-shared/dist/util/parse-mime';
|
|
|
|
|
import { ImageConverterService } from './image-converter.service';
|
2022-04-25 13:14:44 +02:00
|
|
|
import { ImageResult } from './imageresult';
|
2022-04-21 16:53:40 +02:00
|
|
|
|
2022-04-15 12:52:53 +02:00
|
|
|
@Injectable()
|
|
|
|
|
export class ImageProcessorService {
|
2022-08-27 14:24:26 +02:00
|
|
|
constructor(private readonly imageConverter: ImageConverterService) {}
|
|
|
|
|
|
2022-04-15 12:52:53 +02:00
|
|
|
public async process(
|
|
|
|
|
image: Buffer,
|
2022-08-27 14:24:26 +02:00
|
|
|
filetype: FileType,
|
2022-04-25 13:14:44 +02:00
|
|
|
): AsyncFailable<ImageResult> {
|
2022-08-27 14:24:26 +02:00
|
|
|
if (filetype.category === SupportedFileTypeCategory.Image) {
|
|
|
|
|
return await this.processStill(image, filetype);
|
|
|
|
|
} else if (filetype.category === SupportedFileTypeCategory.Animation) {
|
|
|
|
|
return await this.processAnimation(image, filetype);
|
2022-04-15 12:52:53 +02:00
|
|
|
} else {
|
2022-07-04 17:11:42 +02:00
|
|
|
return Fail(FT.SysValidation, 'Unsupported mime type');
|
2022-04-15 12:52:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async processStill(
|
|
|
|
|
image: Buffer,
|
2022-08-27 14:24:26 +02:00
|
|
|
filetype: FileType,
|
2022-04-25 13:14:44 +02:00
|
|
|
): AsyncFailable<ImageResult> {
|
2022-08-27 14:24:26 +02:00
|
|
|
const outputFileType = ParseFileType(ImageFileType.QOI);
|
|
|
|
|
if (HasFailed(outputFileType)) return outputFileType;
|
2022-04-16 16:35:28 +02:00
|
|
|
|
2022-08-27 14:24:26 +02:00
|
|
|
return this.imageConverter.convert(image, filetype, outputFileType, {});
|
2022-04-15 12:52:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async processAnimation(
|
|
|
|
|
image: Buffer,
|
2022-08-27 14:24:26 +02:00
|
|
|
filetype: FileType,
|
2022-04-25 13:14:44 +02:00
|
|
|
): AsyncFailable<ImageResult> {
|
2022-08-27 17:25:39 +02:00
|
|
|
// Webps and gifs are stored as is for now
|
2022-04-21 16:53:40 +02:00
|
|
|
return {
|
|
|
|
|
image: image,
|
2022-08-27 14:24:26 +02:00
|
|
|
filetype: filetype.identifier,
|
2022-04-21 16:53:40 +02:00
|
|
|
};
|
2022-04-15 12:52:53 +02:00
|
|
|
}
|
|
|
|
|
}
|