Dashboard.js 6.7 KB

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