/** * DevExtreme (ui/file_manager/file_provider/onedrive.js) * Version: 19.1.16 * Build date: Tue Oct 18 2022 * * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) { return typeof obj } : function(obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj }, _typeof(obj) } var _ajax = require("../../../core/utils/ajax"); var _ajax2 = _interopRequireDefault(_ajax); var _deferred = require("../../../core/utils/deferred"); var _common = require("../../../core/utils/common"); var _file_provider = require("./file_provider"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function") } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) { descriptor.writable = true } Object.defineProperty(target, descriptor.key, descriptor) } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) { _defineProperties(Constructor.prototype, protoProps) } if (staticProps) { _defineProperties(Constructor, staticProps) } Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor } function _inherits(subClass, superClass) { if ("function" !== typeof superClass && null !== superClass) { throw new TypeError("Super expression must either be null or a function") } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) { _setPrototypeOf(subClass, superClass) } } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) { o.__proto__ = p; return o }; return _setPrototypeOf(o, p) } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function() { var result, Super = _getPrototypeOf(Derived); if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget) } else { result = Super.apply(this, arguments) } return _possibleConstructorReturn(this, result) } } function _possibleConstructorReturn(self, call) { if (call && ("object" === _typeof(call) || "function" === typeof call)) { return call } else { if (void 0 !== call) { throw new TypeError("Derived constructors may only return object or undefined") } } return _assertThisInitialized(self) } function _assertThisInitialized(self) { if (void 0 === self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called") } return self } function _isNativeReflectConstruct() { if ("undefined" === typeof Reflect || !Reflect.construct) { return false } if (Reflect.construct.sham) { return false } if ("function" === typeof Proxy) { return true } try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {})); return true } catch (e) { return false } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) { return o.__proto__ || Object.getPrototypeOf(o) }; return _getPrototypeOf(o) } var REQUIRED_ITEM_FIELDS = "id,name,folder,lastModifiedDateTime,size,parentReference"; var REST_API_URL = "https://graph.microsoft.com/"; var DRIVE_API_URL = REST_API_URL + "v1.0/drive"; var APP_ROOT_URL = DRIVE_API_URL + "/special/approot"; var OneDriveFileProvider = function(_FileProvider) { _inherits(OneDriveFileProvider, _FileProvider); var _super = _createSuper(OneDriveFileProvider); function OneDriveFileProvider(options) { var _this; _classCallCheck(this, OneDriveFileProvider); options = options || {}; options.dateModifiedExpr = "lastModifiedDateTime"; options.isDirectoryExpr = "folder"; _this = _super.call(this, options); _this._getAccessTokenUrl = options.getAccessTokenUrl || ""; _this._accessToken = ""; _this._accessTokenPromise = null; return _this } _createClass(OneDriveFileProvider, [{ key: "_authorizationString", get: function() { return "Bearer ".concat(this._accessToken) } }, { key: "getItems", value: function(path, itemType) { return this._getItems(path, itemType) } }, { key: "initiateFileUpload", value: function(uploadInfo) { var _this2 = this; var folderPath = uploadInfo.destinationFolder.relativeName; var fileName = uploadInfo.file.name; var customData = uploadInfo.customData; return this._ensureAccessTokenAcquired().then(function() { return _this2._createFile(folderPath, fileName) }).then(function(entry) { return _this2._initiateUploadSession(entry.id).done(function(info) { customData.uploadUrl = info.uploadUrl }) }) } }, { key: "uploadFileChunk", value: function(_ref, _ref2) { var customData = _ref.customData, uploadedBytesCount = _ref.uploadedBytesCount, file = _ref.file; var blob = _ref2.blob, size = _ref2.size; return this._uploadFileChunk(customData.uploadUrl, blob, size, uploadedBytesCount, file.size) } }, { key: "abortFileUpload", value: function(uploadInfo) { var _this3 = this; return this._ensureAccessTokenAcquired().then(function() { return _this3._cancelUploadSession(uploadInfo.customData.uploadUrl) }) } }, { key: "_getItems", value: function(path, itemType) { var _this4 = this; return this._ensureAccessTokenAcquired().then(function() { return _this4._getEntriesByPath(path) }).then(function(entries) { return _this4._convertDataObjectsToFileItems(entries.children, path, itemType) }) } }, { key: "_ensureAccessTokenAcquired", value: function() { var _this5 = this; if (this._accessTokenPromise) { return this._accessTokenPromise } var deferred = new _deferred.Deferred; if (this._accessToken) { deferred.resolve() } else { _ajax2.default.sendRequest({ url: this._getAccessTokenUrl, dataType: "json" }).done(function(_ref3) { var token = _ref3.token; _this5._accessToken = token; _this5._accessTokenPromise = null; deferred.resolve() }) } this._accessTokenPromise = deferred.promise(); return this._accessTokenPromise } }, { key: "_getEntriesByPath", value: function(path) { var itemPath = this._prepareItemRelativePath(path); var queryString = "?$select=".concat(REQUIRED_ITEM_FIELDS, "&$expand=children($select=").concat(REQUIRED_ITEM_FIELDS, ")"); var url = APP_ROOT_URL + itemPath + queryString; return _ajax2.default.sendRequest({ url: url, dataType: "json", cache: false, headers: { Authorization: this._authorizationString } }) } }, { key: "_uploadFileChunk", value: function(uploadUrl, chunkBlob, chunkSize, uploadedSize, totalSize) { var chunkEndPosition = uploadedSize + chunkSize - 1; var contentRange = "bytes ".concat(uploadedSize, "-").concat(chunkEndPosition, "/").concat(totalSize); return _ajax2.default.sendRequest({ url: uploadUrl, method: "PUT", dataType: "json", data: chunkBlob, upload: { onprogress: _common.noop, onloadstart: _common.noop, onabort: _common.noop }, cache: false, headers: { Authorization: this._authorizationString, "Content-Range": contentRange } }) } }, { key: "_initiateUploadSession", value: function(fileId) { var url = "".concat(DRIVE_API_URL, "/items/").concat(fileId, "/createUploadSession"); return _ajax2.default.sendRequest({ url: url, method: "POST", dataType: "json", cache: false, headers: { Authorization: this._authorizationString } }) } }, { key: "_createFile", value: function(folderPath, objectName) { var itemPath = this._prepareItemRelativePath(folderPath); var queryString = "?$select=".concat(REQUIRED_ITEM_FIELDS); var url = APP_ROOT_URL + itemPath + "/children" + queryString; var params = { name: objectName, file: {}, "@microsoft.graph.conflictBehavior": "rename" }; var data = JSON.stringify(params); return _ajax2.default.sendRequest({ url: url, method: "POST", dataType: "json", data: data, cache: false, headers: { Authorization: this._authorizationString, "Content-Type": "application/json" } }) } }, { key: "_cancelUploadSession", value: function(uploadUrl) { return _ajax2.default.sendRequest({ url: uploadUrl, method: "DELETE", dataType: "json", cache: false, headers: { Authorization: this._authorizationString } }) } }, { key: "_prepareItemRelativePath", value: function(path) { return "" === path ? "" : ":/".concat(path, ":") } }, { key: "_hasSubDirs", value: function(dataObj) { return Object.prototype.hasOwnProperty.call(dataObj, "folder") && dataObj.folder.childCount > 0 } }]); return OneDriveFileProvider }(_file_provider.FileProvider); module.exports = OneDriveFileProvider;