({ fileBuffer, config, isLossless })
| 131 | } |
| 132 | |
| 133 | async function processFileByFormat({ fileBuffer, config, isLossless }) { |
| 134 | const imageMetadata = await parseImageMetadata(fileBuffer); |
| 135 | |
| 136 | if (!imageMetadata.format) { |
| 137 | throw new Error('Unknown file format'); |
| 138 | } |
| 139 | |
| 140 | switch (imageMetadata.format) { |
| 141 | case 'jpeg': { |
| 142 | return processJpeg({ fileBuffer, config, isLossless }); |
| 143 | } |
| 144 | |
| 145 | case 'png': { |
| 146 | return processPng({ fileBuffer, config, isLossless }); |
| 147 | } |
| 148 | |
| 149 | case 'gif': { |
| 150 | return processGif({ fileBuffer, config, isLossless }); |
| 151 | } |
| 152 | |
| 153 | case 'svg': { |
| 154 | return processSvg({ fileBuffer, config }); |
| 155 | } |
| 156 | |
| 157 | default: { |
| 158 | throw new Error(`Unsupported image format: "${imageMetadata.format}"`); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | async function processJpeg({ fileBuffer, config, isLossless }) { |
| 164 | const sharpImage = sharp(fileBuffer) |
no test coverage detected