@trap_stevo/legendarybuilderproreact-ui
Version:
The legendary UI & utility API that makes your application a legendary application. ~ Created by Steven Compton
272 lines • 12.1 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { useNavigate } from "react-router-dom";
import { getStorage, ref, uploadBytesResumable, listAll, deleteObject, getMetadata, getDownloadURL } from "firebase/storage";
import { HUDUniversalHUDUtilityManager, Base64ToArrayBuffer } from "./HUDUniversalHUDUtilityManager.js";
function GetFileData(_x) {
return _GetFileData.apply(this, arguments);
}
function _GetFileData() {
_GetFileData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(fileRef) {
var itemData, fileDownloadURL, fileName, underscoreIndex, fileNameI, fileNameP, currentFile;
return _regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) switch (_context7.prev = _context7.next) {
case 0:
_context7.prev = 0;
_context7.next = 3;
return getMetadata(fileRef);
case 3:
itemData = _context7.sent;
_context7.next = 6;
return getDownloadURL(fileRef);
case 6:
fileDownloadURL = _context7.sent;
fileName = fileRef.name;
underscoreIndex = fileName.indexOf("_");
if (!(underscoreIndex !== -1)) {
_context7.next = 14;
break;
}
fileNameI = fileName.substring(0, underscoreIndex);
fileNameP = fileName.substring(underscoreIndex + 1);
currentFile = {
contentType: itemData.contentType,
size: parseInt(itemData.size),
status: "Uploaded",
name: fileNameP,
id: fileName,
dateCreated: HUDUniversalHUDUtilityManager.ConvertUTCDateToDateDisplay(parseInt(fileNameI), true),
originPath: fileRef.fullPath,
url: fileDownloadURL
};
return _context7.abrupt("return", currentFile);
case 14:
return _context7.abrupt("return", null);
case 17:
_context7.prev = 17;
_context7.t0 = _context7["catch"](0);
return _context7.abrupt("return", null);
case 20:
case "end":
return _context7.stop();
}
}, _callee7, null, [[0, 17]]);
}));
return _GetFileData.apply(this, arguments);
}
;
var HUDUniversalDataManager = {
UploadFiles: function () {
var _UploadFiles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(hudStorage, files, directoryPath, onProgress) {
var uploadPromises;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
uploadPromises = files.map(function (file, index) {
return new Promise( /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(resolve, reject) {
var uploadedFile;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return HUDUniversalDataManager.UploadFile(hudStorage, file, index, directoryPath + file.id, onProgress);
case 2:
uploadedFile = _context.sent;
resolve(uploadedFile);
case 4:
case "end":
return _context.stop();
}
}, _callee);
}));
return function (_x6, _x7) {
return _ref.apply(this, arguments);
};
}());
});
return _context2.abrupt("return", Promise.all(uploadPromises).then(function (uploadedFiles) {
console.log("Uploaded all files!");
return uploadedFiles;
})["catch"](function (error) {
console.error("Did not upload files: ", error);
return [];
}));
case 2:
case "end":
return _context2.stop();
}
}, _callee2);
}));
function UploadFiles(_x2, _x3, _x4, _x5) {
return _UploadFiles.apply(this, arguments);
}
return UploadFiles;
}(),
UploadFile: function () {
var _UploadFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(hudStorage, file, fileIndex, filePath, onProgress) {
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
return _context3.abrupt("return", new Promise(function (resolve, reject) {
try {
var storageRef = ref(hudStorage, filePath);
var fileBlob = new Blob([Base64ToArrayBuffer(file.data)], {
type: file.type
});
var uploadTask = uploadBytesResumable(storageRef, fileBlob);
uploadTask.on('state_changed', function (snapshot) {
var progress = snapshot.totalBytes !== 0 ? snapshot.bytesTransferred / snapshot.totalBytes * 100 : 0;
onProgress(progress, file.id, fileIndex);
}, function (error) {
console.error("Did not upload file: ", error);
}, function () {
console.log("Upload successful!");
getDownloadURL(uploadTask.snapshot.ref).then(function (fileDownloadURL) {
file.contentType = file.type;
file.status = "Uploaded";
file.url = fileDownloadURL;
resolve(file);
});
});
return;
} catch (error) {
console.log("Did not upload file " + file.name, error);
resolve(null);
return;
}
}));
case 1:
case "end":
return _context3.stop();
}
}, _callee3);
}));
function UploadFile(_x8, _x9, _x10, _x11, _x12) {
return _UploadFile.apply(this, arguments);
}
return UploadFile;
}(),
GetFiles: function () {
var _GetFiles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(hudStorage, directoryPath) {
var dirRef, files, listResult, _iterator, _step, itemRef, currentFile;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) switch (_context4.prev = _context4.next) {
case 0:
_context4.prev = 0;
dirRef = ref(hudStorage, directoryPath);
files = [];
_context4.next = 5;
return listAll(dirRef);
case 5:
listResult = _context4.sent;
_iterator = _createForOfIteratorHelper(listResult.items);
_context4.prev = 7;
_iterator.s();
case 9:
if ((_step = _iterator.n()).done) {
_context4.next = 17;
break;
}
itemRef = _step.value;
_context4.next = 13;
return GetFileData(itemRef);
case 13:
currentFile = _context4.sent;
if (currentFile !== null) {
files.push(currentFile);
}
case 15:
_context4.next = 9;
break;
case 17:
_context4.next = 22;
break;
case 19:
_context4.prev = 19;
_context4.t0 = _context4["catch"](7);
_iterator.e(_context4.t0);
case 22:
_context4.prev = 22;
_iterator.f();
return _context4.finish(22);
case 25:
return _context4.abrupt("return", files);
case 28:
_context4.prev = 28;
_context4.t1 = _context4["catch"](0);
console.error("Did not retrieve files: ", _context4.t1);
return _context4.abrupt("return", []);
case 32:
case "end":
return _context4.stop();
}
}, _callee4, null, [[0, 28], [7, 19, 22, 25]]);
}));
function GetFiles(_x13, _x14) {
return _GetFiles.apply(this, arguments);
}
return GetFiles;
}(),
GetFile: function () {
var _GetFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(hudStorage, filePath) {
var fileRef, currentFile;
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) switch (_context5.prev = _context5.next) {
case 0:
_context5.prev = 0;
fileRef = ref(hudStorage, filePath);
_context5.next = 4;
return GetFileData(fileRef);
case 4:
currentFile = _context5.sent;
return _context5.abrupt("return", currentFile);
case 8:
_context5.prev = 8;
_context5.t0 = _context5["catch"](0);
console.log("Did not get file " + filePath, _context5.t0);
return _context5.abrupt("return", null);
case 12:
case "end":
return _context5.stop();
}
}, _callee5, null, [[0, 8]]);
}));
function GetFile(_x15, _x16) {
return _GetFile.apply(this, arguments);
}
return GetFile;
}(),
DeleteFile: function () {
var _DeleteFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(hudStorage, filePath) {
var fileRef;
return _regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) switch (_context6.prev = _context6.next) {
case 0:
_context6.prev = 0;
fileRef = ref(hudStorage, filePath);
_context6.next = 4;
return deleteObject(fileRef);
case 4:
return _context6.abrupt("return", true);
case 7:
_context6.prev = 7;
_context6.t0 = _context6["catch"](0);
console.log("Did not get file " + filePath, _context6.t0);
return _context6.abrupt("return", false);
case 11:
case "end":
return _context6.stop();
}
}, _callee6, null, [[0, 7]]);
}));
function DeleteFile(_x17, _x18) {
return _DeleteFile.apply(this, arguments);
}
return DeleteFile;
}()
};
export { HUDUniversalDataManager };