index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const Plugin = require('../../core/Plugin')
  2. const { Provider } = require('../../server')
  3. const { ProviderView } = require('../../views')
  4. const { h } = require('preact')
  5. module.exports = class GoogleDrive extends Plugin {
  6. constructor (uppy, opts) {
  7. super(uppy, opts)
  8. this.type = 'acquirer'
  9. this.id = this.opts.id || 'GoogleDrive'
  10. this.title = 'Google Drive'
  11. this.icon = () =>
  12. <svg aria-hidden="true" class="UppyIcon UppyModalTab-icon" width="28" height="28" viewBox="0 0 16 16">
  13. <path d="M2.955 14.93l2.667-4.62H16l-2.667 4.62H2.955zm2.378-4.62l-2.666 4.62L0 10.31l5.19-8.99 2.666 4.62-2.523 4.37zm10.523-.25h-5.333l-5.19-8.99h5.334l5.19 8.99z" />
  14. </svg>
  15. this[this.id] = new Provider(uppy, {
  16. host: this.opts.host,
  17. provider: 'drive',
  18. authProvider: 'google'
  19. })
  20. this.files = []
  21. this.onAuth = this.onAuth.bind(this)
  22. this.render = this.render.bind(this)
  23. // set default options
  24. const defaultOptions = {}
  25. // merge default options with the ones set by user
  26. this.opts = Object.assign({}, defaultOptions, opts)
  27. }
  28. install () {
  29. this.view = new ProviderView(this)
  30. // Set default state for Google Drive
  31. this.setPluginState({
  32. authenticated: false,
  33. files: [],
  34. folders: [],
  35. directories: [],
  36. activeRow: -1,
  37. filterInput: '',
  38. isSearchVisible: false
  39. })
  40. const target = this.opts.target
  41. if (target) {
  42. this.mount(target, this)
  43. }
  44. }
  45. uninstall () {
  46. this.view.tearDown()
  47. this.unmount()
  48. }
  49. onAuth (authenticated) {
  50. this.setPluginState({ authenticated })
  51. if (authenticated) {
  52. this.view.getFolder('root')
  53. }
  54. }
  55. getUsername (data) {
  56. for (const item of data.items) {
  57. if (item.userPermission.role === 'owner') {
  58. for (const owner of item.owners) {
  59. if (owner.isAuthenticatedUser) {
  60. return owner.emailAddress
  61. }
  62. }
  63. }
  64. }
  65. }
  66. isFolder (item) {
  67. return item.mimeType === 'application/vnd.google-apps.folder'
  68. }
  69. getItemData (item) {
  70. return Object.assign({}, item, {size: parseFloat(item.fileSize)})
  71. }
  72. getItemIcon (item) {
  73. return <img src={item.iconLink} />
  74. }
  75. getItemSubList (item) {
  76. return item.items.filter((i) => {
  77. return this.isFolder(i) || !i.mimeType.startsWith('application/vnd.google')
  78. })
  79. }
  80. getItemName (item) {
  81. return item.title ? item.title : '/'
  82. }
  83. getMimeType (item) {
  84. return item.mimeType
  85. }
  86. getItemId (item) {
  87. return item.id
  88. }
  89. getItemRequestPath (item) {
  90. return this.getItemId(item)
  91. }
  92. getItemModifiedDate (item) {
  93. return item.modifiedByMeDate
  94. }
  95. getItemThumbnailUrl (item) {
  96. return `${this.opts.host}/${this.GoogleDrive.id}/thumbnail/${this.getItemRequestPath(item)}`
  97. }
  98. render (state) {
  99. return this.view.render(state)
  100. }
  101. }