UNPKG

@orfeas126/box-ui-elements

Version:
213 lines (210 loc) 7.83 kB
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * * @file Recursively create folder and upload files * @author Box */ import noop from 'lodash/noop'; import { getFileFromEntry } from '../../utils/uploads'; import FolderAPI from '../Folder'; import { STATUS_COMPLETE, STATUS_ERROR, ERROR_CODE_UPLOAD_CHILD_FOLDER_FAILED, ERROR_CODE_ITEM_NAME_IN_USE } from '../../constants'; class FolderUploadNode { /** * [constructor] * * @param {string} name * @param {Function} addFilesToUploadQueue * @param {Function} addFolderToUploadQueue * @returns {void} */ constructor(_name, addFilesToUploadQueue, addFolderToUploadQueue, fileAPIOptions, baseAPIOptions, _entry) { _defineProperty(this, "files", []); _defineProperty(this, "folders", {}); /** * Upload all child folders * * @private * @param {Function} errorCallback * @returns {Promise} */ _defineProperty(this, "uploadChildFolders", async errorCallback => { // $FlowFixMe const folders = Object.values(this.folders); const promises = folders.map(folder => folder.upload(this.folderId, errorCallback)); await Promise.all(promises); }); /** * Create folder and add it to the upload queue * * @private * @param {Function} errorCallback * @param {boolean} isRoot * @returns {Promise} */ _defineProperty(this, "createAndUploadFolder", async (errorCallback, isRoot) => { await this.buildCurrentFolderFromEntry(); let errorEncountered = false; let errorCode = ''; try { const data = await this.createFolder(); this.folderId = data.id; } catch (error) { // @TODO: Handle 429 if (error.code === ERROR_CODE_ITEM_NAME_IN_USE) { this.folderId = error.context_info.conflicts[0].id; } else if (isRoot) { errorCallback(error); } else { // If this is a child folder of the folder being uploaded, this errorCallback will set // an error message on the root folder being uploaded. Set a generic messages saying that a // child has caused the error. The child folder will be tagged with the error message in // the call to this.addFolderToUploadQueue below errorEncountered = true; errorCode = error.code; errorCallback({ code: ERROR_CODE_UPLOAD_CHILD_FOLDER_FAILED }); } } // The root folder has already been added to the upload queue in ContentUploader if (isRoot) { return; } const folderObject = { extension: '', name: this.name, status: STATUS_COMPLETE, isFolder: true, size: 1, progress: 100 }; if (errorEncountered) { folderObject.status = STATUS_ERROR; folderObject.error = { code: errorCode }; } this.addFolderToUploadQueue(folderObject); }); /** * Format files to Array<UploadFileWithAPIOptions> for upload * * @private * @returns {Array<UploadFileWithAPIOptions>} */ _defineProperty(this, "getFormattedFiles", () => this.files.map(file => ({ file, options: _objectSpread(_objectSpread({}, this.fileAPIOptions), {}, { folderId: this.folderId, uploadInitTimestamp: Date.now() }) }))); /** * Create FolderUploadNode instances from entries * * @private * @param {Array<FileSystemFileEntry>} entries * @returns {Promise<any>} */ _defineProperty(this, "createFolderUploadNodesFromEntries", async entries => { await Promise.all(entries.map(async entry => { const { isFile, name } = entry; if (isFile) { const file = await getFileFromEntry(entry); this.files.push(file); return; } this.folders[name] = new FolderUploadNode(name, this.addFilesToUploadQueue, this.addFolderToUploadQueue, this.fileAPIOptions, _objectSpread(_objectSpread({}, this.baseAPIOptions), this.fileAPIOptions), entry); })); }); /** * Recursively read an entry * * @private * @param {DirectoryReader} reader * @param {Function} resolve * @returns {void} */ _defineProperty(this, "readEntry", (reader, resolve) => { reader.readEntries(async entries => { // Quit recursing when there are no remaining entries. if (!entries.length) { resolve(); return; } await this.createFolderUploadNodesFromEntries(entries); this.readEntry(reader, resolve); }, noop); }); /** * Build current folder from entry * * @private * @returns {Promise<any>} */ _defineProperty(this, "buildCurrentFolderFromEntry", () => { if (!this.entry) { return Promise.resolve(); } return new Promise(resolve => { // $FlowFixMe entry is not empty const reader = this.entry.createReader(); this.readEntry(reader, resolve); }); }); /** * Returns the folderId * @returns {string} */ _defineProperty(this, "getFolderId", () => { return this.folderId; }); this.name = _name; this.addFilesToUploadQueue = addFilesToUploadQueue; this.addFolderToUploadQueue = addFolderToUploadQueue; this.fileAPIOptions = fileAPIOptions; this.baseAPIOptions = baseAPIOptions; this.entry = _entry; } /** * Upload a folder * * @public * @param {string} parentFolderId * @param {Function} errorCallback * @param {boolean} isRoot * @returns {Promise} */ async upload(parentFolderId, errorCallback, isRoot = false) { this.parentFolderId = parentFolderId; await this.createAndUploadFolder(errorCallback, isRoot); // Check if folder was successfully created before we attempt to upload its contents. if (this.getFolderId()) { this.addFilesToUploadQueue(this.getFormattedFiles(), noop, true); await this.uploadChildFolders(errorCallback); } } /** * Promisify create folder * * @private * @returns {Promise} */ createFolder() { const folderAPI = new FolderAPI(_objectSpread(_objectSpread({}, this.baseAPIOptions), {}, { id: `folder_${this.parentFolderId}` })); return new Promise((resolve, reject) => { folderAPI.create(this.parentFolderId, this.name, resolve, reject); }); } } export default FolderUploadNode; //# sourceMappingURL=FolderUploadNode.js.map