@wordpress/upload-media
Version:
Core media upload logic.
105 lines (103 loc) • 3.36 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/upload-media/src/get-image-dimensions.ts
var get_image_dimensions_exports = {};
__export(get_image_dimensions_exports, {
getImageDimensions: () => getImageDimensions
});
module.exports = __toCommonJS(get_image_dimensions_exports);
var MAX_HEADER_BYTES = 512 * 1024;
function parseJpeg(view) {
let offset = 2;
while (offset < view.byteLength) {
if (view.getUint8(offset) !== 255) {
return null;
}
while (offset < view.byteLength && view.getUint8(offset) === 255) {
offset++;
}
if (offset >= view.byteLength) {
return null;
}
const marker = view.getUint8(offset);
offset++;
if (marker === 1 || marker >= 208 && marker <= 217) {
continue;
}
if (marker === 218) {
return null;
}
if (offset + 2 > view.byteLength) {
return null;
}
const segmentLength = view.getUint16(offset);
if (segmentLength < 2) {
return null;
}
const isStartOfFrame = marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204;
if (isStartOfFrame) {
if (offset + 7 > view.byteLength) {
return null;
}
const height = view.getUint16(offset + 3);
const width = view.getUint16(offset + 5);
const interlaced = marker === 194 || marker === 198 || marker === 202 || marker === 206;
return { width, height, interlaced };
}
offset += segmentLength;
}
return null;
}
function parsePng(view) {
if (view.byteLength < 29) {
return null;
}
const isIhdr = view.getUint8(12) === 73 && // I
view.getUint8(13) === 72 && // H
view.getUint8(14) === 68 && // D
view.getUint8(15) === 82;
if (!isIhdr) {
return null;
}
const width = view.getUint32(16);
const height = view.getUint32(20);
const interlaceMethod = view.getUint8(28);
return { width, height, interlaced: interlaceMethod !== 0 };
}
async function getImageDimensions(file) {
try {
const headerBytes = Math.min(file.size, MAX_HEADER_BYTES);
const buffer = await file.slice(0, headerBytes).arrayBuffer();
const view = new DataView(buffer);
if (view.byteLength >= 3 && view.getUint16(0) === 65496) {
return parseJpeg(view);
}
if (view.byteLength >= 8 && view.getUint32(0) === 2303741511 && view.getUint32(4) === 218765834) {
return parsePng(view);
}
return null;
} catch {
return null;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getImageDimensions
});
//# sourceMappingURL=get-image-dimensions.cjs.map