UNPKG

@oberoncms/plugin-uploadthing

Version:

An Puck component and OberonCMS plugin for embeding uploadthing images

35 lines (34 loc) 884 B
import { imageSize } from "image-size"; async function getImageSize(url, defaultSize = { width: 100, height: 100 }) { const MAX_HEADER_BYTES = 128 * 1024; let totalBytes = 0; const chunks = []; const response = await fetch(url); if (!response.body) { throw new Error("Image not found for size processing"); } const reader = response.body.getReader(); while (true) { const { done, value } = await reader.read(); if (done) { return defaultSize; } if (totalBytes + value.length > MAX_HEADER_BYTES) { reader.cancel(); return defaultSize; } totalBytes += value.length; chunks.push(value); try { const { width, height } = imageSize(Buffer.concat(chunks)); if (width && height) { return { width, height }; } return defaultSize; } catch (error) { } } } export { getImageSize };