@trap_stevo/legendarybuilderproreact-ui
Version:
The legendary UI & utility API that makes your application a legendary application. ~ Created by Steven Compton
149 lines • 6.18 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _regeneratorRuntime from "@babel/runtime/regenerator";
var fileTypeMapping = {
"video": ["mp4", "mkv", "avi"],
"audio": ["mp3", "wav", "aac"],
"text": ["txt", "md", "json"],
"image": ["png", "jpg", "jpeg", "gif"],
"document": ["pdf"],
"binary": ["bin", "exe"]
};
var mimeTypeMapping = {
"video": "video/",
"audio": "audio/",
"text": "text/",
"image": "image/",
"document": "application/pdf",
"app-binary": "application/octet-stream",
"binary": "binary/octet-stream"
};
export function DetermineFileType(file) {
var contentType = file.contentType,
name = file.name;
if (!contentType) {
return "unknown";
}
if (contentType === mimeTypeMapping["binary"] && name) {
var fileExtension = name.split(".").pop().toLowerCase();
for (var _i = 0, _Object$entries = Object.entries(fileTypeMapping); _i < _Object$entries.length; _i++) {
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
type = _Object$entries$_i[0],
extensions = _Object$entries$_i[1];
if (extensions.includes(fileExtension)) {
return type;
}
}
}
for (var _i2 = 0, _Object$entries2 = Object.entries(mimeTypeMapping); _i2 < _Object$entries2.length; _i2++) {
var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
_type = _Object$entries2$_i[0],
mimePrefix = _Object$entries2$_i[1];
if (contentType.startsWith(mimePrefix)) {
return _type;
}
}
return "unknown";
}
;
export function Download(_x) {
return _Download.apply(this, arguments);
}
function _Download() {
_Download = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref) {
var data, _ref$fileName, fileName, _ref$fileType, fileType, _ref$extType, extType, _ref$headers, headers, _ref$method, method, _ref$body, body, _ref$params, params, _ref$query, query, fileTypeMapping, fullFileType, url, options, queryString, blob, response, base64Data, byteCharacters, byteNumbers, i, byteArray, a;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
data = _ref.data, _ref$fileName = _ref.fileName, fileName = _ref$fileName === void 0 ? "file" : _ref$fileName, _ref$fileType = _ref.fileType, fileType = _ref$fileType === void 0 ? "txt" : _ref$fileType, _ref$extType = _ref.extType, extType = _ref$extType === void 0 ? "txt" : _ref$extType, _ref$headers = _ref.headers, headers = _ref$headers === void 0 ? {} : _ref$headers, _ref$method = _ref.method, method = _ref$method === void 0 ? "GET" : _ref$method, _ref$body = _ref.body, body = _ref$body === void 0 ? null : _ref$body, _ref$params = _ref.params, params = _ref$params === void 0 ? {} : _ref$params, _ref$query = _ref.query, query = _ref$query === void 0 ? {} : _ref$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;
_context.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")))) {
_context.next = 20;
break;
}
_context.next = 12;
return fetch(url, options);
case 12:
response = _context.sent;
if (response.ok) {
_context.next = 15;
break;
}
throw new Error("Did not download file | ".concat(fileName, ".").concat(fileType, ": ").concat(response.statusText));
case 15:
_context.next = 17;
return response.blob();
case 17:
blob = _context.sent;
_context.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);
_context.next = 31;
break;
case 28:
_context.prev = 28;
_context.t0 = _context["catch"](3);
console.error("Did not download file | ".concat(fileName, ".").concat(extType ? extType : fileType, ": "), _context.t0);
case 31:
return _context.abrupt("return");
case 32:
case "end":
return _context.stop();
}
}, _callee, null, [[3, 28]]);
}));
return _Download.apply(this, arguments);
}
;