Skip to content

Commit 259d97c

Browse files
Expose detectMimeType (#41)
1 parent 1723cf4 commit 259d97c

6 files changed

Lines changed: 25 additions & 13 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ Helpers to check supported media types are exposed in `bare-media/types`:
267267
- `isMediaSupported(mimetype)`: returns `true` if the mimetype is either a supported image or video format.
268268
- `isStripMetadataSupported(mimetype)`: returns `true` if `metadata.strip()` supports the mimetype.
269269

270+
Detect the MIME type of a buffer:
271+
272+
```js
273+
import { detectMimeType } from 'bare-media'
274+
275+
const mimetype = detectMimeType(buffer)
276+
```
277+
278+
> This may return non-media MIME types, use `isMediaSupported()` to validate support.
279+
270280
## License
271281

272282
Apache-2.0

src/codecs.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import getMimeType from 'get-mime-type'
2+
import getFileFormat from 'get-file-format'
3+
14
import { IMAGE } from '../types'
25

36
export const codecs = {
@@ -41,3 +44,7 @@ export async function importFFmpeg() {
4144
export function supportsQuality(mimetype) {
4245
return { 'image/webp': true, 'image/jpeg': true }[mimetype] || false
4346
}
47+
48+
export function detectMimeType(buffer) {
49+
return getMimeType(getFileFormat(buffer))
50+
}

src/image.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import fs from 'bare-fs'
22
import fetch from 'bare-fetch'
33

4-
import { EXIF } from '../types'
5-
import { importCodec, supportsQuality } from './codecs'
4+
import { EXIF } from '../types.js'
5+
import { importCodec, supportsQuality, detectMimeType } from './codecs.js'
66
import { metadata, ImageMetadataPipeline } from './image/metadata'
7-
import { isHttpUrl, detectMimeType, calculateFitDimensions } from './util'
7+
import { isHttpUrl, calculateFitDimensions } from './util'
88

99
const animatableMimetypes = ['image/gif', 'image/webp']
1010

src/image/metadata.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IMAGE } from '../../types.js'
2-
import { detectMimeType, isHttpUrl } from '../util.js'
2+
import { detectMimeType } from '../codecs.js'
3+
import { isHttpUrl } from '../util.js'
34

45
const supportedExifMimetypes = new Set([IMAGE.JPEG, IMAGE.JPG, IMAGE.TIFF, IMAGE.TIF])
56

src/util.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
import getMimeType from 'get-mime-type'
2-
import getFileFormat from 'get-file-format'
3-
4-
export function detectMimeType(buffer) {
5-
return getMimeType(getFileFormat(buffer))
6-
}
7-
81
export function calculateFitDimensions(width, height, maxWidth = Infinity, maxHeight = Infinity) {
92
if (width <= maxWidth && height <= maxHeight) {
103
return { width, height }

test/common.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { test } from 'brittle'
22

3-
import { calculateFitDimensions, detectMimeType } from '../src/util'
3+
import { detectMimeType } from '../index'
4+
import { calculateFitDimensions } from '../src/util'
45

56
// util
67

@@ -50,7 +51,7 @@ test('util calculateFitDimensions()', async (t) => {
5051
}
5152
})
5253

53-
test('util detectMimeType()', async (t) => {
54+
test('detectMimeType()', async (t) => {
5455
t.is(detectMimeType(Buffer.from([0xff, 0xd8, 0xff])), 'image/jpeg')
5556
t.is(detectMimeType(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])), 'image/png')
5657
t.is(detectMimeType(Buffer.from([0x47, 0x49, 0x46, 0x38])), 'image/gif')

0 commit comments

Comments
 (0)