UNPKG

@trap_stevo/legendarybuilderproreact-ui

Version:

The legendary UI & utility API that makes your application a legendary application. ~ Created by Steven Compton

590 lines 20.6 kB
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _regeneratorRuntime from "@babel/runtime/regenerator"; export var extensionToMime = { "mp4": "video/mp4", "m4v": "video/mp4", "mkv": "video/x-matroska", "avi": "video/x-msvideo", "mov": "video/quicktime", "webm": "video/webm", "mp3": "audio/mpeg", "m4a": "audio/mp4", "wav": "audio/wav", "aac": "audio/aac", "flac": "audio/flac", "ogg": "audio/ogg", "png": "image/png", "jpg": "image/jpeg", "jpeg": "image/jpeg", "gif": "image/gif", "bmp": "image/bmp", "webp": "image/webp", "svg": "image/svg+xml", "heic": "image/heic", "heif": "image/heif", "txt": "text/plain", "md": "text/markdown", "json": "application/json", "csv": "text/csv", "log": "text/plain", "js": "text/javascript", "ts": "application/typescript", "jsx": "text/jsx", "tsx": "text/tsx", "html": "text/html", "css": "text/css", "py": "text/x-python", "java": "text/x-java-source", "c": "text/x-c", "cpp": "text/x-c++src", "cs": "text/x-csharp", "sh": "text/x-shellscript", "xml": "application/xml", "yml": "text/yaml", "yaml": "text/yaml", "pdf": "application/pdf", "doc": "application/msword", "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "xls": "application/vnd.ms-excel", "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "ppt": "application/vnd.ms-powerpoint", "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", "odt": "application/vnd.oasis.opendocument.text", "ods": "application/vnd.oasis.opendocument.spreadsheet", "odp": "application/vnd.oasis.opendocument.presentation", "zip": "application/zip", "rar": "application/vnd.rar", "7z": "application/x-7z-compressed", "tar": "application/x-tar", "gz": "application/gzip", "bz2": "application/x-bzip2", "bin": "application/octet-stream", "exe": "application/x-msdownload", "dll": "application/x-msdownload", "so": "application/octet-stream", "deb": "application/vnd.debian.binary-package", "rpm": "application/x-rpm", "ttf": "font/ttf", "otf": "font/otf", "woff": "font/woff", "woff2": "font/woff2", "obj": "application/octet-stream", "fbx": "application/octet-stream", "glb": "model/gltf-binary", "gltf": "model/gltf+json", "stl": "model/stl" }; export var mimeToExtension = Object.entries(extensionToMime).reduce(function (acc, _ref) { var _ref2 = _slicedToArray(_ref, 2), ext = _ref2[0], mime = _ref2[1]; if (!acc[mime]) { acc[mime] = ext; } return acc; }, {}); var fileTypeMapping = { video: ["mp4", "mkv", "avi", "mov", "webm"], audio: ["mp3", "wav", "aac", "flac", "ogg"], image: ["png", "jpg", "jpeg", "gif", "bmp", "webp", "svg"], text: ["txt", "md", "json", "csv", "log"], code: ["js", "ts", "jsx", "tsx", "html", "css", "py", "java", "c", "cpp", "cs", "sh", "xml", "yml", "yaml"], document: ["pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "odt", "ods", "odp"], archive: ["zip", "rar", "7z", "tar", "gz", "bz2"], binary: ["bin", "exe", "dll", "so", "deb", "rpm"], font: ["ttf", "otf", "woff", "woff2"], model3d: ["obj", "fbx", "glb", "gltf", "stl"] }; var mimeTypeMapping = { "video": "video/", "audio": "audio/", "text": "text/", "image": "image/", "model3d": "model/", "code": "text/x-", "font": "font/", "document": ["application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"], "app-binary": "application/octet-stream", "binary": "binary/octet-stream" }; export function GetFileExtension(input) { if (typeof input !== "string" || !input.trim()) { return null; } var s = input.trim(); if (s.startsWith("data:")) { var comma = s.indexOf(","); var semi = s.indexOf(";"); var end = semi > -1 ? semi : comma > -1 ? comma : s.length; var mime = s.slice(5, end).toLowerCase(); return mimeToExtension[mime] || null; } if (s.includes("/") && !s.includes("\\") && !s.includes(".")) { var _mime = s.split(";")[0].toLowerCase(); return mimeToExtension[_mime] || null; } var base = s.split("/").pop(); if (base && base.includes(".")) { var ext = base.split(".").pop().toLowerCase(); return ext || null; } return null; } ; export function GetMimeType(input) { if (!input) { return "application/octet-stream"; } if (typeof input === "string" && input.includes("/")) { return input.split(";")[0].trim(); } var ext = (typeof input === "string" ? input : "").toLowerCase(); return extensionToMime[ext] || "application/octet-stream"; } ; export function GetFilenameFromURL(url) { try { var u = new URL(url); var raw = (u.pathname.split("/").pop() || "").split("?")[0]; return decodeURIComponent(raw); } catch (_unused) { return null; } } ; export function GetExtensionFromURL(urlOrName) { if (!urlOrName) { return null; } try { var s = String(urlOrName); var base = s.includes("/") ? s.split("?")[0].split("#")[0].split("/").pop() : s; if (!base || !base.includes(".")) { return null; } return base.split(".").pop().toLowerCase(); } catch (_unused2) { return null; } } ; export function NormalizeMime(mime) { if (!mime || typeof mime !== "string") { return "application/octet-stream"; } var clean = mime.split(";")[0].trim().toLowerCase(); var ext = mimeToExtension[clean] ? mimeToExtension[clean] : null; if (ext) { return clean; } return clean || "application/octet-stream"; } ; export function CategorizeMime(mime) { var m = (mime || "").toLowerCase(); if (m.startsWith("image/")) { return "image"; } if (m.startsWith("video/")) { return "video"; } if (m.startsWith("audio/")) { return "audio"; } if (m === "application/pdf") { return "document"; } if (m.startsWith("text/") || m === "application/json" || m === "application/xml") { return "text"; } if (m.startsWith("model/")) { return "model3d"; } if (m.startsWith("font/")) { return "font"; } return "binary"; } ; export function PreviewableMime(mime) { var m = (mime || "").toLowerCase(); return m.startsWith("image/") || m.startsWith("video/") || m.startsWith("audio/") || m === "application/pdf" || m.startsWith("text/") || m === "application/json" || m === "application/xml"; } ; function likelyPresignedS3(url) { try { var u = new URL(url); var host = u.hostname.toLowerCase(); var s3Host = host === "s3.amazonaws.com" || host.endsWith(".s3.amazonaws.com") || host.includes(".s3.") || host === "s3.us-east-1.amazonaws.com"; var qp = u.searchParams; var hasSig = qp.has("X-Amz-Algorithm") || qp.has("X-Amz-Signature") || qp.has("x-amz-signature"); return s3Host && hasSig; } catch (_unused3) { return false; } } export function DetectContentTypeFromURL(_x) { return _DetectContentTypeFromURL.apply(this, arguments); } function _DetectContentTypeFromURL() { _DetectContentTypeFromURL = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(url) { var _ref3, _ref3$head, head, _ref3$timeoutMs, timeoutMs, extractSubtype, u, override, _mime2, _subtype, presigned, controller, tid, res, ct, _mime3, _subtype2, _res, _ct, _mime4, _subtype3, ext, mime, subtype, _args = arguments; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _ref3 = _args.length > 1 && _args[1] !== undefined ? _args[1] : {}, _ref3$head = _ref3.head, head = _ref3$head === void 0 ? true : _ref3$head, _ref3$timeoutMs = _ref3.timeoutMs, timeoutMs = _ref3$timeoutMs === void 0 ? 5000 : _ref3$timeoutMs; extractSubtype = function extractSubtype(mime) { if (!mime || typeof mime !== "string") { return null; } var base = mime.split(";")[0].trim(); var parts = base.split("/"); return parts.length > 1 ? parts[1] : parts[0]; }; _context.prev = 2; u = new URL(url); override = u.searchParams.get("response-content-type") || u.searchParams.get("ResponseContentType") || u.searchParams.get("responseContentType"); if (!override) { _context.next = 9; break; } _mime2 = NormalizeMime(override); _subtype = extractSubtype(_mime2); return _context.abrupt("return", { mime: _mime2, category: CategorizeMime(_mime2), subtype: _subtype, via: "query" }); case 9: _context.next = 13; break; case 11: _context.prev = 11; _context.t0 = _context["catch"](2); case 13: presigned = likelyPresignedS3(url); if (!(typeof fetch === "function")) { _context.next = 47; break; } controller = typeof AbortController !== "undefined" ? new AbortController() : null; tid = controller ? setTimeout(function () { return controller.abort(); }, timeoutMs) : null; _context.prev = 17; if (!presigned) { _context.next = 30; break; } _context.next = 21; return fetch(url, { method: "GET", headers: { Range: "bytes=0-0" }, signal: controller === null || controller === void 0 ? void 0 : controller.signal, mode: "cors", redirect: "follow", cache: "no-store" }); case 21: res = _context.sent; if (!(res && res.ok)) { _context.next = 28; break; } ct = res.headers.get("content-type") || res.headers.get("Content-Type"); if (!ct) { _context.next = 28; break; } _mime3 = NormalizeMime(ct); _subtype2 = extractSubtype(_mime3); return _context.abrupt("return", { mime: _mime3, category: CategorizeMime(_mime3), subtype: _subtype2, via: "get-range" }); case 28: _context.next = 40; break; case 30: if (!head) { _context.next = 40; break; } _context.next = 33; return fetch(url, { method: "HEAD", signal: controller === null || controller === void 0 ? void 0 : controller.signal, mode: "cors", redirect: "follow", cache: "no-store" }); case 33: _res = _context.sent; if (!(_res && _res.ok)) { _context.next = 40; break; } _ct = _res.headers.get("content-type") || _res.headers.get("Content-Type"); if (!_ct) { _context.next = 40; break; } _mime4 = NormalizeMime(_ct); _subtype3 = extractSubtype(_mime4); return _context.abrupt("return", { mime: _mime4, category: CategorizeMime(_mime4), subtype: _subtype3, via: "head" }); case 40: _context.next = 44; break; case 42: _context.prev = 42; _context.t1 = _context["catch"](17); case 44: _context.prev = 44; if (tid) { clearTimeout(tid); } return _context.finish(44); case 47: ext = GetExtensionFromURL(url); mime = ext ? extensionToMime[ext] || "application/octet-stream" : "application/octet-stream"; subtype = extractSubtype(mime); return _context.abrupt("return", { mime: mime, category: CategorizeMime(mime), subtype: subtype, via: "ext" }); case 51: case "end": return _context.stop(); } }, _callee, null, [[2, 11], [17, 42, 44, 47]]); })); return _DetectContentTypeFromURL.apply(this, arguments); } ; export function ResolveContentType(_x2) { return _ResolveContentType.apply(this, arguments); } function _ResolveContentType() { _ResolveContentType = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(_ref4) { var contentType, nameOrUrl, _ref4$allowHead, allowHead, _mime5, _yield$DetectContentT, mime, category, via; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: contentType = _ref4.contentType, nameOrUrl = _ref4.nameOrUrl, _ref4$allowHead = _ref4.allowHead, allowHead = _ref4$allowHead === void 0 ? true : _ref4$allowHead; if (!contentType) { _context2.next = 4; break; } _mime5 = NormalizeMime(contentType); return _context2.abrupt("return", { mime: _mime5, category: CategorizeMime(_mime5), source: "explicit" }); case 4: _context2.next = 6; return DetectContentTypeFromURL(nameOrUrl || "", { head: allowHead }); case 6: _yield$DetectContentT = _context2.sent; mime = _yield$DetectContentT.mime; category = _yield$DetectContentT.category; via = _yield$DetectContentT.via; return _context2.abrupt("return", { mime: mime, category: category, source: via }); case 11: case "end": return _context2.stop(); } }, _callee2); })); return _ResolveContentType.apply(this, arguments); } ; export function FileNameContainsExtension(name, mime) { var safeName = (name || "file").split("?")[0].split("#")[0]; var wantExt = mimeToExtension[NormalizeMime(mime)] || GetExtensionFromURL(safeName) || "bin"; var containsExt = GetExtensionFromURL(safeName); if (!containsExt) { return "".concat(safeName, ".").concat(wantExt); } if (extensionToMime[containsExt] && extensionToMime[containsExt].toLowerCase() === NormalizeMime(mime)) { return safeName; } return safeName; } ; export function DetermineFileType(file) { var _name$split$pop; var contentType = file.contentType, name = file.name; var fileExtension = name === null || name === void 0 || (_name$split$pop = name.split(".").pop()) === null || _name$split$pop === void 0 ? void 0 : _name$split$pop.toLowerCase(); if (contentType) { for (var _i = 0, _Object$entries = Object.entries(mimeTypeMapping); _i < _Object$entries.length; _i++) { var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), type = _Object$entries$_i[0], mimePrefix = _Object$entries$_i[1]; if (Array.isArray(mimePrefix)) { if (mimePrefix.includes(contentType)) { return type; } } else if (contentType.startsWith(mimePrefix)) { return type; } } } if (fileExtension) { for (var _i2 = 0, _Object$entries2 = Object.entries(fileTypeMapping); _i2 < _Object$entries2.length; _i2++) { var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2), _type = _Object$entries2$_i[0], extensions = _Object$entries2$_i[1]; if (extensions.includes(fileExtension)) { return _type; } } } return "unknown"; } ; export function Download(_x3) { return _Download.apply(this, arguments); } function _Download() { _Download = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(_ref5) { var data, _ref5$fileName, fileName, _ref5$fileType, fileType, _ref5$extType, extType, _ref5$headers, headers, _ref5$method, method, _ref5$body, body, _ref5$params, params, _ref5$query, query, fileTypeMapping, fullFileType, url, options, queryString, blob, response, base64Data, byteCharacters, byteNumbers, i, byteArray, a; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: data = _ref5.data, _ref5$fileName = _ref5.fileName, fileName = _ref5$fileName === void 0 ? "file" : _ref5$fileName, _ref5$fileType = _ref5.fileType, fileType = _ref5$fileType === void 0 ? "txt" : _ref5$fileType, _ref5$extType = _ref5.extType, extType = _ref5$extType === void 0 ? "txt" : _ref5$extType, _ref5$headers = _ref5.headers, headers = _ref5$headers === void 0 ? {} : _ref5$headers, _ref5$method = _ref5.method, method = _ref5$method === void 0 ? "GET" : _ref5$method, _ref5$body = _ref5.body, body = _ref5$body === void 0 ? null : _ref5$body, _ref5$params = _ref5.params, params = _ref5$params === void 0 ? {} : _ref5$params, _ref5$query = _ref5.query, query = _ref5$query === void 0 ? {} : _ref5$query; fileTypeMapping = { "app-bin": "application/octet-stream", "bin": "binary/octet-stream", "txt": "text/plain", "json": "application/json", "mp4": "video/mp4", "png": "image/png", "jpg": "image/jpeg", "pdf": "application/pdf", "csv": "text/csv", "html": "text/html" }; fullFileType = fileTypeMapping[fileType] || fileType; _context3.prev = 3; url = data; options = { method: method, headers: headers }; if (Object.keys(params).length > 0) { Object.keys(params).forEach(function (key) { url = url.replace(":".concat(key), encodeURIComponent(params[key])); }); } if (Object.keys(query).length > 0) { queryString = new URLSearchParams(query).toString(); url += (url.includes("?") ? "&" : "?") + queryString; } if (body && method !== "GET" && method !== "HEAD") { options.body = JSON.stringify(body); } if (!(typeof data === "string" && (data.startsWith("http") || data.startsWith("https")))) { _context3.next = 20; break; } _context3.next = 12; return fetch(url, options); case 12: response = _context3.sent; if (response.ok) { _context3.next = 15; break; } throw new Error("Did not download file | ".concat(fileName, ".").concat(fileType, ": ").concat(response.statusText)); case 15: _context3.next = 17; return response.blob(); case 17: blob = _context3.sent; _context3.next = 21; break; case 20: if (typeof data === "string" && data.startsWith("data:")) { base64Data = data.split(",")[1]; byteCharacters = atob(base64Data); byteNumbers = new Array(byteCharacters.length); for (i = 0; i < byteCharacters.length; i++) { byteNumbers[i] = byteCharacters.charCodeAt(i); } byteArray = new Uint8Array(byteNumbers); blob = new Blob([byteArray], { type: fullFileType }); } else { blob = new Blob([data], { type: fullFileType }); } case 21: a = document.createElement("a"); a.href = URL.createObjectURL(blob); a.download = "".concat(fileName, ".").concat(extType ? extType : fileType); a.click(); URL.revokeObjectURL(a.href); _context3.next = 31; break; case 28: _context3.prev = 28; _context3.t0 = _context3["catch"](3); console.error("Did not download file | ".concat(fileName, ".").concat(extType ? extType : fileType, ": "), _context3.t0); case 31: return _context3.abrupt("return"); case 32: case "end": return _context3.stop(); } }, _callee3, null, [[3, 28]]); })); return _Download.apply(this, arguments); } ;