UNPKG

filepond

Version:

FilePond, where files go to stretch their bits.

131 lines (130 loc) • 3.87 kB
/*! * FilePond v5.0.2 * Copyright (c) 2017-2026 Pqina B.V. * Released under the MIT License * https://filepond.com */ import { createValidatorExtension as T } from "./common/createValidatorExtension.js"; import { isFileEntry as D, isFile as h, isImageFile as M, isVideoFile as V } from "../utils/test.js"; import { getMediaSize as g } from "../utils/media.js"; const v = T({ name: "MediaResolutionValidator", props: { toNaturalResolution: (m) => `${Math.round(m / 1e6)}` }, factory: ({ props: m, didSetProps: O }, { updateEntry: N }) => { let E = !1, _ = !1, c = !1; return O( ({ minWidth: i = 1, maxWidth: R = 1 / 0, minHeight: o = 1, maxHeight: t = 1 / 0, minResolution: e = 1, maxResolution: n = 1 / 0 }) => { E = i > 0 || R < 1 / 0, _ = o > 0 || t < 1 / 0, c = e > 0 || n < 1 / 0; } ), { validateEntry: async (i) => { if (!D(i) || !h(i.file)) return null; const { file: R } = i; if (!E && !_ && !c) return null; const o = await g(R); if (o === null) return { code: "VALIDATION_MEDIA_SIZE_UNAVAILABLE" }; N(i, { meta: { size: o } }); const { width: t, height: e } = o, { minWidth: n, maxWidth: a, minHeight: l, maxHeight: s, minResolution: u, maxResolution: I, toNaturalResolution: d } = m; if (E && (t < n || t > a)) { const r = t < n, A = t > a; if (n > 1 && a < 1 / 0) return { code: "VALIDATION_MEDIA_WIDTH_RANGE_MISMATCH", values: { minWidth: n, minWidthUnit: "unitPixels", maxWidth: a, maxWidthUnit: "unitPixels" } }; if (r) return { code: "VALIDATION_MEDIA_WIDTH_UNDERFLOW", values: { minWidth: n, minWidthUnit: "unitPixels" } }; if (A) return { code: "VALIDATION_MEDIA_WIDTH_OVERFLOW", values: { maxWidth: a, maxWidthUnit: "unitPixels" } }; } if (_ && (e < l || e > s)) { const r = e < l, A = e > s; if (l > 1 && s < 1 / 0) return { code: "VALIDATION_MEDIA_HEIGHT_RANGE_MISMATCH", values: { minHeight: l, minHeightUnit: "unitPixels", maxHeight: s, maxHeightUnit: "unitPixels" } }; if (r) return { code: "VALIDATION_MEDIA_HEIGHT_UNDERFLOW", values: { minHeight: l, minHeightUnit: "unitPixels" } }; if (A) return { code: "VALIDATION_MEDIA_HEIGHT_OVERFLOW", values: { maxHeight: s, maxHeightUnit: "unitPixels" } }; } const f = t * e; if (c && (f < u || f > I)) { const r = f < u, A = f > I; if (u > 1 && I < 1 / 0) return { code: "VALIDATION_MEDIA_RESOLUTION_RANGE_MISMATCH", values: { minResolution: d(u), maxResolution: d(I) } }; if (r) return { code: "VALIDATION_MEDIA_RESOLUTION_UNDERFLOW", values: { minResolution: d(u) } }; if (A) return { code: "VALIDATION_MEDIA_RESOLUTION_OVERFLOW", values: { maxResolution: d(I) } }; } return null; }, canValidateEntry: (i) => D(i) && (M(i.file) || V(i.file)) }; } }); export { v as MediaResolutionValidator };