From 481db27c28533371f629c113be05d5f999f20e47 Mon Sep 17 00:00:00 2001 From: Ardi Date: Fri, 20 Aug 2021 12:00:17 +0700 Subject: [PATCH] Create new helper for validation --- src/utils/validation.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/utils/validation.ts diff --git a/src/utils/validation.ts b/src/utils/validation.ts new file mode 100644 index 0000000..3f656c2 --- /dev/null +++ b/src/utils/validation.ts @@ -0,0 +1,23 @@ +export function fileSizeValidation(stream?: any, maxSize?: number, callback?): Promise { + + return new Promise((resolve, reject) => { + let bytes=0; + + stream.on("data", (chunk) => { + bytes += chunk.length; + }); + + stream.on("end", () => { + if (bytes > maxSize) { + reject(`File exceeds the maximum size ${maxSize}`); + } else { + resolve(bytes); + } + }); + + stream.on("error", (e) => { + reject(e); + }); + }); + +} \ No newline at end of file