bare-media 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-media",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -26,6 +26,7 @@
26
26
  "bare-tiff": "^1.0.1",
27
27
  "bare-webp": "^1.0.3",
28
28
  "cross-worker": "^1.1.0",
29
+ "get-file-format": "^1.0.1",
29
30
  "get-mime-type": "^2.0.1",
30
31
  "hrpc": "^4.0.0",
31
32
  "hyperschema": "^1.12.3",
package/worker/media.js CHANGED
@@ -1,10 +1,13 @@
1
1
  import b4a from 'b4a'
2
2
  import fs from 'bare-fs'
3
3
  import fetch from 'bare-fetch'
4
- import getMimeType from 'get-mime-type'
5
4
 
6
- import { importCodec, supportsQuality } from '../shared/codecs.js'
7
- import { calculateFitDimensions } from './util'
5
+ import {
6
+ importCodec,
7
+ isCodecSupported,
8
+ supportsQuality
9
+ } from '../shared/codecs.js'
10
+ import { detectMimeType, calculateFitDimensions } from './util'
8
11
 
9
12
  const DEFAULT_PREVIEW_FORMAT = 'image/webp'
10
13
 
@@ -22,10 +25,15 @@ export async function createPreview({
22
25
  format,
23
26
  encoding
24
27
  }) {
25
- mimetype = mimetype || getMimeType(path)
26
28
  format = format || DEFAULT_PREVIEW_FORMAT
27
29
 
28
30
  const buff = await getBuffer({ path, httpLink, buffer })
31
+ mimetype = mimetype || detectMimeType(buff, path)
32
+
33
+ if (!isCodecSupported(mimetype)) {
34
+ throw new Error(`Unsupported file type: No codec available for ${mimetype}`)
35
+ }
36
+
29
37
  const rgba = await decodeImageToRGBA(buff, mimetype, maxFrames)
30
38
  const { width, height } = rgba
31
39
 
@@ -118,6 +126,11 @@ export async function createPreview({
118
126
 
119
127
  export async function decodeImage({ path, httpLink, buffer, mimetype }) {
120
128
  const buff = await getBuffer({ path, httpLink, buffer })
129
+ mimetype = mimetype || detectMimeType(buff, path)
130
+
131
+ if (!isCodecSupported(mimetype)) {
132
+ throw new Error(`Unsupported file type: No codec available for ${mimetype}`)
133
+ }
121
134
 
122
135
  const rgba = await decodeImageToRGBA(buff, mimetype)
123
136
  const { width, height, data } = rgba
package/worker/util.js CHANGED
@@ -1,5 +1,12 @@
1
+ import getMimeType from 'get-mime-type'
2
+ import getFileFormat from 'get-file-format'
3
+
1
4
  export const log = (...args) => console.log('[bare-media]', ...args)
2
5
 
6
+ export function detectMimeType(buffer, path) {
7
+ return getMimeType(getFileFormat(buffer)) || getMimeType(path)
8
+ }
9
+
3
10
  export function calculateFitDimensions(width, height, maxWidth, maxHeight) {
4
11
  if (width <= maxWidth && height <= maxHeight) {
5
12
  return { width, height }