TransloaditFormResult.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const BasePlugin = require('@uppy/core/lib/BasePlugin')
  2. const findDOMElement = require('@uppy/utils/lib/findDOMElement')
  3. /**
  4. * After an upload completes, inject result data from Transloadit in a hidden input.
  5. *
  6. * Must be added _after_ the Transloadit plugin.
  7. */
  8. class TransloaditFormResult extends BasePlugin {
  9. constructor (uppy, opts) {
  10. super(uppy, opts)
  11. this.id = this.opts.id || 'TransloaditFormResult'
  12. this.type = 'modifier'
  13. this.handleUpload = this.handleUpload.bind(this)
  14. }
  15. getAssemblyStatuses (fileIDs) {
  16. const assemblyIds = new Set(
  17. fileIDs.map(fileID => this.uppy.getFile(fileID)?.transloadit?.assembly).filter(Boolean),
  18. )
  19. const tl = this.uppy.getPlugin(this.opts.transloaditPluginId || 'Transloadit')
  20. return Array.from(assemblyIds, (id) => tl.getAssembly(id))
  21. }
  22. handleUpload (fileIDs) {
  23. const assemblies = this.getAssemblyStatuses(fileIDs)
  24. const input = document.createElement('input')
  25. input.type = 'hidden'
  26. input.name = this.opts.name
  27. input.value = JSON.stringify(assemblies)
  28. const target = findDOMElement(this.opts.target)
  29. target.appendChild(input)
  30. }
  31. install () {
  32. this.uppy.addPostProcessor(this.handleUpload)
  33. }
  34. uninstall () {
  35. this.uppy.removePostProcessor(this.handleUpload)
  36. }
  37. }
  38. module.exports = TransloaditFormResult