DropboxClient.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. DropboxClient = function(editorUi)
  6. {
  7. DrawioClient.call(this, editorUi, 'dbauth');
  8. this.client = new Dropbox({clientId: App.DROPBOX_APPKEY});
  9. this.client.setAccessToken(this.token);
  10. };
  11. // Extends DrawioClient
  12. mxUtils.extend(DropboxClient, DrawioClient);
  13. /**
  14. * FIXME: How to find name of app folder for current user. The Apps part of the
  15. * name is internationalized so this hardcoded check does not work everywhere.
  16. */
  17. DropboxClient.prototype.appPath = '/drawio/';
  18. /**
  19. * Executes the first step for connecting to Google Drive.
  20. */
  21. DropboxClient.prototype.extension = '.html';
  22. /**
  23. * Executes the first step for connecting to Google Drive.
  24. */
  25. DropboxClient.prototype.writingFile = false;
  26. /**
  27. * Executes the first step for connecting to Google Drive.
  28. */
  29. DropboxClient.prototype.maxRetries = 4;
  30. /**
  31. * Authorizes the client, gets the userId and calls <open>.
  32. */
  33. DropboxClient.prototype.logout = function()
  34. {
  35. this.client.authTokenRevoke().then(mxUtils.bind(this, function()
  36. {
  37. this.client.setAccessToken(null);
  38. this.clearPersistentToken();
  39. this.setUser(null);
  40. this.token = null;
  41. }));
  42. };
  43. /**
  44. * Checks if the client is authorized and calls the next step.
  45. */
  46. DropboxClient.prototype.updateUser = function(success)
  47. {
  48. var acceptResponse = true;
  49. var timeoutThread = window.setTimeout(mxUtils.bind(this, function()
  50. {
  51. acceptResponse = false;
  52. this.ui.handleError({code: App.ERROR_TIMEOUT});
  53. }), this.ui.timeout);
  54. var promise = this.client.usersGetCurrentAccount();
  55. promise.then(mxUtils.bind(this, function(response)
  56. {
  57. window.clearTimeout(timeoutThread);
  58. if (acceptResponse)
  59. {
  60. this.setUser(new DrawioUser(response.account_id, response.email, response.name.display_name));
  61. if (success != null)
  62. {
  63. success();
  64. }
  65. }
  66. }));
  67. // Workaround for IE8/9 support with catch function
  68. promise['catch'](mxUtils.bind(this, function(err)
  69. {
  70. if (err != null && err.status === 401)
  71. {
  72. this.client.setAccessToken(null);
  73. this.execute(success);
  74. }
  75. else
  76. {
  77. window.clearTimeout(timeoutThread);
  78. if (acceptResponse)
  79. {
  80. this.setUser(null);
  81. this.ui.handleError(err);
  82. }
  83. }
  84. }));
  85. };
  86. /**
  87. * Authorizes the client, gets the userId and calls <open>.
  88. */
  89. DropboxClient.prototype.execute = function(fn)
  90. {
  91. if (this.client.getAccessToken() != null)
  92. {
  93. if (this.user == null)
  94. {
  95. this.updateUser(fn);
  96. }
  97. else
  98. {
  99. fn();
  100. }
  101. }
  102. else
  103. {
  104. this.ui.showAuthDialog(this, false, mxUtils.bind(this, function(remember, success)
  105. {
  106. this.authenticate(mxUtils.bind(this, function()
  107. {
  108. if (success != null)
  109. {
  110. success();
  111. }
  112. if (this.client.getAccessToken() != null)
  113. {
  114. this.updateUser(fn, mxUtils.bind(this, function(err)
  115. {
  116. this.ui.handleError(e);
  117. }));
  118. }
  119. }));
  120. }));
  121. }
  122. };
  123. /**
  124. * Authorizes the client, gets the userId and calls <open>.
  125. */
  126. DropboxClient.prototype.authenticate = function(fn)
  127. {
  128. window.open(this.client.getAuthenticationUrl('https://' +
  129. window.location.host + '/dropbox.html'), 'oauth');
  130. window.onDropboxCallback = mxUtils.bind(this, function(token, authWindow)
  131. {
  132. try
  133. {
  134. window.onDropboxCallback = null;
  135. if (authWindow != null)
  136. {
  137. authWindow.close();
  138. }
  139. if (token == null)
  140. {
  141. this.ui.hideDialog();
  142. this.ui.handleError({message: mxResources.get('cannotLogin')});
  143. }
  144. else
  145. {
  146. this.client.setAccessToken(token);
  147. this.setPersistentToken(token);
  148. fn();
  149. }
  150. }
  151. catch (e)
  152. {
  153. this.ui.handleError(e);
  154. }
  155. });
  156. };
  157. /**
  158. * Checks if the client is authorized and calls the next step.
  159. */
  160. DropboxClient.prototype.getLibrary = function(path, success, error)
  161. {
  162. this.getFile(path, success, error, true);
  163. };
  164. /**
  165. * DenyConvert is ignored in this client, just added for API compatibility.
  166. */
  167. DropboxClient.prototype.getFile = function(path, success, error, asLibrary)
  168. {
  169. asLibrary = (asLibrary != null) ? asLibrary : false;
  170. var fn = mxUtils.bind(this, function()
  171. {
  172. this.execute(mxUtils.bind(this, function()
  173. {
  174. if (/^https:\/\//i.test(path) || /\.vsdx$/i.test(path) || /\.gliffy$/i.test(path) || /\.png$/i.test(path))
  175. {
  176. var tokens = path.split('/');
  177. var name = (tokens.length > 0) ? tokens[tokens.length - 1] : path;
  178. this.ui.convertFile(path, name, null, this.extension, success, error);
  179. }
  180. else
  181. {
  182. var arg = {path: '/' + path};
  183. if (urlParams['rev'] != null)
  184. {
  185. arg.rev = urlParams['rev'];
  186. }
  187. this.readFile(arg, mxUtils.bind(this, function(data, response)
  188. {
  189. success((asLibrary) ? new DropboxLibrary(this.ui, data, response) :
  190. new DropboxFile(this.ui, data, response));
  191. }), error);
  192. }
  193. }));
  194. });
  195. fn();
  196. };
  197. /**
  198. * Translates this point by the given vector.
  199. *
  200. * @param {number} dx X-coordinate of the translation.
  201. * @param {number} dy Y-coordinate of the translation.
  202. */
  203. DropboxClient.prototype.readFile = function(arg, success, error)
  204. {
  205. var acceptResponse = true;
  206. var timeoutThread = window.setTimeout(mxUtils.bind(this, function()
  207. {
  208. acceptResponse = false;
  209. error({code: App.ERROR_TIMEOUT});
  210. }), this.ui.timeout);
  211. // Workaround for Uncaught DOMException in filesDownload is to
  212. // execute a checkExists in parallel to "catch" the file not found case
  213. this.checkExists(arg.path.substring(1), mxUtils.bind(this, function(checked, exists)
  214. {
  215. window.clearTimeout(timeoutThread);
  216. if (acceptResponse && !exists)
  217. {
  218. acceptResponse = false;
  219. error({message: mxResources.get('fileNotFound')});
  220. }
  221. }), true);
  222. // Download file in parallel
  223. // LATER: Report Uncaught DOMException with path/not_found in filesDownload
  224. var promise = this.client.filesDownload(arg);
  225. promise.then(mxUtils.bind(this, function(response)
  226. {
  227. window.clearTimeout(timeoutThread);
  228. if (acceptResponse)
  229. {
  230. acceptResponse = false;
  231. try
  232. {
  233. var reader = new FileReader();
  234. reader.onload = mxUtils.bind(this, function(event)
  235. {
  236. success(reader.result, response);
  237. });
  238. reader.readAsText(response.fileBlob);
  239. }
  240. catch (e)
  241. {
  242. error(e);
  243. }
  244. }
  245. }));
  246. // Workaround for IE8/9 support with catch function
  247. promise['catch'](function(err)
  248. {
  249. window.clearTimeout(timeoutThread);
  250. if (acceptResponse)
  251. {
  252. acceptResponse = false;
  253. error(e);
  254. }
  255. });
  256. };
  257. /**
  258. * Translates this point by the given vector.
  259. *
  260. * @param {number} dx X-coordinate of the translation.
  261. * @param {number} dy Y-coordinate of the translation.
  262. */
  263. DropboxClient.prototype.checkExists = function(filename, fn, noConfirm)
  264. {
  265. var promise = this.client.filesGetMetadata({path: '/' + filename.toLowerCase(), include_deleted: false});
  266. promise.then(mxUtils.bind(this, function(response)
  267. {
  268. if (noConfirm)
  269. {
  270. fn(false, true, response);
  271. }
  272. else
  273. {
  274. this.ui.confirm(mxResources.get('replaceIt', [filename]), function()
  275. {
  276. fn(true, true, response);
  277. }, function()
  278. {
  279. fn(false, true, response);
  280. });
  281. }
  282. }));
  283. // Workaround for IE8/9 support with catch function
  284. promise['catch'](function(err)
  285. {
  286. fn(true, false);
  287. });
  288. };
  289. /**
  290. * Translates this point by the given vector.
  291. *
  292. * @param {number} dx X-coordinate of the translation.
  293. * @param {number} dy Y-coordinate of the translation.
  294. */
  295. DropboxClient.prototype.renameFile = function(file, filename, success, error)
  296. {
  297. if (/[\\\/:\?\*"\|]/.test(filename))
  298. {
  299. error({message: mxResources.get('dropboxCharsNotAllowed')});
  300. }
  301. else
  302. {
  303. // Appends working directory of source file
  304. if (file != null && filename != null)
  305. {
  306. var path = file.stat.path_display.substring(1);
  307. var idx = path.lastIndexOf('/');
  308. if (idx > 0)
  309. {
  310. filename = path.substring(0, idx + 1) + filename;
  311. }
  312. }
  313. if (file != null && filename != null && file.stat.path_lower.substring(1) !== filename.toLowerCase())
  314. {
  315. // Checks if file exists
  316. this.execute(mxUtils.bind(this, function()
  317. {
  318. this.checkExists(filename, mxUtils.bind(this, function(checked, exists, response)
  319. {
  320. if (checked)
  321. {
  322. var thenHandler = mxUtils.bind(this, function(deleteResponse)
  323. {
  324. // Uses write and remove because move does not allow overwriting an existing target
  325. var move = this.client.filesMove({from_path: file.stat.path_display, to_path: '/' +
  326. filename, autorename: false});
  327. move.then(mxUtils.bind(this, function(response)
  328. {
  329. success(response);
  330. }))
  331. // Workaround for IE8/9 support with catch function
  332. move['catch'](error);
  333. });
  334. // API fails on same name with different upper-/lowercase
  335. if (!exists || response.path_lower.substring(1) === filename.toLowerCase())
  336. {
  337. thenHandler();
  338. }
  339. else
  340. {
  341. // Deletes file first to avoid conflict in filesMove (non-atomic)
  342. var promise = this.client.filesDelete({path: '/' + filename.toLowerCase()});
  343. promise.then(thenHandler);
  344. // Workaround for IE8/9 support with catch function
  345. promise['catch'](error);
  346. }
  347. }
  348. else
  349. {
  350. error();
  351. }
  352. }));
  353. }));
  354. }
  355. else
  356. {
  357. // Same name with different upper-/lowercase
  358. // is not supported by Dropbox API
  359. error({message: mxResources.get('invalidName')});
  360. }
  361. }
  362. };
  363. /**
  364. * Translates this point by the given vector.
  365. *
  366. * @param {number} dx X-coordinate of the translation.
  367. * @param {number} dy Y-coordinate of the translation.
  368. */
  369. DropboxClient.prototype.insertLibrary = function(filename, data, success, error)
  370. {
  371. this.insertFile(filename, data, success, error, true);
  372. };
  373. /**
  374. * Translates this point by the given vector.
  375. *
  376. * @param {number} dx X-coordinate of the translation.
  377. * @param {number} dy Y-coordinate of the translation.
  378. */
  379. DropboxClient.prototype.insertFile = function(filename, data, success, error, asLibrary)
  380. {
  381. asLibrary = (asLibrary != null) ? asLibrary : false;
  382. this.execute(mxUtils.bind(this, function()
  383. {
  384. this.checkExists(filename, mxUtils.bind(this, function(checked)
  385. {
  386. if (checked)
  387. {
  388. this.writeFile(filename, data, mxUtils.bind(this, function(stat)
  389. {
  390. if (asLibrary)
  391. {
  392. success(new DropboxLibrary(this.ui, data, stat));
  393. }
  394. else
  395. {
  396. success(new DropboxFile(this.ui, data, stat));
  397. }
  398. }), error);
  399. }
  400. else
  401. {
  402. error();
  403. }
  404. }));
  405. }));
  406. };
  407. /**
  408. * Translates this point by the given vector.
  409. *
  410. * @param {number} dx X-coordinate of the translation.
  411. * @param {number} dy Y-coordinate of the translation.
  412. */
  413. DropboxClient.prototype.saveFile = function(filename, data, success, error)
  414. {
  415. this.execute(mxUtils.bind(this, function()
  416. {
  417. this.writeFile(filename, data, success, error);
  418. }));
  419. };
  420. /**
  421. * Translates this point by the given vector.
  422. *
  423. * @param {number} dx X-coordinate of the translation.
  424. * @param {number} dy Y-coordinate of the translation.
  425. */
  426. DropboxClient.prototype.writeFile = function(filename, data, success, error)
  427. {
  428. if (/[\\\/:\?\*"\|]/.test(filename))
  429. {
  430. if (error != null)
  431. {
  432. error({message: mxResources.get('dropboxCharsNotAllowed')});
  433. }
  434. }
  435. else if (data.length >= 150000000 /*150MB*/)
  436. {
  437. if (error != null)
  438. {
  439. error({message: mxResources.get('drawingTooLarge') + ' (' +
  440. this.ui.formatFileSize(data.length) + ' / 150 MB)'});
  441. }
  442. }
  443. else if (this.writingFile)
  444. {
  445. if (error != null)
  446. {
  447. error({code: App.ERROR_BUSY});
  448. }
  449. }
  450. else
  451. {
  452. this.writingFile = true;
  453. var acceptResponse = true;
  454. var timeoutThread = null;
  455. var retryCount = 0;
  456. // Cancels any pending requests
  457. if (this.requestThread != null)
  458. {
  459. window.clearTimeout(this.requestThread);
  460. }
  461. var fn = mxUtils.bind(this, function()
  462. {
  463. if (timeoutThread != null)
  464. {
  465. window.clearTimeout(timeoutThread);
  466. }
  467. timeoutThread = window.setTimeout(mxUtils.bind(this, function()
  468. {
  469. this.writingFile = false;
  470. acceptResponse = false;
  471. if (error != null)
  472. {
  473. error({code: App.ERROR_TIMEOUT, retry: fn});
  474. }
  475. }), this.ui.timeout);
  476. var promise = this.client.filesUpload({path: '/' + filename, mode: {'.tag': 'overwrite'},
  477. contents: new Blob([data], {type: 'text/plain'})});
  478. promise.then(mxUtils.bind(this, function(response)
  479. {
  480. window.clearTimeout(timeoutThread);
  481. if (acceptResponse)
  482. {
  483. this.writingFile = false;
  484. try
  485. {
  486. if (success != null)
  487. {
  488. success(response);
  489. }
  490. }
  491. catch (e)
  492. {
  493. if (error != null)
  494. {
  495. error(e);
  496. }
  497. }
  498. }
  499. }));
  500. // Workaround for IE8/9 support with catch function
  501. promise['catch'](mxUtils.bind(this, function(err)
  502. {
  503. window.clearTimeout(timeoutThread);
  504. if (acceptResponse)
  505. {
  506. // LATER: Check error codes where a retry makes sense
  507. if (retryCount < this.maxRetries)
  508. {
  509. retryCount++;
  510. var jitter = 1 + 0.1 * (Math.random() - 0.5);
  511. this.requestThread = window.setTimeout(fn, Math.round(Math.pow(2, retryCount) * jitter * 1000));
  512. }
  513. else
  514. {
  515. this.writingFile = false;
  516. if (error != null)
  517. {
  518. error(err);
  519. }
  520. }
  521. }
  522. }));
  523. });
  524. fn();
  525. }
  526. };
  527. /**
  528. * Translates this point by the given vector.
  529. *
  530. * @param {number} dx X-coordinate of the translation.
  531. * @param {number} dy Y-coordinate of the translation.
  532. */
  533. DropboxClient.prototype.pickLibrary = function(fn)
  534. {
  535. // Authentication will be carried out on open to make sure the
  536. // autosave does not show an auth dialog. Showing it here will
  537. // block the second dialog (the file picker) so it's too early.
  538. Dropbox.choose(
  539. {
  540. linkType : 'direct',
  541. cancel: mxUtils.bind(this, function()
  542. {
  543. // do nothing
  544. }),
  545. success : mxUtils.bind(this, function(files)
  546. {
  547. if (this.ui.spinner.spin(document.body, mxResources.get('loading')))
  548. {
  549. var error = mxUtils.bind(this, function(e)
  550. {
  551. this.ui.spinner.stop();
  552. this.ui.handleError(e);
  553. });
  554. var tmp = files[0].link.indexOf(this.appPath);
  555. if (tmp > 0)
  556. {
  557. // Checks if file is in app folder by loading file from there and comparing relative path and size
  558. // KNOWN: This check fails if a file is inside a drawio directory with same relative path and size
  559. this.execute(mxUtils.bind(this, function()
  560. {
  561. var rel = decodeURIComponent(files[0].link.substring(tmp + this.appPath.length - 1));
  562. this.readFile({path: rel}, mxUtils.bind(this, function(data, stat)
  563. {
  564. if (stat != null && parseInt(files[0].bytes) === parseInt(stat.size) && rel === stat.path_display)
  565. {
  566. // No need to load file a second time
  567. try
  568. {
  569. this.ui.spinner.stop();
  570. fn(rel.substring(1), new DropboxLibrary(this.ui, data, stat));
  571. }
  572. catch (e)
  573. {
  574. this.ui.handleError(e);
  575. }
  576. }
  577. else
  578. {
  579. this.createLibrary(files[0], fn, error);
  580. }
  581. }), error);
  582. }));
  583. }
  584. else
  585. {
  586. this.execute(mxUtils.bind(this, function()
  587. {
  588. this.createLibrary(files[0], fn, error);
  589. }));
  590. }
  591. }
  592. })
  593. });
  594. };
  595. /**
  596. * Translates this point by the given vector.
  597. *
  598. * @param {number} dx X-coordinate of the translation.
  599. * @param {number} dy Y-coordinate of the translation.
  600. */
  601. DropboxClient.prototype.createLibrary = function(file, success, error)
  602. {
  603. this.ui.confirm(mxResources.get('note') + ': ' + mxResources.get('fileWillBeSavedInAppFolder', [file.name]), mxUtils.bind(this, function()
  604. {
  605. this.ui.loadUrl(file.link, mxUtils.bind(this, function(data)
  606. {
  607. this.insertFile(file.name, data, mxUtils.bind(this, function(newFile)
  608. {
  609. try
  610. {
  611. this.ui.spinner.stop();
  612. success(newFile.getHash().substring(1), newFile);
  613. }
  614. catch (e)
  615. {
  616. error(e);
  617. }
  618. }), error, true);
  619. }), error);
  620. }), mxUtils.bind(this, function()
  621. {
  622. this.ui.spinner.stop();
  623. }));
  624. };
  625. /**
  626. * Translates this point by the given vector.
  627. *
  628. * @param {number} dx X-coordinate of the translation.
  629. * @param {number} dy Y-coordinate of the translation.
  630. */
  631. DropboxClient.prototype.pickFile = function(fn, readOnly)
  632. {
  633. if (Dropbox.choose != null)
  634. {
  635. fn = (fn != null) ? fn : mxUtils.bind(this, function(path, file)
  636. {
  637. this.ui.loadFile((path != null) ? 'D' + encodeURIComponent(path) : file.getHash(), null, file);
  638. });
  639. // Authentication will be carried out on open to make sure the
  640. // autosave does not show an auth dialog. Showing it here will
  641. // block the second dialog (the file picker) so it's too early.
  642. Dropbox.choose(
  643. {
  644. linkType : 'direct',
  645. cancel: mxUtils.bind(this, function()
  646. {
  647. // do nothing
  648. }),
  649. success : mxUtils.bind(this, function(files)
  650. {
  651. if (this.ui.spinner.spin(document.body, mxResources.get('loading')))
  652. {
  653. // File used for read-only
  654. if (readOnly)
  655. {
  656. this.ui.spinner.stop();
  657. fn(files[0].link);
  658. }
  659. else
  660. {
  661. var error = mxUtils.bind(this, function(e)
  662. {
  663. this.ui.spinner.stop();
  664. this.ui.handleError(e);
  665. });
  666. var success = mxUtils.bind(this, function(path, file)
  667. {
  668. this.ui.spinner.stop();
  669. fn(path, file);
  670. });
  671. if ((/\.vsdx$/i.test(files[0].name) || /\.gliffy$/i.test(files[0].name) ||
  672. /\.png$/i.test(files[0].name)))
  673. {
  674. success(files[0].link);
  675. }
  676. else
  677. {
  678. var tmp = files[0].link.indexOf(this.appPath);
  679. if (tmp > 0)
  680. {
  681. // Checks if file is in app folder by loading file from there and comparing relative path and size
  682. // KNOWN: This check fails if a file is inside a drawio directory with same relative path and size
  683. this.execute(mxUtils.bind(this, function()
  684. {
  685. var rel = decodeURIComponent(files[0].link.substring(tmp + this.appPath.length - 1));
  686. this.readFile({path: rel}, mxUtils.bind(this, function(data, stat)
  687. {
  688. if (stat != null && parseInt(files[0].bytes) === parseInt(stat.size) && rel === stat.path_display)
  689. {
  690. this.ui.spinner.stop();
  691. // No need to load file a second time
  692. fn(rel.substring(1), new DropboxFile(this.ui, data, stat));
  693. }
  694. else
  695. {
  696. this.createFile(files[0], success, error);
  697. }
  698. }), error);
  699. }));
  700. }
  701. else
  702. {
  703. this.execute(mxUtils.bind(this, function()
  704. {
  705. this.createFile(files[0], success, error);
  706. }));
  707. }
  708. }
  709. }
  710. }
  711. })
  712. });
  713. }
  714. else
  715. {
  716. this.ui.handleError({message: mxResources.get('serviceUnavailableOrBlocked')});
  717. }
  718. };
  719. /**
  720. * Translates this point by the given vector.
  721. *
  722. * @param {number} dx X-coordinate of the translation.
  723. * @param {number} dy Y-coordinate of the translation.
  724. */
  725. DropboxClient.prototype.createFile = function(file, success, error)
  726. {
  727. this.ui.loadUrl(file.link, mxUtils.bind(this, function(data)
  728. {
  729. if (data != null && data.length > 0)
  730. {
  731. this.ui.confirm(mxResources.get('note') + ': ' + mxResources.get('fileWillBeSavedInAppFolder', [file.name]), mxUtils.bind(this, function()
  732. {
  733. this.insertFile(file.name, data, mxUtils.bind(this, function(newFile)
  734. {
  735. success(file.name, newFile);
  736. }), error);
  737. }), mxUtils.bind(this, function()
  738. {
  739. this.ui.spinner.stop();
  740. }));
  741. }
  742. else
  743. {
  744. this.ui.spinner.stop();
  745. if (error != null)
  746. {
  747. error({message: mxResources.get('errorLoadingFile')});
  748. }
  749. }
  750. }), error, /(\.png)$/i.test(file.name));
  751. };