Dashboard.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // The @uppy/ dependencies are resolved from source
  2. /* eslint-disable import/no-extraneous-dependencies */
  3. import Uppy, { debugLogger } from '@uppy/core'
  4. import Dashboard from '@uppy/dashboard'
  5. import RemoteSources from '@uppy/remote-sources'
  6. import Webcam from '@uppy/webcam'
  7. import ScreenCapture from '@uppy/screen-capture'
  8. import GoldenRetriever from '@uppy/golden-retriever'
  9. import Tus from '@uppy/tus'
  10. import AwsS3 from '@uppy/aws-s3'
  11. import AwsS3Multipart from '@uppy/aws-s3-multipart'
  12. import XHRUpload from '@uppy/xhr-upload'
  13. import Transloadit from '@uppy/transloadit'
  14. import Form from '@uppy/form'
  15. import ImageEditor from '@uppy/image-editor'
  16. import DropTarget from '@uppy/drop-target'
  17. import Audio from '@uppy/audio'
  18. import Compressor from '@uppy/compressor'
  19. /* eslint-enable import/no-extraneous-dependencies */
  20. import generateSignatureIfSecret from './generateSignatureIfSecret.js'
  21. // DEV CONFIG: create a .env file in the project root directory to customize those values.
  22. const {
  23. VITE_UPLOADER : UPLOADER,
  24. VITE_COMPANION_URL : COMPANION_URL,
  25. VITE_COMPANION_ALLOWED_HOSTS : companionAllowedHosts,
  26. VITE_TUS_ENDPOINT : TUS_ENDPOINT,
  27. VITE_XHR_ENDPOINT : XHR_ENDPOINT,
  28. VITE_TRANSLOADIT_KEY : TRANSLOADIT_KEY,
  29. VITE_TRANSLOADIT_SECRET : TRANSLOADIT_SECRET,
  30. VITE_TRANSLOADIT_TEMPLATE : TRANSLOADIT_TEMPLATE,
  31. VITE_TRANSLOADIT_SERVICE_URL : TRANSLOADIT_SERVICE_URL,
  32. } = import.meta.env
  33. import.meta.env.VITE_TRANSLOADIT_KEY &&= '***' // to avoid leaking secrets in screenshots.
  34. import.meta.env.VITE_TRANSLOADIT_SECRET &&= '***' // to avoid leaking secrets in screenshots.
  35. console.log(import.meta.env)
  36. // DEV CONFIG: enable or disable Golden Retriever
  37. const RESTORE = false
  38. async function getAssemblyOptions () {
  39. return generateSignatureIfSecret(TRANSLOADIT_SECRET, {
  40. auth: {
  41. key: TRANSLOADIT_KEY,
  42. },
  43. // It's more secure to use a template_id and enable
  44. // Signature Authentication
  45. template_id: TRANSLOADIT_TEMPLATE,
  46. })
  47. }
  48. // Rest is implementation! Obviously edit as necessary...
  49. export default () => {
  50. const uppyDashboard = new Uppy({
  51. logger: debugLogger,
  52. meta: {
  53. username: 'John',
  54. license: 'Creative Commons',
  55. },
  56. allowMultipleUploadBatches: false,
  57. // restrictions: { requiredMetaFields: ['caption'] },
  58. })
  59. .use(Dashboard, {
  60. trigger: '#pick-files',
  61. // inline: true,
  62. target: '.foo',
  63. metaFields: [
  64. { id: 'license', name: 'License', placeholder: 'specify license' },
  65. { id: 'caption', name: 'Caption', placeholder: 'add caption' },
  66. ],
  67. showProgressDetails: true,
  68. proudlyDisplayPoweredByUppy: true,
  69. note: '2 files, images and video only',
  70. })
  71. // .use(GoogleDrive, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  72. // .use(Instagram, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  73. // .use(Dropbox, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  74. // .use(Box, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  75. // .use(Facebook, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  76. // .use(OneDrive, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  77. // .use(Zoom, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  78. // .use(Url, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  79. // .use(Unsplash, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  80. .use(RemoteSources, {
  81. companionUrl: COMPANION_URL,
  82. sources: ['Box', 'Dropbox', 'Facebook', 'GoogleDrive', 'Instagram', 'OneDrive', 'Unsplash', 'Zoom', 'Url'],
  83. companionAllowedHosts,
  84. })
  85. .use(Webcam, {
  86. target: Dashboard,
  87. showVideoSourceDropdown: true,
  88. showRecordingLength: true,
  89. })
  90. .use(Audio, {
  91. target: Dashboard,
  92. showRecordingLength: true,
  93. })
  94. .use(ScreenCapture, { target: Dashboard })
  95. .use(Form, { target: '#upload-form' })
  96. .use(ImageEditor, { target: Dashboard })
  97. .use(DropTarget, {
  98. target: document.body,
  99. })
  100. .use(Compressor)
  101. switch (UPLOADER) {
  102. case 'tus':
  103. uppyDashboard.use(Tus, { endpoint: TUS_ENDPOINT, limit: 6 })
  104. break
  105. case 's3':
  106. uppyDashboard.use(AwsS3, { companionUrl: COMPANION_URL, limit: 6 })
  107. break
  108. case 's3-multipart':
  109. uppyDashboard.use(AwsS3Multipart, { companionUrl: COMPANION_URL, limit: 6 })
  110. break
  111. case 'xhr':
  112. uppyDashboard.use(XHRUpload, { endpoint: XHR_ENDPOINT, limit: 6, bundle: true })
  113. break
  114. case 'transloadit':
  115. uppyDashboard.use(Transloadit, {
  116. service: TRANSLOADIT_SERVICE_URL,
  117. waitForEncoding: true,
  118. getAssemblyOptions,
  119. })
  120. break
  121. case 'transloadit-s3':
  122. uppyDashboard.use(AwsS3, { companionUrl: COMPANION_URL })
  123. uppyDashboard.use(Transloadit, {
  124. waitForEncoding: true,
  125. importFromUploadURLs: true,
  126. getAssemblyOptions,
  127. })
  128. break
  129. case 'transloadit-xhr':
  130. uppyDashboard.setMeta({
  131. params: JSON.stringify({
  132. auth: { key: TRANSLOADIT_KEY },
  133. template_id: TRANSLOADIT_TEMPLATE,
  134. }),
  135. })
  136. uppyDashboard.use(XHRUpload, {
  137. method: 'POST',
  138. endpoint: `${TRANSLOADIT_SERVICE_URL}/assemblies`,
  139. allowedMetaFields: ['params'],
  140. bundle: true,
  141. })
  142. break
  143. default:
  144. }
  145. if (RESTORE) {
  146. uppyDashboard.use(GoldenRetriever, { serviceWorker: true })
  147. }
  148. window.uppy = uppyDashboard
  149. uppyDashboard.on('complete', (result) => {
  150. if (result.failed.length === 0) {
  151. console.log('Upload successful 😀')
  152. } else {
  153. console.warn('Upload failed 😞')
  154. }
  155. console.log('successful files:', result.successful)
  156. console.log('failed files:', result.failed)
  157. if (UPLOADER === 'transloadit') {
  158. console.log('Transloadit result:', result.transloadit)
  159. }
  160. })
  161. const modalTrigger = document.querySelector('#pick-files')
  162. if (modalTrigger) modalTrigger.click()
  163. }