onedrive.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * DevExtreme (ui/file_manager/file_provider/onedrive.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. function _typeof(obj) {
  11. "@babel/helpers - typeof";
  12. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
  13. return typeof obj
  14. } : function(obj) {
  15. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
  16. }, _typeof(obj)
  17. }
  18. var _ajax = require("../../../core/utils/ajax");
  19. var _ajax2 = _interopRequireDefault(_ajax);
  20. var _deferred = require("../../../core/utils/deferred");
  21. var _common = require("../../../core/utils/common");
  22. var _file_provider = require("./file_provider");
  23. function _interopRequireDefault(obj) {
  24. return obj && obj.__esModule ? obj : {
  25. "default": obj
  26. }
  27. }
  28. function _classCallCheck(instance, Constructor) {
  29. if (!(instance instanceof Constructor)) {
  30. throw new TypeError("Cannot call a class as a function")
  31. }
  32. }
  33. function _defineProperties(target, props) {
  34. for (var i = 0; i < props.length; i++) {
  35. var descriptor = props[i];
  36. descriptor.enumerable = descriptor.enumerable || false;
  37. descriptor.configurable = true;
  38. if ("value" in descriptor) {
  39. descriptor.writable = true
  40. }
  41. Object.defineProperty(target, descriptor.key, descriptor)
  42. }
  43. }
  44. function _createClass(Constructor, protoProps, staticProps) {
  45. if (protoProps) {
  46. _defineProperties(Constructor.prototype, protoProps)
  47. }
  48. if (staticProps) {
  49. _defineProperties(Constructor, staticProps)
  50. }
  51. Object.defineProperty(Constructor, "prototype", {
  52. writable: false
  53. });
  54. return Constructor
  55. }
  56. function _inherits(subClass, superClass) {
  57. if ("function" !== typeof superClass && null !== superClass) {
  58. throw new TypeError("Super expression must either be null or a function")
  59. }
  60. subClass.prototype = Object.create(superClass && superClass.prototype, {
  61. constructor: {
  62. value: subClass,
  63. writable: true,
  64. configurable: true
  65. }
  66. });
  67. Object.defineProperty(subClass, "prototype", {
  68. writable: false
  69. });
  70. if (superClass) {
  71. _setPrototypeOf(subClass, superClass)
  72. }
  73. }
  74. function _setPrototypeOf(o, p) {
  75. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
  76. o.__proto__ = p;
  77. return o
  78. };
  79. return _setPrototypeOf(o, p)
  80. }
  81. function _createSuper(Derived) {
  82. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  83. return function() {
  84. var result, Super = _getPrototypeOf(Derived);
  85. if (hasNativeReflectConstruct) {
  86. var NewTarget = _getPrototypeOf(this).constructor;
  87. result = Reflect.construct(Super, arguments, NewTarget)
  88. } else {
  89. result = Super.apply(this, arguments)
  90. }
  91. return _possibleConstructorReturn(this, result)
  92. }
  93. }
  94. function _possibleConstructorReturn(self, call) {
  95. if (call && ("object" === _typeof(call) || "function" === typeof call)) {
  96. return call
  97. } else {
  98. if (void 0 !== call) {
  99. throw new TypeError("Derived constructors may only return object or undefined")
  100. }
  101. }
  102. return _assertThisInitialized(self)
  103. }
  104. function _assertThisInitialized(self) {
  105. if (void 0 === self) {
  106. throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
  107. }
  108. return self
  109. }
  110. function _isNativeReflectConstruct() {
  111. if ("undefined" === typeof Reflect || !Reflect.construct) {
  112. return false
  113. }
  114. if (Reflect.construct.sham) {
  115. return false
  116. }
  117. if ("function" === typeof Proxy) {
  118. return true
  119. }
  120. try {
  121. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
  122. return true
  123. } catch (e) {
  124. return false
  125. }
  126. }
  127. function _getPrototypeOf(o) {
  128. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) {
  129. return o.__proto__ || Object.getPrototypeOf(o)
  130. };
  131. return _getPrototypeOf(o)
  132. }
  133. var REQUIRED_ITEM_FIELDS = "id,name,folder,lastModifiedDateTime,size,parentReference";
  134. var REST_API_URL = "https://graph.microsoft.com/";
  135. var DRIVE_API_URL = REST_API_URL + "v1.0/drive";
  136. var APP_ROOT_URL = DRIVE_API_URL + "/special/approot";
  137. var OneDriveFileProvider = function(_FileProvider) {
  138. _inherits(OneDriveFileProvider, _FileProvider);
  139. var _super = _createSuper(OneDriveFileProvider);
  140. function OneDriveFileProvider(options) {
  141. var _this;
  142. _classCallCheck(this, OneDriveFileProvider);
  143. options = options || {};
  144. options.dateModifiedExpr = "lastModifiedDateTime";
  145. options.isDirectoryExpr = "folder";
  146. _this = _super.call(this, options);
  147. _this._getAccessTokenUrl = options.getAccessTokenUrl || "";
  148. _this._accessToken = "";
  149. _this._accessTokenPromise = null;
  150. return _this
  151. }
  152. _createClass(OneDriveFileProvider, [{
  153. key: "_authorizationString",
  154. get: function() {
  155. return "Bearer ".concat(this._accessToken)
  156. }
  157. }, {
  158. key: "getItems",
  159. value: function(path, itemType) {
  160. return this._getItems(path, itemType)
  161. }
  162. }, {
  163. key: "initiateFileUpload",
  164. value: function(uploadInfo) {
  165. var _this2 = this;
  166. var folderPath = uploadInfo.destinationFolder.relativeName;
  167. var fileName = uploadInfo.file.name;
  168. var customData = uploadInfo.customData;
  169. return this._ensureAccessTokenAcquired().then(function() {
  170. return _this2._createFile(folderPath, fileName)
  171. }).then(function(entry) {
  172. return _this2._initiateUploadSession(entry.id).done(function(info) {
  173. customData.uploadUrl = info.uploadUrl
  174. })
  175. })
  176. }
  177. }, {
  178. key: "uploadFileChunk",
  179. value: function(_ref, _ref2) {
  180. var customData = _ref.customData,
  181. uploadedBytesCount = _ref.uploadedBytesCount,
  182. file = _ref.file;
  183. var blob = _ref2.blob,
  184. size = _ref2.size;
  185. return this._uploadFileChunk(customData.uploadUrl, blob, size, uploadedBytesCount, file.size)
  186. }
  187. }, {
  188. key: "abortFileUpload",
  189. value: function(uploadInfo) {
  190. var _this3 = this;
  191. return this._ensureAccessTokenAcquired().then(function() {
  192. return _this3._cancelUploadSession(uploadInfo.customData.uploadUrl)
  193. })
  194. }
  195. }, {
  196. key: "_getItems",
  197. value: function(path, itemType) {
  198. var _this4 = this;
  199. return this._ensureAccessTokenAcquired().then(function() {
  200. return _this4._getEntriesByPath(path)
  201. }).then(function(entries) {
  202. return _this4._convertDataObjectsToFileItems(entries.children, path, itemType)
  203. })
  204. }
  205. }, {
  206. key: "_ensureAccessTokenAcquired",
  207. value: function() {
  208. var _this5 = this;
  209. if (this._accessTokenPromise) {
  210. return this._accessTokenPromise
  211. }
  212. var deferred = new _deferred.Deferred;
  213. if (this._accessToken) {
  214. deferred.resolve()
  215. } else {
  216. _ajax2.default.sendRequest({
  217. url: this._getAccessTokenUrl,
  218. dataType: "json"
  219. }).done(function(_ref3) {
  220. var token = _ref3.token;
  221. _this5._accessToken = token;
  222. _this5._accessTokenPromise = null;
  223. deferred.resolve()
  224. })
  225. }
  226. this._accessTokenPromise = deferred.promise();
  227. return this._accessTokenPromise
  228. }
  229. }, {
  230. key: "_getEntriesByPath",
  231. value: function(path) {
  232. var itemPath = this._prepareItemRelativePath(path);
  233. var queryString = "?$select=".concat(REQUIRED_ITEM_FIELDS, "&$expand=children($select=").concat(REQUIRED_ITEM_FIELDS, ")");
  234. var url = APP_ROOT_URL + itemPath + queryString;
  235. return _ajax2.default.sendRequest({
  236. url: url,
  237. dataType: "json",
  238. cache: false,
  239. headers: {
  240. Authorization: this._authorizationString
  241. }
  242. })
  243. }
  244. }, {
  245. key: "_uploadFileChunk",
  246. value: function(uploadUrl, chunkBlob, chunkSize, uploadedSize, totalSize) {
  247. var chunkEndPosition = uploadedSize + chunkSize - 1;
  248. var contentRange = "bytes ".concat(uploadedSize, "-").concat(chunkEndPosition, "/").concat(totalSize);
  249. return _ajax2.default.sendRequest({
  250. url: uploadUrl,
  251. method: "PUT",
  252. dataType: "json",
  253. data: chunkBlob,
  254. upload: {
  255. onprogress: _common.noop,
  256. onloadstart: _common.noop,
  257. onabort: _common.noop
  258. },
  259. cache: false,
  260. headers: {
  261. Authorization: this._authorizationString,
  262. "Content-Range": contentRange
  263. }
  264. })
  265. }
  266. }, {
  267. key: "_initiateUploadSession",
  268. value: function(fileId) {
  269. var url = "".concat(DRIVE_API_URL, "/items/").concat(fileId, "/createUploadSession");
  270. return _ajax2.default.sendRequest({
  271. url: url,
  272. method: "POST",
  273. dataType: "json",
  274. cache: false,
  275. headers: {
  276. Authorization: this._authorizationString
  277. }
  278. })
  279. }
  280. }, {
  281. key: "_createFile",
  282. value: function(folderPath, objectName) {
  283. var itemPath = this._prepareItemRelativePath(folderPath);
  284. var queryString = "?$select=".concat(REQUIRED_ITEM_FIELDS);
  285. var url = APP_ROOT_URL + itemPath + "/children" + queryString;
  286. var params = {
  287. name: objectName,
  288. file: {},
  289. "@microsoft.graph.conflictBehavior": "rename"
  290. };
  291. var data = JSON.stringify(params);
  292. return _ajax2.default.sendRequest({
  293. url: url,
  294. method: "POST",
  295. dataType: "json",
  296. data: data,
  297. cache: false,
  298. headers: {
  299. Authorization: this._authorizationString,
  300. "Content-Type": "application/json"
  301. }
  302. })
  303. }
  304. }, {
  305. key: "_cancelUploadSession",
  306. value: function(uploadUrl) {
  307. return _ajax2.default.sendRequest({
  308. url: uploadUrl,
  309. method: "DELETE",
  310. dataType: "json",
  311. cache: false,
  312. headers: {
  313. Authorization: this._authorizationString
  314. }
  315. })
  316. }
  317. }, {
  318. key: "_prepareItemRelativePath",
  319. value: function(path) {
  320. return "" === path ? "" : ":/".concat(path, ":")
  321. }
  322. }, {
  323. key: "_hasSubDirs",
  324. value: function(dataObj) {
  325. return Object.prototype.hasOwnProperty.call(dataObj, "folder") && dataObj.folder.childCount > 0
  326. }
  327. }]);
  328. return OneDriveFileProvider
  329. }(_file_provider.FileProvider);
  330. module.exports = OneDriveFileProvider;