import path from 'path' ; import sizeOf from 'image-size' ; import { Image } from '@icarus/shared' ; const isUnwantedImage = ( filename ) => { return filename . includes ( '__MACOSX' ) ; } ; const isImageExtension = ( extension ) => { const lowerCased = extension . toLowerCase ( ) ; return lowerCased === '.jpg' || lowerCased === '.jpeg' || lowerCased === '.bmp' || lowerCased === '.png' || lowerCased === '.tiff' ; } ; export const isReadableImage = ( filename ) => { const extension = path . extname ( filename ) ; return isImageExtension ( extension ) && ! isUnwantedImage ( filename ) ; } ; export const constructImageInfo = ( name , contentBuffer ) : Image => { const info = sizeOf ( contentBuffer ) ; if ( info . width === undefined || info . height === undefined || info . type === undefined ) { throw new Error ( `Cannot detect size of ${ name }` ) ; } const shouldSwitchWidthAndHeight = info . orientation && info . orientation >= 5 && info . orientation <= 8 ; return { name , width : shouldSwitchWidthAndHeight ? info . height : info . width , height : shouldSwitchWidthAndHeight ? info . width : info . height , content : `data:image/${ info . type };base64,${ contentBuffer . toString ( 'base64' ) }` , } ; } ;