Dashboard.js 6.2 KB

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