Skip to content

Commit

Permalink
Add incomplete file interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
neos1803 committed Aug 17, 2021
1 parent ceb6a4b commit 150a6b1
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/interceptors/request.inteceptors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { BadRequestException, CallHandler, Injectable, NestInterceptor, Type } from "@nestjs/common";
import { GraphQLExecutionContext } from "@nestjs/graphql";
import { Observable, throwError } from "rxjs";
import { tap } from "rxjs/operators";
import * as fs from "fs";

// export function SingleFileInterceptor(type?: string, maxSize?: number): Type<NestInterceptor> {

// class RequestInterceptor implements NestInterceptor {
// intercept(
// context: GraphQLExecutionContext,
// next: CallHandler
// ): Observable<any> {
// const [
// _,
// payloads,
// {
// req: {
// body: { query },
// // ipInfo: { ip, country }
// }
// },
// {
// fieldNodes,
// variableValues
// }
// ] = context.getArgs();
// console.log(payloads.file)
// // const stream = await Promise.resolve(payloads.file).then((v) => {
// // return v.createReadStream()
// // });
// // fileSizeValidation(1000000, stream, (error, status) => {
// // if (!status) {
// // throw new BadRequestException('File melebihi limit');
// // // return next.handle().pipe(tap(() => throwError('File melebihi limit')))
// // }
// // });
// return next.handle().pipe();
// }
// }

// return RequestInterceptor;
// }

function fileSizeValidation(size?: number, stream?: any, callback?) {

let bytes=0;

stream.on("data", (chunk) => {
bytes += chunk.length;
});

stream.on("end", () => {
// console.log('end :', bytes);
if (bytes > 600000) {
callback(null, false);
} else {
callback(null, true);
}
});

}

0 comments on commit 150a6b1

Please sign in to comment.