GoogleDrive.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {
  2. Provider,
  3. getAllowedHosts,
  4. tokenStorage,
  5. type CompanionPluginOptions,
  6. } from '@uppy/companion-client'
  7. import { UIPlugin, Uppy } from '@uppy/core'
  8. import { ProviderViews } from '@uppy/provider-views'
  9. import { h, type ComponentChild } from 'preact'
  10. import type { UppyFile, Body, Meta } from '@uppy/utils/lib/UppyFile'
  11. import type { UnknownProviderPluginState } from '@uppy/core/lib/Uppy'
  12. import DriveProviderViews from './DriveProviderViews.ts'
  13. import locale from './locale.ts'
  14. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  15. // @ts-ignore We don't want TS to generate types for the package.json
  16. import packageJson from '../package.json'
  17. export type GoogleDriveOptions = CompanionPluginOptions
  18. export default class GoogleDrive<
  19. M extends Meta,
  20. B extends Body,
  21. > extends UIPlugin<GoogleDriveOptions, M, B, UnknownProviderPluginState> {
  22. static VERSION = packageJson.version
  23. icon: () => h.JSX.Element
  24. provider: Provider<M, B>
  25. view!: ProviderViews<M, B>
  26. storage: typeof tokenStorage
  27. files: UppyFile<M, B>[]
  28. rootFolderId: string | null = 'root'
  29. constructor(uppy: Uppy<M, B>, opts: GoogleDriveOptions) {
  30. super(uppy, opts)
  31. this.type = 'acquirer'
  32. this.storage = this.opts.storage || tokenStorage
  33. this.files = []
  34. this.id = this.opts.id || 'GoogleDrive'
  35. this.icon = () => (
  36. <svg
  37. aria-hidden="true"
  38. focusable="false"
  39. width="32"
  40. height="32"
  41. viewBox="0 0 32 32"
  42. >
  43. <g fillRule="nonzero" fill="none">
  44. <path
  45. d="M6.663 22.284l.97 1.62c.202.34.492.609.832.804l3.465-5.798H5c0 .378.1.755.302 1.096l1.361 2.278z"
  46. fill="#0066DA"
  47. />
  48. <path
  49. d="M16 12.09l-3.465-5.798c-.34.195-.63.463-.832.804l-6.4 10.718A2.15 2.15 0 005 18.91h6.93L16 12.09z"
  50. fill="#00AC47"
  51. />
  52. <path
  53. d="M23.535 24.708c.34-.195.63-.463.832-.804l.403-.67 1.928-3.228c.201-.34.302-.718.302-1.096h-6.93l1.474 2.802 1.991 2.996z"
  54. fill="#EA4335"
  55. />
  56. <path
  57. d="M16 12.09l3.465-5.798A2.274 2.274 0 0018.331 6h-4.662c-.403 0-.794.11-1.134.292L16 12.09z"
  58. fill="#00832D"
  59. />
  60. <path
  61. d="M20.07 18.91h-8.14l-3.465 5.798c.34.195.73.292 1.134.292h12.802c.403 0 .794-.11 1.134-.292L20.07 18.91z"
  62. fill="#2684FC"
  63. />
  64. <path
  65. d="M23.497 12.455l-3.2-5.359a2.252 2.252 0 00-.832-.804L16 12.09l4.07 6.82h6.917c0-.377-.1-.755-.302-1.096l-3.188-5.359z"
  66. fill="#FFBA00"
  67. />
  68. </g>
  69. </svg>
  70. )
  71. this.opts.companionAllowedHosts = getAllowedHosts(
  72. this.opts.companionAllowedHosts,
  73. this.opts.companionUrl,
  74. )
  75. this.provider = new Provider(uppy, {
  76. companionUrl: this.opts.companionUrl,
  77. companionHeaders: this.opts.companionHeaders,
  78. companionKeysParams: this.opts.companionKeysParams,
  79. companionCookiesRule: this.opts.companionCookiesRule,
  80. provider: 'drive',
  81. pluginId: this.id,
  82. supportsRefreshToken: true,
  83. })
  84. this.defaultLocale = locale
  85. this.i18nInit()
  86. this.title = this.i18n('pluginNameGoogleDrive')
  87. this.render = this.render.bind(this)
  88. }
  89. install(): void {
  90. this.view = new DriveProviderViews(this, {
  91. provider: this.provider,
  92. loadAllFiles: true,
  93. virtualList: true,
  94. })
  95. const { target } = this.opts
  96. if (target) {
  97. this.mount(target, this)
  98. }
  99. }
  100. uninstall(): void {
  101. this.view.tearDown()
  102. this.unmount()
  103. }
  104. render(state: unknown): ComponentChild {
  105. return this.view.render(state)
  106. }
  107. }