webapi.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**
  2. * DevExtreme (ui/file_manager/file_provider/webapi.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 _common = require("../../../core/utils/common");
  21. var _guid = require("../../../core/guid");
  22. var _guid2 = _interopRequireDefault(_guid);
  23. var _window = require("../../../core/utils/window");
  24. var _iterator = require("../../../core/utils/iterator");
  25. var _deferred = require("../../../core/utils/deferred");
  26. var _file_provider = require("./file_provider");
  27. var _data = require("../../../core/utils/data");
  28. function _interopRequireDefault(obj) {
  29. return obj && obj.__esModule ? obj : {
  30. "default": obj
  31. }
  32. }
  33. function _classCallCheck(instance, Constructor) {
  34. if (!(instance instanceof Constructor)) {
  35. throw new TypeError("Cannot call a class as a function")
  36. }
  37. }
  38. function _defineProperties(target, props) {
  39. for (var i = 0; i < props.length; i++) {
  40. var descriptor = props[i];
  41. descriptor.enumerable = descriptor.enumerable || false;
  42. descriptor.configurable = true;
  43. if ("value" in descriptor) {
  44. descriptor.writable = true
  45. }
  46. Object.defineProperty(target, descriptor.key, descriptor)
  47. }
  48. }
  49. function _createClass(Constructor, protoProps, staticProps) {
  50. if (protoProps) {
  51. _defineProperties(Constructor.prototype, protoProps)
  52. }
  53. if (staticProps) {
  54. _defineProperties(Constructor, staticProps)
  55. }
  56. Object.defineProperty(Constructor, "prototype", {
  57. writable: false
  58. });
  59. return Constructor
  60. }
  61. function _inherits(subClass, superClass) {
  62. if ("function" !== typeof superClass && null !== superClass) {
  63. throw new TypeError("Super expression must either be null or a function")
  64. }
  65. subClass.prototype = Object.create(superClass && superClass.prototype, {
  66. constructor: {
  67. value: subClass,
  68. writable: true,
  69. configurable: true
  70. }
  71. });
  72. Object.defineProperty(subClass, "prototype", {
  73. writable: false
  74. });
  75. if (superClass) {
  76. _setPrototypeOf(subClass, superClass)
  77. }
  78. }
  79. function _setPrototypeOf(o, p) {
  80. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
  81. o.__proto__ = p;
  82. return o
  83. };
  84. return _setPrototypeOf(o, p)
  85. }
  86. function _createSuper(Derived) {
  87. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  88. return function() {
  89. var result, Super = _getPrototypeOf(Derived);
  90. if (hasNativeReflectConstruct) {
  91. var NewTarget = _getPrototypeOf(this).constructor;
  92. result = Reflect.construct(Super, arguments, NewTarget)
  93. } else {
  94. result = Super.apply(this, arguments)
  95. }
  96. return _possibleConstructorReturn(this, result)
  97. }
  98. }
  99. function _possibleConstructorReturn(self, call) {
  100. if (call && ("object" === _typeof(call) || "function" === typeof call)) {
  101. return call
  102. } else {
  103. if (void 0 !== call) {
  104. throw new TypeError("Derived constructors may only return object or undefined")
  105. }
  106. }
  107. return _assertThisInitialized(self)
  108. }
  109. function _assertThisInitialized(self) {
  110. if (void 0 === self) {
  111. throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
  112. }
  113. return self
  114. }
  115. function _isNativeReflectConstruct() {
  116. if ("undefined" === typeof Reflect || !Reflect.construct) {
  117. return false
  118. }
  119. if (Reflect.construct.sham) {
  120. return false
  121. }
  122. if ("function" === typeof Proxy) {
  123. return true
  124. }
  125. try {
  126. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
  127. return true
  128. } catch (e) {
  129. return false
  130. }
  131. }
  132. function _getPrototypeOf(o) {
  133. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) {
  134. return o.__proto__ || Object.getPrototypeOf(o)
  135. };
  136. return _getPrototypeOf(o)
  137. }
  138. var window = (0, _window.getWindow)();
  139. var FILE_CHUNK_BLOB_NAME = "chunk";
  140. var WebApiFileProvider = function(_FileProvider) {
  141. _inherits(WebApiFileProvider, _FileProvider);
  142. var _super = _createSuper(WebApiFileProvider);
  143. function WebApiFileProvider(options) {
  144. var _this;
  145. _classCallCheck(this, WebApiFileProvider);
  146. options = (0, _common.ensureDefined)(options, {});
  147. _this = _super.call(this, options);
  148. _this._endpointUrl = options.endpointUrl;
  149. _this._hasSubDirsGetter = (0, _data.compileGetter)(options.hasSubDirectoriesExpr || "hasSubDirectories");
  150. return _this
  151. }
  152. _createClass(WebApiFileProvider, [{
  153. key: "getItems",
  154. value: function(path, itemType) {
  155. return this._getItems(path, itemType)
  156. }
  157. }, {
  158. key: "renameItem",
  159. value: function(item, name) {
  160. return this._executeRequest("Rename", {
  161. id: item.relativeName,
  162. name: name
  163. })
  164. }
  165. }, {
  166. key: "createFolder",
  167. value: function(parentFolder, name) {
  168. return this._executeRequest("CreateDir", {
  169. parentId: parentFolder.relativeName,
  170. name: name
  171. })
  172. }
  173. }, {
  174. key: "deleteItems",
  175. value: function(items) {
  176. var _this2 = this;
  177. return items.map(function(item) {
  178. return _this2._executeRequest("Remove", {
  179. id: item.relativeName
  180. })
  181. })
  182. }
  183. }, {
  184. key: "moveItems",
  185. value: function(items, destinationFolder) {
  186. var _this3 = this;
  187. return items.map(function(item) {
  188. return _this3._executeRequest("Move", {
  189. sourceId: item.relativeName,
  190. destinationId: destinationFolder.relativeName + "/" + item.name
  191. })
  192. })
  193. }
  194. }, {
  195. key: "copyItems",
  196. value: function(items, destinationFolder) {
  197. var _this4 = this;
  198. return items.map(function(item) {
  199. return _this4._executeRequest("Copy", {
  200. sourceId: item.relativeName,
  201. destinationId: destinationFolder.relativeName + "/" + item.name
  202. })
  203. })
  204. }
  205. }, {
  206. key: "initiateFileUpload",
  207. value: function(uploadInfo) {
  208. uploadInfo.customData.uploadId = new _guid2.default
  209. }
  210. }, {
  211. key: "uploadFileChunk",
  212. value: function(uploadInfo, chunk) {
  213. var args = {
  214. destinationId: uploadInfo.destinationFolder.relativeName,
  215. chunkMetadata: JSON.stringify({
  216. UploadId: uploadInfo.customData.uploadId,
  217. FileName: uploadInfo.file.name,
  218. Index: chunk.index,
  219. TotalCount: uploadInfo.totalChunkCount,
  220. FileSize: uploadInfo.file.size
  221. })
  222. };
  223. var formData = new window.FormData;
  224. formData.append(FILE_CHUNK_BLOB_NAME, chunk.blob);
  225. formData.append("arguments", JSON.stringify(args));
  226. formData.append("command", "UploadChunk");
  227. var deferred = new _deferred.Deferred;
  228. _ajax2.default.sendRequest({
  229. url: this._endpointUrl,
  230. method: "POST",
  231. dataType: "json",
  232. data: formData,
  233. upload: {
  234. onprogress: _common.noop,
  235. onloadstart: _common.noop,
  236. onabort: _common.noop
  237. },
  238. cache: false
  239. }).then(function(result) {
  240. !result.success && deferred.reject(result) || deferred.resolve()
  241. }, function(e) {
  242. return deferred.reject(e)
  243. });
  244. return deferred.promise()
  245. }
  246. }, {
  247. key: "abortFileUpload",
  248. value: function(uploadInfo) {
  249. return this._executeRequest("AbortUpload", {
  250. uploadId: uploadInfo.customData.uploadId
  251. })
  252. }
  253. }, {
  254. key: "_getItems",
  255. value: function(path, itemType) {
  256. var _this5 = this;
  257. return this._getEntriesByPath(path).then(function(result) {
  258. return _this5._convertDataObjectsToFileItems(result.result, path, itemType)
  259. })
  260. }
  261. }, {
  262. key: "_getItemsIds",
  263. value: function(items) {
  264. return items.map(function(it) {
  265. return it.relativeName
  266. })
  267. }
  268. }, {
  269. key: "_getEntriesByPath",
  270. value: function(path) {
  271. return this._executeRequest("GetDirContents", {
  272. parentId: path
  273. })
  274. }
  275. }, {
  276. key: "_executeRequest",
  277. value: function(command, args) {
  278. var method = "GetDirContents" === command ? "GET" : "POST";
  279. var deferred = new _deferred.Deferred;
  280. _ajax2.default.sendRequest({
  281. url: this._getEndpointUrl(command, args),
  282. method: method,
  283. dataType: "json",
  284. cache: false
  285. }).then(function(result) {
  286. !result.success && deferred.reject(result) || deferred.resolve(result)
  287. }, function(e) {
  288. return deferred.reject(e)
  289. });
  290. return deferred.promise()
  291. }
  292. }, {
  293. key: "_getEndpointUrl",
  294. value: function(command, args) {
  295. var queryString = this._getQueryString({
  296. command: command,
  297. arguments: JSON.stringify(args)
  298. });
  299. var separator = this._endpointUrl && this._endpointUrl.indexOf("?") > 0 ? "&" : "?";
  300. return this._endpointUrl + separator + queryString
  301. }
  302. }, {
  303. key: "_getQueryString",
  304. value: function(params) {
  305. var pairs = [];
  306. var keys = Object.keys(params);
  307. for (var i = 0; i < keys.length; i++) {
  308. var key = keys[i];
  309. var value = params[key];
  310. if (void 0 === value) {
  311. continue
  312. }
  313. if (null === value) {
  314. value = ""
  315. }
  316. if (Array.isArray(value)) {
  317. this._processQueryStringArrayParam(key, value, pairs)
  318. } else {
  319. var pair = this._getQueryStringPair(key, value);
  320. pairs.push(pair)
  321. }
  322. }
  323. return pairs.join("&")
  324. }
  325. }, {
  326. key: "_processQueryStringArrayParam",
  327. value: function(key, array, pairs) {
  328. var _this6 = this;
  329. (0, _iterator.each)(array, function(_, item) {
  330. var pair = _this6._getQueryStringPair(key, item);
  331. pairs.push(pair)
  332. })
  333. }
  334. }, {
  335. key: "_getQueryStringPair",
  336. value: function(key, value) {
  337. return encodeURIComponent(key) + "=" + encodeURIComponent(value)
  338. }
  339. }, {
  340. key: "_hasSubDirs",
  341. value: function(dataObj) {
  342. var hasSubDirs = this._hasSubDirsGetter(dataObj);
  343. return "boolean" === typeof hasSubDirs ? hasSubDirs : true
  344. }
  345. }]);
  346. return WebApiFileProvider
  347. }(_file_provider.FileProvider);
  348. module.exports = WebApiFileProvider;