All files / api/facade processImage.ts

100% Statements 44/44
100% Branches 2/2
100% Functions 1/1
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 451x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { IDatabaseFile } from '@contracts/databaseFile';
import { IFile } from '@contracts/file';
import { ResultWithValue } from '@contracts/resultWithValue';
import { anyObject } from '@helpers/typescriptHacks';
import { getApiFileService } from '@services/internal/apiFileService';
import { getImageProcessingService } from '@services/internal/imageProcessingService';
import { getLog } from '@services/internal/logService';
 
export const processImageFromFormData = async (props: {
  fileFromForm: IFile;
  handlerName: string;
  width?: number;
  height?: number;
}): Promise<ResultWithValue<IDatabaseFile>> => {
  const bufferResult = await getApiFileService().formDataToBuffer(props.fileFromForm);
  if (bufferResult.isSuccess == false) {
    getLog().e(`${props.handlerName} processImageFromFormData: `, bufferResult.value);
    return {
      isSuccess: false,
      value: anyObject,
      errorMessage: bufferResult.errorMessage,
    };
  }
 
  const resizedBufferResult = await getImageProcessingService().resize({
    input: bufferResult.value,
    resize: {
      width: props.width,
      height: props.height,
      fit: 'contain',
    },
  });
 
  return {
    isSuccess: true,
    value: {
      name: props.fileFromForm.newFilename,
      mediaType: props.fileFromForm.mimetype,
      enablePublicUrl: true,
      base64Content: resizedBufferResult.value.toString('base64'),
    },
    errorMessage: '',
  };
};