| Total Complexity | 8 |
| Complexity/F | 4 |
| Lines of Code | 37 |
| Function Count | 2 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import * as fs from "fs" |
||
| 2 | import { isBuffer } from "../util" |
||
| 3 | |||
| 4 | function getPictureMimeTypeFromBuffer(pictureBuffer: Buffer) { |
||
| 5 | if ( |
||
| 6 | pictureBuffer.length > 3 && |
||
| 7 | pictureBuffer.compare(Buffer.from([0xff, 0xd8, 0xff]), 0, 3, 0, 3) === 0 |
||
| 8 | ) { |
||
| 9 | return "image/jpeg" |
||
| 10 | } |
||
| 11 | if ( |
||
| 12 | pictureBuffer.length > 8 && |
||
| 13 | pictureBuffer.compare(Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]), 0, 8, 0, 8) === 0 |
||
| 14 | ) { |
||
| 15 | return "image/png" |
||
| 16 | } |
||
| 17 | return "" |
||
| 18 | } |
||
| 19 | |||
| 20 | export function retrievePictureAndMimeType({ |
||
| 21 | filenameOrBuffer, |
||
| 22 | mimeType |
||
| 23 | }: { |
||
| 24 | filenameOrBuffer?: Buffer | string |
||
| 25 | mimeType?: string |
||
| 26 | }) { |
||
| 27 | if (!filenameOrBuffer) { |
||
| 28 | throw new TypeError("Missing image buffer or filename") |
||
| 29 | } |
||
| 30 | const pictureBuffer = isBuffer(filenameOrBuffer) ? |
||
| 31 | filenameOrBuffer : fs.readFileSync(filenameOrBuffer) |
||
| 32 | return { |
||
| 33 | pictureBuffer, |
||
| 34 | mimeType: mimeType ?? getPictureMimeTypeFromBuffer(pictureBuffer) |
||
| 35 | } |
||
| 36 | } |
||
| 37 |