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( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/file/index.ts var file_exports = {}; __export(file_exports, { base64DecodeImageKey: () => base64DecodeImageKey, base64toBlob: () => base64toBlob, dataURLToBlob: () => dataURLToBlob, downloadPackageImages: () => downloadPackageImages, fileToBase64: () => fileToBase64, generateImg: () => generateImg, getBase64ByImage: () => getBase64ByImage, getBase64ByUrl: () => getBase64ByUrl, getBase64Image: () => getBase64Image, getFileByRect: () => getFileByRect, getFileSize: () => getFileSize, getImageKey: () => getImageKey, urlToBase64V2: () => urlToBase64V2, urlToImg: () => urlToImg }); module.exports = __toCommonJS(file_exports); var import_base_64 = __toESM(require("base-64")); var import_jszip = __toESM(require("jszip")); var import_file_saver = __toESM(require("file-saver")); var import_utils = require("../utils"); var import_lodash_es = require("lodash-es"); var urlToImg = (url) => { const resImage = new Promise((resolve) => { const image = new Image(); image.crossOrigin = ""; image.src = url; image.onload = () => { resolve(image); }; }); return resImage; }; var base64DecodeImageKey = (base64ImgKey) => { let tempStr = base64ImgKey; if ((0, import_utils.matchS3Prefix)(tempStr)) { tempStr = tempStr.replace(/^v[0-9]_/, ""); tempStr = import_base_64.default.decode(tempStr); } const [bucket, ...pathArr] = tempStr.split("_"); return tempStr = `${bucket}/${pathArr.join("_")}`; }; var getBase64ByUrl = function(src, outputFormat = "image/png") { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open("GET", src, true); xhr.responseType = "arraybuffer"; xhr.onload = function(e) { if (Number(xhr.status) === 200) { const uInt8Array = new Uint8Array(xhr.response); let i = uInt8Array.length; const binaryString = new Array(i); while (i--) { binaryString[i] = String.fromCharCode(uInt8Array[i]); } const data = binaryString.join(""); const base64 = window.btoa(data); const dataUrl = "data:" + (outputFormat || "image/png") + ";base64," + base64; resolve(dataUrl); } else { reject(e); } }; xhr.onerror = (e) => { reject(e); }; xhr.send(); }); }; var fileToBase64 = (file) => { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function(e) { resolve(e == null ? void 0 : e.target.result); }; reader.onerror = function(e) { reject(e); }; }); }; var getBase64Image = (image, width, height) => { const canvas = document.createElement("canvas"); canvas.width = width !== void 0 ? width : image.width; canvas.height = height !== void 0 ? height : image.height; const ctx = canvas.getContext("2d"); ctx == null ? void 0 : ctx.drawImage(image, 0, 0, canvas.width, canvas.height); const ext = image.src.substring(image.src.lastIndexOf(".") + 1).toLowerCase(); const dataURL = canvas.toDataURL("image/" + ext); return dataURL; }; var getBase64ByImage = function(src) { return new Promise((resolve, reject) => { const image = new Image(); const timestamp = (/* @__PURE__ */ new Date()).getTime(); const imgUrl = src + "?" + timestamp; image.src = imgUrl; image.onload = function() { function getBase64Image2(img) { const canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.height; const ctx = canvas.getContext("2d"); ctx == null ? void 0 : ctx.drawImage(img, 0, 0, img.width, img.height); const ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase(); const dataURL = canvas.toDataURL("image/" + ext); return dataURL; } const base64 = getBase64Image2(image); resolve(base64); }; image.onerror = (e) => { reject(e); }; }); }; var urlToBase64V2 = (url) => { return new Promise((resolve, reject) => { let image = new Image(); image.onload = function() { var _a; let canvas = document.createElement("canvas"); canvas.width = image.naturalWidth; canvas.height = image.naturalHeight; (_a = canvas == null ? void 0 : canvas.getContext("2d")) == null ? void 0 : _a.drawImage(image, 0, 0); let result = canvas.toDataURL("image/png"); resolve(result); }; const imgUrl = url; image.setAttribute("crossOrigin", "Anonymous"); image.src = imgUrl; image.onerror = () => { reject(new Error("Images fail to load")); }; }).catch((error) => { throw new Error(error); }); }; function base64toBlob(base64) { if (!base64) return; var arr = base64.split(","), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], { type: mime }); } var downloadPackageImages = async (imgDataList, zipName) => { let imgDataDownLoadList = []; let imgBlobList = []; let imageSuffix = []; let zip = new import_jszip.default(); let img = zip.folder(zipName); try { for (let i2 = 0; i2 < imgDataList.length; i2++) { let src = imgDataList[i2].url; let suffix = src.substring(src.lastIndexOf(".")); let base64ByUrl = await urlToBase64V2(imgDataList[i2].url); if (!base64ByUrl) continue; let blob = base64toBlob(base64ByUrl); imgDataDownLoadList.push(imgDataList[i2]); imgBlobList.push(blob); imageSuffix.push(suffix); } if (imgBlobList.length === 0) throw new Error("The number of pictures is zero !"); if (imgBlobList.length > 0) { for (var i = 0; i < imgBlobList.length; i++) { img == null ? void 0 : img.file( imgDataDownLoadList[i].name + (0, import_lodash_es.get)(imageSuffix, `.${i}`, imageSuffix[0]), // @ts-ignore imgBlobList[i], { base64: true } ); } } zip.generateAsync({ type: "blob" }).then(function(content) { import_file_saver.default.saveAs(content, zipName + ".zip"); }); } catch (error) { throw new Error(error); } }; function getFileSize(size) { if (!size) return ""; var num = 1024; if (size < num) return size + "B"; if (size < Math.pow(num, 2)) return (size / num).toFixed(2) + "K"; if (size < Math.pow(num, 3)) return (size / Math.pow(num, 2)).toFixed(2) + "M"; if (size < Math.pow(num, 4)) return (size / Math.pow(num, 3)).toFixed(2) + "G"; return (size / Math.pow(num, 4)).toFixed(2) + "T"; } var dataURLToBlob = (dataurl) => { const arr = dataurl.split(","); const mime = arr[0].match(/:(.*?);/)[1]; const bstr = atob(arr[1]); let n = bstr.length; const u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], { type: mime }); }; var generateImg = (_imgKey, host = "http://10.0.0.120") => { let imgKey = _imgKey; let imgUrl = ""; if (!imgKey) return ""; if (/(http|https):\/\/([\w.]+\/?)\S*/ig.test(imgKey)) { return imgKey; } try { if ((0, import_utils.matchS3Prefix)(imgKey)) { imgKey = base64DecodeImageKey(imgKey); if (imgKey.endsWith("/")) { const i = imgKey.substring(0, imgKey.length - 1); imgKey = i; } } imgUrl = `${host}/file/${imgKey}`; if (_imgKey.includes("v3")) { imgUrl = `${host}/minio/${imgKey}`; } } catch (error) { console.error(error); imgUrl = ""; } return imgUrl; }; var getImageKey = (imageKey, preFix) => { const splitIndex = preFix || "v1_"; if (imageKey.startsWith(splitIndex)) { return window.atob(imageKey.split(splitIndex)[1]).replace("_", "/"); } else { return imageKey; } }; var getFileByRect = async (img, odRect) => { var _a; let image; if ((0, import_lodash_es.isString)(img)) { const url = generateImg(img); image = await urlToImg(url); } else { image = img; } const commonCanvas = document.createElement("canvas"); commonCanvas.width = odRect.w * image.width; commonCanvas.height = odRect.h * image.height; commonCanvas.style.display = "none"; document.body.appendChild(commonCanvas); const commonCtx = commonCanvas.getContext("2d"); commonCtx == null ? void 0 : commonCtx.translate(-odRect.x * image.width, -odRect.y * image.height); commonCtx == null ? void 0 : commonCtx.drawImage(image, 0, 0); const base64 = commonCanvas.toDataURL("image/jpeg"); const blobData = dataURLToBlob(base64); (_a = commonCanvas.parentNode) == null ? void 0 : _a.removeChild(commonCanvas); const file = new window.File([blobData], `${(/* @__PURE__ */ new Date()).getTime()}`, { type: "image/jpeg" }); return file; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { base64DecodeImageKey, base64toBlob, dataURLToBlob, downloadPackageImages, fileToBase64, generateImg, getBase64ByImage, getBase64ByUrl, getBase64Image, getFileByRect, getFileSize, getImageKey, urlToBase64V2, urlToImg });