Dashboard.js 5.7 KB

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