UNPKG

filepond

Version:

FilePond, where files go to stretch their bits.

181 lines (180 loc) • 4.63 kB
/*! * FilePond v5.0.2 * Copyright (c) 2017-2026 Pqina B.V. * Released under the MIT License * https://filepond.com */ import { sizeCreate as u } from "./size.js"; import { isImageFile as d, isVideoFile as m } from "./test.js"; function M(t) { return d(t) ? h(t) : m(t) ? W(t) : Promise.resolve(null); } async function h(t, r) { const { bytesToRead: e = 65536 } = {}, i = await t.slice(0, e).arrayBuffer(), n = new DataView(i); let o; return b(n) ? o = y(n) : z(n) ? o = L(n) : F(n) ? o = P(n) : p(n) ? o = R(n) : (I(n) || S(n)) && (o = V(n)), o || B(t); } function b(t) { return t.getUint16(0) === 65496; } function y(t) { const { byteLength: r } = t; let e = 2; for (; e < r - 7; ) { const i = t.getUint16(e), n = t.getUint16(e + 2); if ((i & 65280) === 65280 && i >= 65472 && i <= 65487) return u(t.getUint16(e + 7), t.getUint16(e + 5)); e += n + 2; } return null; } function z(t) { return t.getUint32(0) === 2303741511; } function L(t) { return u(t.getUint32(16), t.getUint32(20)); } function F(t) { return t.getUint32(0) === 1380533830 || t.getUint32(8) === 1464156752; } function P(t) { const { byteLength: r } = t, e = 1464156752, i = 1380533830, n = 1448097880, o = 1448097824, l = 1448097868, a = (g, c) => g.getUint8(c) | g.getUint8(c + 1) << 8 | g.getUint8(c + 2) << 16; if (r < 16 || t.getUint32(0, !1) !== i || t.getUint32(8, !1) !== e) return null; let f = 12; for (; f + 8 <= r; ) { const g = t.getUint32(f, !1), c = t.getUint32(f + 4, !0), s = f + 8; if (s + c > r) break; if (g === n) return u( 1 + a(t, s + 4), 1 + a(t, s + 7) ); if (g === o && c >= 10 && t.getUint8(s + 3) === 157 && t.getUint8(s + 4) === 1 && t.getUint8(s + 5) === 42) return u( // only interested in the first 14 bits (& 0x3fff) t.getUint16(s + 6, !0) & 16383, t.getUint16(s + 8, !0) & 16383 ); if (g === l && c >= 5 && t.getUint8(s) === 47) { const U = t.getUint32(s + 1, !0); return u((U & 16383) + 1, (U >>> 14 & 16383) + 1); } f = s + c + c % 2; } return null; } function p(t) { return t.getUint32(0) === 1195984440; } function R(t) { return u(t.getUint16(6, !0), t.getUint16(8, !0)); } const E = [ 1751476579, // 'heic' 1751476600, // 'heix' 1751476589, // 'heim' 1751476595, // 'heis' 1835623985, // 'mif1' 1835623985 // 'msf1' ]; function I(t) { if (t.getUint32(4) !== 1718909296) return !1; const e = t.getUint32(8); return E.includes(e); } function S(t) { if (t.getUint32(4) !== 1718909296) return !1; const e = t.getUint32(8, !1); return e === 1635150182 || e === 1635150195; } function V(t) { const { byteLength: r } = t; let e = 0; for (; e < r - 16; ) { const i = t.getUint32(e), n = t.getUint32(e + 4); if (n === 1835365473) { e += 12; continue; } if (n === 1768977008 || n === 1768973167) { e += 8; continue; } if (n === 1769173093) return u(t.getUint32(e + 12), t.getUint32(e + 16)); e += i > 8 ? i : 8; } return null; } function B(t) { return new Promise((r, e) => { const i = new Image(); i.onerror = (n) => { URL.revokeObjectURL(i.src), e(new Error("Unknown")); }, i.onload = () => { const { naturalWidth: n, naturalHeight: o } = i; URL.revokeObjectURL(i.src), r({ width: n, height: o }); }, i.src = URL.createObjectURL(t); }); } async function W(t, r) { const { bytesToRead: e = 65536 } = {}, i = await t.slice(0, e).arrayBuffer(), n = new DataView(i); let o; return k(n) && (o = G(n)), o || j(t); } function k(t) { return t.getUint32(4) === 1718909296; } function G(t) { const { byteLength: r } = t; let e = 0; for (; e < r; ) { const i = t.getUint32(e), n = t.getUint32(e + 4); if (n === 1836019574) { e += 8; continue; } if (n === 1953653099) { e += 8; continue; } if (n === 1953196132) return u( t.getUint32(e + i - 8) >> 16, t.getUint32(e + i - 4) >> 16 ); e += i > 8 ? i : 8; } return null; } function j(t) { return new Promise((r, e) => { const i = document.createElement("video"); i.preload = "metadata", i.onerror = e, i.onloadedmetadata = () => { const { videoWidth: n, videoHeight: o } = i; URL.revokeObjectURL(i.src), r({ width: n, height: o }); }, i.src = URL.createObjectURL(t); }); } export { h as getImageSize, B as getImageSizeWithElement, M as getMediaSize, W as getVideoSize, j as getVideoSizeWithElement };