0

Check if filesize is > than 25mb and throw an error. I think there is some glitch with my code below . a help would be much appreciated.

#Snippet 1

const checkSize = () => async (data) => {
  // eslint-disable-next-line no-use-before-define
  const filesize = formatBytes(data.filesize);

  if (filesize > 2.5e+7) {
    throw new errors.BadRequest('exceeded the limit');
  }

#snippet 2

const formatBytes = (bytes, decimals = 2) => {
  if (bytes === 0) {
    return '0 Bytes';
  }

  const k = 1024;
  const dm = decimals < 0 ? 0 : decimals;
  // const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

  const i = Math.floor(Math.log(bytes) / Math.log(k));

  // eslint-disable-next-line no-restricted-properties
  return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))}`;
};

0 Answers0