one
Version:
One is a React framework for web, iOS, and Android apps.
106 lines (104 loc) • 3.01 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var getImageData_exports = {};
__export(getImageData_exports, {
getImageData: () => getImageData,
getSharp: () => getSharp,
processImageMeta: () => processImageMeta
});
module.exports = __toCommonJS(getImageData_exports);
var import_node_fs = require("node:fs");
var import_node_path = require("node:path");
let sharpWarned = false;
async function getSharp() {
try {
const sharpModule = await import("sharp");
return sharpModule.default;
} catch (e) {
if (!sharpWarned) {
sharpWarned = true;
console.warn(`
[one] To use getImageData, install sharp:
npm install sharp
`);
}
return null;
}
}
async function processImageMeta(filePath) {
const sharp = await getSharp();
if (!sharp) {
return null;
}
try {
const image = sharp(filePath);
const metadata = await image.metadata();
const { width = 0, height = 0 } = metadata;
const blurBuffer = await image.resize(10).blur(1).jpeg({ quality: 40 }).toBuffer();
const blurDataURL = `data:image/jpeg;base64,${blurBuffer.toString("base64")}`;
return {
width,
height,
blurDataURL
};
} catch (e) {
console.warn(`[one] processImageMeta: Failed to process ${filePath}:`, e);
return null;
}
}
async function getImageData(filePath) {
let resolvedPath;
let src;
if (filePath.startsWith("/")) {
resolvedPath = (0, import_node_path.resolve)("./public", filePath.slice(1));
src = filePath;
} else {
resolvedPath = (0, import_node_path.resolve)(filePath);
src = filePath.replace(/^\.\.?\//, "").replace(/^public\//, "");
if (!src.startsWith("/")) {
src = "/" + src;
}
}
const defaultResult = {
src,
width: 0,
height: 0,
blurDataURL: ""
};
if (!(0, import_node_fs.existsSync)(resolvedPath)) {
console.warn(`[one] getImageData: File not found: ${resolvedPath}`);
return defaultResult;
}
const meta = await processImageMeta(resolvedPath);
if (!meta) {
return defaultResult;
}
return {
src,
...meta
};
}