index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { BasePlugin } from '@uppy/core'
  2. import Dropbox from '@uppy/dropbox'
  3. import GoogleDrive from '@uppy/google-drive'
  4. import Instagram from '@uppy/instagram'
  5. import Facebook from '@uppy/facebook'
  6. import OneDrive from '@uppy/onedrive'
  7. import Box from '@uppy/box'
  8. import Unsplash from '@uppy/unsplash'
  9. import Url from '@uppy/url'
  10. import Zoom from '@uppy/zoom'
  11. import packageJson from '../package.json'
  12. const availablePlugins = {
  13. // Using a null-prototype object to avoid prototype pollution.
  14. __proto__: null,
  15. Box,
  16. Dropbox,
  17. Facebook,
  18. GoogleDrive,
  19. Instagram,
  20. OneDrive,
  21. Unsplash,
  22. Url,
  23. Zoom,
  24. }
  25. export default class RemoteSources extends BasePlugin {
  26. static VERSION = packageJson.version
  27. #installedPlugins = new Set()
  28. constructor (uppy, opts) {
  29. super(uppy, opts)
  30. this.id = this.opts.id || 'RemoteSources'
  31. this.type = 'preset'
  32. const defaultOptions = {
  33. sources: Object.keys(availablePlugins),
  34. }
  35. this.opts = { ...defaultOptions, ...opts }
  36. if (this.opts.companionUrl == null) {
  37. throw new Error('Please specify companionUrl for RemoteSources to work, see https://uppy.io/docs/remote-sources#companionUrl')
  38. }
  39. }
  40. setOptions (newOpts) {
  41. this.uninstall()
  42. super.setOptions(newOpts)
  43. this.install()
  44. }
  45. install () {
  46. this.opts.sources.forEach((pluginId) => {
  47. const optsForRemoteSourcePlugin = { ...this.opts, sources: undefined }
  48. const plugin = availablePlugins[pluginId]
  49. if (plugin == null) {
  50. const pluginNames = Object.keys(availablePlugins)
  51. const formatter = new Intl.ListFormat('en', { style: 'long', type: 'disjunction' })
  52. throw new Error(`Invalid plugin: "${pluginId}" is not one of: ${formatter.format(pluginNames)}.`)
  53. }
  54. this.uppy.use(plugin, optsForRemoteSourcePlugin)
  55. // `plugin` is a class, but we want to track the instance object
  56. // so we have to do `getPlugin` here.
  57. this.#installedPlugins.add(this.uppy.getPlugin(pluginId))
  58. })
  59. }
  60. uninstall () {
  61. for (const plugin of this.#installedPlugins) {
  62. this.uppy.removePlugin(plugin)
  63. }
  64. this.#installedPlugins.clear()
  65. }
  66. }