I wrote a script to verify and convert images as they pass through. Utilizing resources from nestjs, magic-bytes.js, and Sharp. However, I encountered the following error:
Type 'Promise<Buffer>[]' is missing the following properties from type 'Promise<File | File[]>': then, catch, finally, [Symbol.toStringTag]ts(2739)
Any assistance in resolving this issue would be greatly appreciated.
import { ArgumentMetadata, BadRequestException, Injectable, PipeTransform } from "@nestjs/common";
import { filetypemime } from "magic-bytes.js";
import * as sharp from "sharp";
@Injectable()
export class ParseFile implements PipeTransform {
async transform(
incomingFiles: Express.Multer.File | Express.Multer.File[],
metadata: ArgumentMetadata,
): Promise<Express.Multer.File | Express.Multer.File[]> {
const files = Array.isArray(incomingFiles) ? incomingFiles : [incomingFiles];
const bytes = files.map((file) => new Uint8Array(file.buffer));
const fileMimetype = filetypemime(bytes);
if (fileMimetype) {
console.log(`File type is ${fileMimetype}`);
}
if (incomingFiles === undefined || incomingFiles === null) {
throw new BadRequestException("Validation failed (file expected)");
}
if (Array.isArray(incomingFiles) && incomingFiles.length === 0) {
throw new BadRequestException("Validation failed (files expected)");
}
const compressedFiles: Promise<Express.Multer.File | Express.Multer.File[]> = files.map(
async (file) => await sharp(file.buffer).resize(320, 240).toFormat("webp").toBuffer(),
);
return Array.isArray(incomingFiles) ? compressedFiles : compressedFiles;
}
}
I attempted to remove the type from compressedFiles, but then encountered a different error:
Type 'Promise<Buffer>[]' is not assignable to type 'File | File[]'