Dashboard.js 5.3 KB

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