ParseFile.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  7. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  8. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  9. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  10. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  11. var _ParseFileEncode = require("./ParseFileEncode");
  12. var ParseError = require('./ParseError').default;
  13. var dataUriRegexp = /^data:([a-zA-Z]+\/[-a-zA-Z0-9+.]+)(;charset=[a-zA-Z0-9\-/]*)?;base64,/;
  14. var ParseFile = function () {
  15. function ParseFile(name, data, type, metadata, tags) {
  16. (0, _classCallCheck2.default)(this, ParseFile);
  17. var specifiedType = type || '';
  18. this._name = name;
  19. this._metadata = metadata || {};
  20. this._tags = tags || {};
  21. if (data !== undefined) {
  22. if (Array.isArray(data)) {
  23. this._data = ParseFile.encodeBase64(data);
  24. this._source = {
  25. format: 'base64',
  26. base64: this._data,
  27. type: specifiedType
  28. };
  29. } else if (typeof Blob !== 'undefined' && data instanceof Blob) {
  30. this._source = {
  31. format: 'file',
  32. file: data,
  33. type: specifiedType
  34. };
  35. } else if (data && typeof data.uri === 'string' && data.uri !== undefined) {
  36. this._source = {
  37. format: 'uri',
  38. uri: data.uri,
  39. type: specifiedType
  40. };
  41. } else if (data && typeof data.base64 === 'string') {
  42. var base64 = data.base64;
  43. var commaIndex = base64.indexOf(',');
  44. if (commaIndex !== -1) {
  45. var matches = dataUriRegexp.exec(base64.slice(0, commaIndex + 1));
  46. this._data = base64.slice(commaIndex + 1);
  47. this._source = {
  48. format: 'base64',
  49. base64: this._data,
  50. type: matches[1]
  51. };
  52. } else {
  53. this._data = base64;
  54. this._source = {
  55. format: 'base64',
  56. base64: base64,
  57. type: specifiedType
  58. };
  59. }
  60. } else {
  61. throw new TypeError('Cannot create a Parse.File with that data.');
  62. }
  63. }
  64. }
  65. (0, _createClass2.default)(ParseFile, [{
  66. key: "getData",
  67. value: function () {
  68. var _this = this;
  69. var options, controller, result;
  70. return _regenerator.default.async(function (_context) {
  71. while (1) {
  72. switch (_context.prev = _context.next) {
  73. case 0:
  74. if (!this._data) {
  75. _context.next = 2;
  76. break;
  77. }
  78. return _context.abrupt("return", this._data);
  79. case 2:
  80. if (this._url) {
  81. _context.next = 4;
  82. break;
  83. }
  84. throw new Error('Cannot retrieve data for unsaved ParseFile.');
  85. case 4:
  86. options = {
  87. requestTask: function (task) {
  88. return _this._requestTask = task;
  89. }
  90. };
  91. controller = _CoreManager.default.getFileController();
  92. _context.next = 8;
  93. return _regenerator.default.awrap(controller.download(this._url, options));
  94. case 8:
  95. result = _context.sent;
  96. this._data = result.base64;
  97. return _context.abrupt("return", this._data);
  98. case 11:
  99. case "end":
  100. return _context.stop();
  101. }
  102. }
  103. }, null, this, null, Promise);
  104. }
  105. }, {
  106. key: "name",
  107. value: function () {
  108. return this._name;
  109. }
  110. }, {
  111. key: "url",
  112. value: function (options) {
  113. options = options || {};
  114. if (!this._url) {
  115. return;
  116. }
  117. if (options.forceSecure) {
  118. return this._url.replace(/^http:\/\//i, 'https://');
  119. }
  120. return this._url;
  121. }
  122. }, {
  123. key: "ipfs",
  124. value: function () {
  125. return this._ipfs;
  126. }
  127. }, {
  128. key: "hash",
  129. value: function () {
  130. return this._hash;
  131. }
  132. }, {
  133. key: "metadata",
  134. value: function () {
  135. return this._metadata;
  136. }
  137. }, {
  138. key: "tags",
  139. value: function () {
  140. return this._tags;
  141. }
  142. }, {
  143. key: "save",
  144. value: function (options) {
  145. var _this2 = this;
  146. options = options || {};
  147. options.requestTask = function (task) {
  148. return _this2._requestTask = task;
  149. };
  150. options.metadata = this._metadata;
  151. options.tags = this._tags;
  152. var controller = _CoreManager.default.getFileController();
  153. if (!this._previousSave) {
  154. if (this._source.format === 'file') {
  155. this._previousSave = controller.saveFile(this._name, this._source, options).then(function (res) {
  156. _this2._name = res.name;
  157. _this2._url = res.url;
  158. _this2._hash = res.hash;
  159. _this2._ipfs = res.ipfs;
  160. _this2._data = null;
  161. _this2._requestTask = null;
  162. return _this2;
  163. });
  164. } else if (this._source.format === 'uri') {
  165. this._previousSave = controller.download(this._source.uri, options).then(function (result) {
  166. if (!(result && result.base64)) {
  167. return {};
  168. }
  169. var newSource = {
  170. format: 'base64',
  171. base64: result.base64,
  172. type: result.contentType
  173. };
  174. _this2._data = result.base64;
  175. _this2._requestTask = null;
  176. return controller.saveBase64(_this2._name, newSource, options);
  177. }).then(function (res) {
  178. _this2._name = res.name;
  179. _this2._url = res.url;
  180. _this2._hash = res.hash;
  181. _this2._ipfs = res.ipfs;
  182. _this2._requestTask = null;
  183. return _this2;
  184. });
  185. } else {
  186. this._previousSave = controller.saveBase64(this._name, this._source, options).then(function (res) {
  187. _this2._name = res.name;
  188. _this2._url = res.url;
  189. _this2._hash = res.hash;
  190. _this2._ipfs = res.ipfs;
  191. _this2._requestTask = null;
  192. return _this2;
  193. });
  194. }
  195. }
  196. if (this._previousSave) {
  197. return this._previousSave;
  198. }
  199. }
  200. }, {
  201. key: "saveIPFS",
  202. value: function (options) {
  203. return this.save((0, _extends2.default)({}, options, {
  204. ipfs: true
  205. }));
  206. }
  207. }, {
  208. key: "cancel",
  209. value: function () {
  210. if (this._requestTask && typeof this._requestTask.abort === 'function') {
  211. this._requestTask.abort();
  212. }
  213. this._requestTask = null;
  214. }
  215. }, {
  216. key: "destroy",
  217. value: function () {
  218. var _this3 = this;
  219. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  220. if (!this._name) {
  221. throw new ParseError(ParseError.FILE_DELETE_UNNAMED_ERROR, 'Cannot delete an unnamed file.');
  222. }
  223. var destroyOptions = {
  224. useMasterKey: true
  225. };
  226. if (options.hasOwnProperty('useMasterKey')) {
  227. destroyOptions.useMasterKey = options.useMasterKey;
  228. }
  229. var controller = _CoreManager.default.getFileController();
  230. return controller.deleteFile(this._name, destroyOptions).then(function () {
  231. _this3._data = null;
  232. _this3._requestTask = null;
  233. return _this3;
  234. });
  235. }
  236. }, {
  237. key: "toJSON",
  238. value: function () {
  239. return {
  240. __type: 'File',
  241. name: this._name,
  242. url: this._url,
  243. ipfs: this._ipfs,
  244. hash: this._hash
  245. };
  246. }
  247. }, {
  248. key: "equals",
  249. value: function (other) {
  250. if (this === other) {
  251. return true;
  252. }
  253. return other instanceof ParseFile && this.name() === other.name() && this.url() === other.url() && typeof this.url() !== 'undefined';
  254. }
  255. }, {
  256. key: "setMetadata",
  257. value: function (metadata) {
  258. var _this4 = this;
  259. if (metadata && typeof metadata === 'object') {
  260. Object.keys(metadata).forEach(function (key) {
  261. _this4.addMetadata(key, metadata[key]);
  262. });
  263. }
  264. }
  265. }, {
  266. key: "addMetadata",
  267. value: function (key, value) {
  268. if (typeof key === 'string') {
  269. this._metadata[key] = value;
  270. }
  271. }
  272. }, {
  273. key: "setTags",
  274. value: function (tags) {
  275. var _this5 = this;
  276. if (tags && typeof tags === 'object') {
  277. Object.keys(tags).forEach(function (key) {
  278. _this5.addTag(key, tags[key]);
  279. });
  280. }
  281. }
  282. }, {
  283. key: "addTag",
  284. value: function (key, value) {
  285. if (typeof key === 'string') {
  286. this._tags[key] = value;
  287. }
  288. }
  289. }], [{
  290. key: "fromJSON",
  291. value: function (obj) {
  292. if (obj.__type !== 'File') {
  293. throw new TypeError('JSON object does not represent a ParseFile');
  294. }
  295. var file = new ParseFile(obj.name);
  296. file._url = obj.url;
  297. file._hash = obj.hash;
  298. file._ipfs = obj.ipfs;
  299. return file;
  300. }
  301. }, {
  302. key: "encodeBase64",
  303. value: function (bytes) {
  304. return (0, _ParseFileEncode.encodeBase64)(bytes);
  305. }
  306. }]);
  307. return ParseFile;
  308. }();
  309. _CoreManager.default.setFileController(require('./ParseFileController.default'));
  310. var _default = ParseFile;
  311. exports.default = _default;
  312. exports.b64Digit = _ParseFileEncode.b64Digit;