index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const Plugin = require('../../core/Plugin')
  2. const { Provider } = require('../../server')
  3. const { ProviderView } = require('../../views')
  4. const icons = require('./icons')
  5. const { h } = require('preact')
  6. module.exports = class Dropbox extends Plugin {
  7. constructor (uppy, opts) {
  8. super(uppy, opts)
  9. this.type = 'acquirer'
  10. this.id = this.opts.id || 'Dropbox'
  11. this.title = 'Dropbox'
  12. this.icon = () => (
  13. <svg class="UppyIcon" width="128" height="118" viewBox="0 0 128 118">
  14. <path d="M38.145.777L1.108 24.96l25.608 20.507 37.344-23.06z" />
  15. <path d="M1.108 65.975l37.037 24.183L64.06 68.525l-37.343-23.06zM64.06 68.525l25.917 21.633 37.036-24.183-25.61-20.51z" />
  16. <path d="M127.014 24.96L89.977.776 64.06 22.407l37.345 23.06zM64.136 73.18l-25.99 21.567-11.122-7.262v8.142l37.112 22.256 37.114-22.256v-8.142l-11.12 7.262z" />
  17. </svg>
  18. )
  19. // writing out the key explicitly for readability the key used to store
  20. // the provider instance must be equal to this.id.
  21. this[this.id] = new Provider(uppy, {
  22. host: this.opts.host,
  23. provider: 'dropbox'
  24. })
  25. this.files = []
  26. this.onAuth = this.onAuth.bind(this)
  27. this.render = this.render.bind(this)
  28. // set default options
  29. const defaultOptions = {}
  30. // merge default options with the ones set by user
  31. this.opts = Object.assign({}, defaultOptions, opts)
  32. this.opts.hostPattern = opts.hostPattern || opts.host
  33. }
  34. install () {
  35. this.view = new ProviderView(this)
  36. // Set default state for Dropbox
  37. this.setPluginState({
  38. authenticated: false,
  39. files: [],
  40. folders: [],
  41. directories: [],
  42. activeRow: -1,
  43. filterInput: '',
  44. isSearchVisible: false
  45. })
  46. const target = this.opts.target
  47. if (target) {
  48. this.mount(target, this)
  49. }
  50. }
  51. uninstall () {
  52. this.view.tearDown()
  53. this.unmount()
  54. }
  55. onAuth (authenticated) {
  56. this.setPluginState({ authenticated })
  57. if (authenticated) {
  58. this.view.getFolder()
  59. }
  60. }
  61. getUsername (data) {
  62. return data.user_email
  63. }
  64. isFolder (item) {
  65. return item['.tag'] === 'folder'
  66. }
  67. getItemData (item) {
  68. return item
  69. }
  70. getItemIcon (item) {
  71. return icons[item['.tag']]()
  72. }
  73. getItemSubList (item) {
  74. return item.entries
  75. }
  76. getItemName (item) {
  77. return item.name || ''
  78. }
  79. getMimeType (item) {
  80. // mime types aren't supported.
  81. return null
  82. }
  83. getItemId (item) {
  84. return item.id
  85. }
  86. getItemRequestPath (item) {
  87. return encodeURIComponent(item.path_lower)
  88. }
  89. getItemModifiedDate (item) {
  90. return item.server_modified
  91. }
  92. getItemThumbnailUrl (item) {
  93. return `${this.opts.host}/${this.Dropbox.id}/thumbnail/${this.getItemRequestPath(item)}`
  94. }
  95. render (state) {
  96. return this.view.render(state)
  97. }
  98. }