dropbox.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. var sync = require('.');
  2. var tabsUtils = require('../utils/tabs');
  3. var searchUtils = require('../utils/search');
  4. var config = {
  5. client_id: 'f0q12zup2uys5w8',
  6. redirect_uri: 'https://violentmonkey.github.io/auth_dropbox.html',
  7. };
  8. function authenticate() {
  9. var params = {
  10. response_type: 'token',
  11. client_id: config.client_id,
  12. redirect_uri: config.redirect_uri,
  13. };
  14. var url = 'https://www.dropbox.com/oauth2/authorize';
  15. var qs = searchUtils.dump(params);
  16. url += '?' + qs;
  17. tabsUtils.create(url);
  18. }
  19. function checkAuthenticate(url) {
  20. var redirect_uri = config.redirect_uri + '#';
  21. if (url.slice(0, redirect_uri.length) === redirect_uri) {
  22. authorized(url.slice(redirect_uri.length));
  23. dropbox.checkSync();
  24. return true;
  25. }
  26. }
  27. function authorized(raw) {
  28. var data = searchUtils.load(raw);
  29. if (data.access_token) {
  30. dropbox.config.set({
  31. uid: data.uid,
  32. token: data.access_token,
  33. });
  34. }
  35. }
  36. function normalize(item) {
  37. return {
  38. size: item.size,
  39. uri: sync.utils.getURI(item.name),
  40. modified: new Date(item.server_modified).getTime(),
  41. //is_deleted: item.is_deleted,
  42. };
  43. }
  44. var Dropbox = sync.BaseService.extend({
  45. name: 'dropbox',
  46. displayName: 'Dropbox',
  47. user: function () {
  48. return this.request({
  49. method: 'POST',
  50. url: 'https://api.dropboxapi.com/2/users/get_current_account',
  51. });
  52. },
  53. getMeta: function () {
  54. return sync.BaseService.prototype.getMeta.call(this)
  55. .catch(function (res) {
  56. if (res.status === 409) return {};
  57. throw res;
  58. });
  59. },
  60. list: function () {
  61. var _this = this;
  62. return _this.request({
  63. method: 'POST',
  64. url: 'https://api.dropboxapi.com/2/files/list_folder',
  65. body: {
  66. path: '',
  67. },
  68. }).then(function (text) {
  69. return JSON.parse(text);
  70. }).then(function (data) {
  71. return data.entries.filter(function (item) {
  72. return item['.tag'] === 'file' && sync.utils.isScriptFile(item.name);
  73. }).map(normalize);
  74. });
  75. },
  76. get: function (path) {
  77. return this.request({
  78. method: 'POST',
  79. url: 'https://content.dropboxapi.com/2/files/download',
  80. headers: {
  81. 'Dropbox-API-Arg': JSON.stringify({
  82. path: '/' + path,
  83. }),
  84. },
  85. });
  86. },
  87. put: function (path, data) {
  88. return this.request({
  89. method: 'POST',
  90. url: 'https://content.dropboxapi.com/2/files/upload',
  91. headers: {
  92. 'Dropbox-API-Arg': JSON.stringify({
  93. path: '/' + path,
  94. mode: 'overwrite',
  95. }),
  96. 'Content-Type': 'application/octet-stream',
  97. },
  98. body: data,
  99. }).then(function (text) {
  100. return JSON.parse(text);
  101. }).then(normalize);
  102. },
  103. remove: function (path) {
  104. return this.request({
  105. method: 'POST',
  106. url: 'https://api.dropboxapi.com/2/files/delete',
  107. body: {
  108. path: '/' + path,
  109. },
  110. }).then(function (text) {
  111. return JSON.parse(text);
  112. }).then(normalize);
  113. },
  114. authenticate: authenticate,
  115. checkAuthenticate: checkAuthenticate,
  116. });
  117. var dropbox = sync.service('dropbox', Dropbox);