TransloaditResultsPlugin.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const { Plugin } = require('@uppy/core')
  2. /**
  3. * Add a `results` key to the upload result data, containing all Transloadit Assembly results.
  4. */
  5. class TransloaditResultsPlugin extends Plugin {
  6. constructor (uppy, opts) {
  7. super(uppy, opts)
  8. this.type = 'modifier'
  9. this.id = 'TransloaditResultsPlugin'
  10. this._afterUpload = this._afterUpload.bind(this)
  11. }
  12. install () {
  13. this.uppy.addPostProcessor(this._afterUpload)
  14. }
  15. _afterUpload (fileIDs, uploadID) {
  16. const { currentUploads } = this.uppy.getState()
  17. const { result } = currentUploads[uploadID]
  18. const assemblies = result && Array.isArray(result.transloadit) ? result.transloadit : []
  19. // Merge the assembly.results[*] arrays and add `stepName` and
  20. // `assemblyId` properties.
  21. const assemblyResults = []
  22. assemblies.forEach((assembly) => {
  23. Object.keys(assembly.results).forEach((stepName) => {
  24. const results = assembly.results[stepName]
  25. results.forEach((result) => {
  26. assemblyResults.push({
  27. ...result,
  28. assemblyId: assembly.assembly_id,
  29. stepName
  30. })
  31. })
  32. })
  33. })
  34. this.uppy.addResultData(uploadID, {
  35. results: assemblyResults
  36. })
  37. }
  38. }
  39. module.exports = TransloaditResultsPlugin