index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const html = require('yo-yo')
  2. const Plugin = require('../Plugin')
  3. const Provider = require('../../uppy-base/src/plugins/Provider')
  4. const View = require('../../generic-provider-views/index')
  5. module.exports = class Google extends Plugin {
  6. constructor (core, opts) {
  7. super(core, opts)
  8. this.type = 'acquirer'
  9. this.id = 'GoogleDrive'
  10. this.title = 'Google Drive'
  11. this.stateId = 'googleDrive'
  12. this.icon = () => html`
  13. <svg class="UppyIcon UppyModalTab-icon" width="28" height="28" viewBox="0 0 16 16">
  14. <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"/>
  15. </svg>
  16. `
  17. // writing out the key explicitly for readability the key used to store
  18. // the provider instance must be equal to this.id.
  19. this.GoogleDrive = new Provider({
  20. host: this.opts.host,
  21. provider: 'drive',
  22. authProvider: 'google'
  23. })
  24. this.files = []
  25. this.onAuth = this.onAuth.bind(this)
  26. // Visual
  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. }
  33. install () {
  34. this.view = new View(this)
  35. // Set default state for Google Drive
  36. this.core.setState({
  37. // writing out the key explicitly for readability the key used to store
  38. // the plugin state must be equal to this.stateId.
  39. googleDrive: {
  40. authenticated: false,
  41. files: [],
  42. folders: [],
  43. directories: [],
  44. activeRow: -1,
  45. filterInput: ''
  46. }
  47. })
  48. const target = this.opts.target
  49. const plugin = this
  50. this.target = this.mount(target, plugin)
  51. // catch error here.
  52. this[this.id].auth().then(this.onAuth).catch(this.view.handleError)
  53. return
  54. }
  55. uninstall () {
  56. this.unmount()
  57. }
  58. onAuth (authenticated) {
  59. this.view.updateState({authenticated})
  60. if (authenticated) {
  61. this.view.getFolder('root')
  62. }
  63. }
  64. isFolder (item) {
  65. return item.mimeType === 'application/vnd.google-apps.folder'
  66. }
  67. getItemData (item) {
  68. return Object.assign({}, item, {size: parseFloat(item.fileSize)})
  69. }
  70. getItemIcon (item) {
  71. return html`<img src=${item.iconLink}/>`
  72. }
  73. getItemSubList (item) {
  74. return item.items.filter((i) => {
  75. return this.isFolder(i) || !i.mimeType.startsWith('application/vnd.google')
  76. })
  77. }
  78. getItemName (item) {
  79. return item.title ? item.title : '/'
  80. }
  81. getMimeType (item) {
  82. return item.mimeType
  83. }
  84. getItemId (item) {
  85. return item.id
  86. }
  87. getItemRequestPath (item) {
  88. return this.getItemId(item)
  89. }
  90. getItemModifiedDate (item) {
  91. return item.modifiedByMeDate
  92. }
  93. getItemThumbnailUrl (item) {
  94. return `${this.opts.host}/${this.GoogleDrive.id}/thumbnail/${this.getItemRequestPath(item)}`
  95. }
  96. render (state) {
  97. return this.view.render(state)
  98. }
  99. }