Dashboard.js 5.7 KB

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