index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const { Plugin } = require('@uppy/core')
  2. const { Provider } = require('@uppy/companion-client')
  3. const ProviderViews = require('@uppy/provider-views')
  4. const { h } = require('preact')
  5. module.exports = class GoogleDrive extends Plugin {
  6. constructor (uppy, opts) {
  7. super(uppy, opts)
  8. this.id = this.opts.id || 'GoogleDrive'
  9. this.title = this.opts.title || 'Google Drive'
  10. Provider.initPlugin(this, opts)
  11. this.title = this.opts.title || 'Google Drive'
  12. this.icon = () => (
  13. <svg aria-hidden="true" width="18px" height="16px" viewBox="0 0 18 16" version="1.1" xmlns="http://www.w3.org/2000/svg">
  14. <g fill-rule="evenodd">
  15. <polygon fill="#3089FC" points="6.32475 10.2 18 10.2 14.999625 15.3 3.324375 15.3" />
  16. <polygon fill="#00A85D" points="3.000375 15.3 0 10.2 5.83875 0.275974026 8.838 5.37597403 5.999625 10.2" />
  17. <polygon fill="#FFD024" points="11.838375 9.92402597 5.999625 0 12.000375 0 17.839125 9.92402597" />
  18. </g>
  19. </svg>
  20. )
  21. this[this.id] = new Provider(uppy, {
  22. serverUrl: this.opts.serverUrl,
  23. serverHeaders: this.opts.serverHeaders,
  24. provider: 'drive',
  25. authProvider: 'google'
  26. })
  27. this.onAuth = this.onAuth.bind(this)
  28. this.render = this.render.bind(this)
  29. }
  30. install () {
  31. this.view = new ProviderViews(this)
  32. // Set default state for Google Drive
  33. this.setPluginState({
  34. authenticated: false,
  35. files: [],
  36. folders: [],
  37. directories: [],
  38. activeRow: -1,
  39. filterInput: '',
  40. isSearchVisible: false
  41. })
  42. const target = this.opts.target
  43. if (target) {
  44. this.mount(target, this)
  45. }
  46. }
  47. uninstall () {
  48. this.view.tearDown()
  49. this.unmount()
  50. }
  51. onAuth (authenticated) {
  52. this.setPluginState({ authenticated })
  53. if (authenticated) {
  54. this.view.getFolder('root')
  55. }
  56. }
  57. getUsername (data) {
  58. for (const item of data.files) {
  59. if (item.ownedByMe) {
  60. for (const permission of item.permissions) {
  61. if (permission.role === 'owner') {
  62. return permission.emailAddress
  63. }
  64. }
  65. }
  66. }
  67. }
  68. isFolder (item) {
  69. return item.mimeType === 'application/vnd.google-apps.folder'
  70. }
  71. getItemData (item) {
  72. return Object.assign({}, item, {size: parseFloat(item.size)})
  73. }
  74. getItemIcon (item) {
  75. return item.iconLink
  76. }
  77. getItemSubList (item) {
  78. return item.files.filter((i) => {
  79. return this.isFolder(i) || !i.mimeType.startsWith('application/vnd.google')
  80. })
  81. }
  82. getItemName (item) {
  83. return item.name ? item.name : '/'
  84. }
  85. getMimeType (item) {
  86. return item.mimeType
  87. }
  88. getItemId (item) {
  89. return item.id
  90. }
  91. getItemRequestPath (item) {
  92. return this.getItemId(item)
  93. }
  94. getItemModifiedDate (item) {
  95. return item.modifiedTime
  96. }
  97. getItemThumbnailUrl (item) {
  98. return `${this.opts.serverUrl}/${this.GoogleDrive.id}/thumbnail/${this.getItemRequestPath(item)}`
  99. }
  100. render (state) {
  101. return this.view.render(state)
  102. }
  103. }