MyCustomProvider.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const Plugin = require('uppy/lib/core/Plugin')
  2. const { Provider } = require('uppy/lib/server')
  3. const { ProviderView } = require('uppy/lib/views')
  4. const { h } = require('preact')
  5. module.exports = class MyCustomProvider extends Plugin {
  6. constructor (uppy, opts) {
  7. super(uppy, opts)
  8. this.type = 'acquirer'
  9. this.id = this.opts.id || 'MyCustomProvider'
  10. this.title = 'MyCustomProvider'
  11. this.icon = () => (
  12. <img src="https://uppy.io/images/logos/uppy-dog-head-arrow.svg" width="23" />
  13. )
  14. // writing out the key explicitly for readability the key used to store
  15. // the provider instance must be equal to this.id.
  16. this[this.id] = new Provider(uppy, {
  17. host: this.opts.host,
  18. provider: 'mycustomprovider'
  19. })
  20. this.files = []
  21. this.onAuth = this.onAuth.bind(this)
  22. this.render = this.render.bind(this)
  23. // merge default options with the ones set by user
  24. this.opts = Object.assign({}, opts)
  25. }
  26. install () {
  27. this.view = new ProviderView(this)
  28. // Set default state
  29. this.setPluginState({
  30. authenticated: false,
  31. files: [],
  32. folders: [],
  33. directories: [],
  34. activeRow: -1,
  35. filterInput: '',
  36. isSearchVisible: false
  37. })
  38. const target = this.opts.target
  39. if (target) {
  40. this.mount(target, this)
  41. }
  42. }
  43. uninstall () {
  44. this.view.tearDown()
  45. this.unmount()
  46. }
  47. onAuth (authenticated) {
  48. this.setPluginState({ authenticated })
  49. if (authenticated) {
  50. this.view.getFolder()
  51. }
  52. }
  53. isFolder (item) {
  54. return false
  55. }
  56. getItemData (item) {
  57. return item
  58. }
  59. getItemIcon (item) {
  60. return () => (
  61. <img src="https://uppy.io/images/logos/uppy-dog-head-arrow.svg" />
  62. )
  63. }
  64. getItemSubList (item) {
  65. return item.entries
  66. }
  67. getItemName (item) {
  68. return item.name
  69. }
  70. getMimeType (item) {
  71. // mime types aren't supported.
  72. return null
  73. }
  74. getItemId (item) {
  75. return item.name
  76. }
  77. getItemRequestPath (item) {
  78. return encodeURIComponent(item.name)
  79. }
  80. getItemModifiedDate (item) {
  81. return Date.now()
  82. }
  83. getItemThumbnailUrl (item) {
  84. return 'https://uppy.io/images/logos/uppy-dog-head-arrow.svg'
  85. }
  86. render (state) {
  87. return this.view.render(state)
  88. }
  89. }