main.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const { inspect } = require('node:util')
  2. const robodog = require('@uppy/robodog')
  3. const TRANSLOADIT_KEY = '35c1aed03f5011e982b6afe82599b6a0'
  4. // A trivial template that resizes images, just for example purposes.
  5. //
  6. // "steps": {
  7. // ":original": { "robot": "/upload/handle" },
  8. // "resize": {
  9. // "use": ":original",
  10. // "robot": "/image/resize",
  11. // "width": 100,
  12. // "height": 100,
  13. // "imagemagick_stack": "v1.0.0"
  14. // }
  15. // }
  16. const TEMPLATE_ID = 'bbc273f69e0c4694a5a9d1b587abc1bc'
  17. /**
  18. * robodog.form
  19. */
  20. const formUppy = robodog.form('#test-form', {
  21. debug: true,
  22. fields: ['message'],
  23. restrictions: {
  24. allowedFileTypes: ['.png'],
  25. },
  26. waitForEncoding: true,
  27. params: {
  28. auth: { key: TRANSLOADIT_KEY },
  29. template_id: TEMPLATE_ID,
  30. },
  31. modal: true,
  32. progressBar: '#test-form .progress',
  33. })
  34. formUppy.on('error', (err) => {
  35. document.querySelector('#test-form .error')
  36. .textContent = err.message
  37. })
  38. formUppy.on('upload-error', (file, err) => {
  39. document.querySelector('#test-form .error')
  40. .textContent = err.message
  41. })
  42. window.formUppy = formUppy
  43. const formUppyWithDashboard = robodog.form('#dashboard-form', {
  44. debug: true,
  45. fields: ['message'],
  46. restrictions: {
  47. allowedFileTypes: ['.png'],
  48. },
  49. waitForEncoding: true,
  50. note: 'Only PNG files please!',
  51. params: {
  52. auth: { key: TRANSLOADIT_KEY },
  53. template_id: TEMPLATE_ID,
  54. },
  55. dashboard: '#dashboard-form .dashboard',
  56. })
  57. window.formUppyWithDashboard = formUppyWithDashboard
  58. const dashboard = robodog.dashboard('#dashboard', {
  59. debug: true,
  60. waitForEncoding: true,
  61. note: 'Images will be resized with Transloadit',
  62. params: {
  63. auth: { key: TRANSLOADIT_KEY },
  64. template_id: TEMPLATE_ID,
  65. },
  66. })
  67. window.dashboard = dashboard
  68. /**
  69. * robodog.modal
  70. */
  71. function openModal () {
  72. robodog.pick({
  73. restrictions: {
  74. allowedFileTypes: ['.png'],
  75. },
  76. waitForEncoding: true,
  77. params: {
  78. auth: { key: TRANSLOADIT_KEY },
  79. template_id: TEMPLATE_ID,
  80. },
  81. providers: [
  82. 'webcam',
  83. ],
  84. // if providers need custom config
  85. // webcam: {
  86. // option: 'whatever'
  87. // }
  88. }).then(console.log, console.error)
  89. }
  90. window.openModal = openModal
  91. /**
  92. * robodog.upload
  93. */
  94. window.doUpload = (event) => {
  95. const resultEl = document.querySelector('#upload-result')
  96. const errorEl = document.querySelector('#upload-error')
  97. robodog.upload(event.target.files, {
  98. waitForEncoding: true,
  99. params: {
  100. auth: { key: TRANSLOADIT_KEY },
  101. template_id: TEMPLATE_ID,
  102. },
  103. }).then((result) => {
  104. resultEl.classList.remove('hidden')
  105. errorEl.classList.add('hidden')
  106. resultEl.textContent = inspect(result.results)
  107. }, (err) => {
  108. resultEl.classList.add('hidden')
  109. errorEl.classList.remove('hidden')
  110. errorEl.textContent = err.message
  111. })
  112. }