Picsur/backend/src/managers/image/image-processor.service.ts

56 lines
1.5 KiB
TypeScript
Raw Normal View History

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