import { Scalar, CustomScalar } from "@nestjs/graphql"; import { FileUpload } from "../helpers"; export type ImageProps = Promise; @Scalar("Upload", (type) => Upload) export class Upload implements CustomScalar { description = "Image file upload type."; supportedFormats = ["image/jpeg", "image/png"]; async parseValue(value: ImageProps): ImageProps { return value; } async serialize(value: ImageProps): ImageProps { return value; } parseLiteral(file: any): any { return file; } // parseLiteral(file: ValueNode) { // if (file.kind === "ObjectValue") { // const fileObject = file as any; // if ( // typeof fileObject.filename === "string" && // typeof fileObject.mimetype === "string" && // typeof fileObject.encoding === "string" && // typeof fileObject.createReadStream === "function" // ) // return Promise.resolve(fileObject); // } // return null; // } // async parseValue(value: ImageFileProps) { // // Comes through ok here // const upload = await value; // const stream = upload.createReadStream(); // const fileType = await FileType.fromStream(stream); // if (isUndefined(fileType)) // throw new BadRequestException("Mime type is unknown."); // if (fileType.mime !== upload.mimetype) // throw new BadRequestException("Mime type does not match file content."); // if (!this.supportedFormats.includes(fileType.mime)) // throw new BadRequestException( // `Unsupported file format. Supports: ${this.supportedFormats.join(" ")}.` // ); // return upload; // } // serialize(value: ImageFileProps) { // return value; // } }