Dashboard.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 = {
  71. // maxFileSize: 1 * 1000000, // 1mb
  72. // minFileSize: 1 * 1000000, // 1mb
  73. // maxTotalFileSize: 1 * 1000000, // 1mb
  74. // maxNumberOfFiles: 3,
  75. // minNumberOfFiles: 1,
  76. // allowedFileTypes: ['image/*', '.jpg', '.jpeg', '.png', '.gif'],
  77. // requiredMetaFields: ['caption'],
  78. // }
  79. const uppyDashboard = new Uppy({
  80. logger: debugLogger,
  81. meta: {
  82. username: 'John',
  83. license: 'Creative Commons',
  84. },
  85. allowMultipleUploadBatches: false,
  86. restrictions,
  87. })
  88. .use(Dashboard, {
  89. trigger: '#pick-files',
  90. // inline: true,
  91. target: '.foo',
  92. metaFields: [
  93. { id: 'license', name: 'License', placeholder: 'specify license' },
  94. { id: 'caption', name: 'Caption', placeholder: 'add caption' },
  95. ],
  96. showProgressDetails: true,
  97. proudlyDisplayPoweredByUppy: true,
  98. note: `${JSON.stringify(restrictions)}`,
  99. })
  100. .use(GoogleDrive, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts, ...getCompanionKeysParams('GOOGLE_DRIVE') })
  101. // .use(Instagram, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  102. // .use(Dropbox, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  103. // .use(Box, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  104. // .use(Facebook, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  105. // .use(OneDrive, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  106. // .use(Zoom, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  107. // .use(Url, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  108. // .use(Unsplash, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  109. .use(RemoteSources, {
  110. companionUrl: COMPANION_URL,
  111. sources: ['GooglePhotos', 'Box', 'Dropbox', 'Facebook', 'Instagram', 'OneDrive', 'Unsplash', 'Zoom', 'Url'],
  112. companionAllowedHosts,
  113. })
  114. .use(Webcam, {
  115. target: Dashboard,
  116. showVideoSourceDropdown: true,
  117. showRecordingLength: true,
  118. })
  119. .use(Audio, {
  120. target: Dashboard,
  121. showRecordingLength: true,
  122. })
  123. .use(ScreenCapture, { target: Dashboard })
  124. .use(Form, { target: '#upload-form' })
  125. .use(ImageEditor, { target: Dashboard })
  126. .use(DropTarget, {
  127. target: document.body,
  128. })
  129. if (COMPRESS) {
  130. uppyDashboard.use(Compressor)
  131. }
  132. switch (UPLOADER) {
  133. case 'tus':
  134. uppyDashboard.use(Tus, { endpoint: TUS_ENDPOINT, limit: 6 })
  135. break
  136. case 's3':
  137. uppyDashboard.use(AwsS3, { companionUrl: COMPANION_URL, limit: 6 })
  138. break
  139. case 's3-multipart':
  140. uppyDashboard.use(AwsS3Multipart, { companionUrl: COMPANION_URL })
  141. break
  142. case 'xhr':
  143. uppyDashboard.use(XHRUpload, { endpoint: XHR_ENDPOINT, limit: 6, bundle: false })
  144. break
  145. case 'transloadit':
  146. uppyDashboard.use(Transloadit, {
  147. service: TRANSLOADIT_SERVICE_URL,
  148. waitForEncoding: true,
  149. assemblyOptions,
  150. })
  151. break
  152. case 'transloadit-s3':
  153. uppyDashboard.use(AwsS3, { companionUrl: COMPANION_URL })
  154. uppyDashboard.use(Transloadit, {
  155. waitForEncoding: true,
  156. importFromUploadURLs: true,
  157. assemblyOptions,
  158. })
  159. break
  160. case 'transloadit-xhr':
  161. uppyDashboard.setMeta({
  162. params: JSON.stringify({
  163. auth: { key: TRANSLOADIT_KEY },
  164. template_id: TRANSLOADIT_TEMPLATE,
  165. }),
  166. })
  167. uppyDashboard.use(XHRUpload, {
  168. method: 'POST',
  169. endpoint: `${TRANSLOADIT_SERVICE_URL}/assemblies`,
  170. allowedMetaFields: ['params'],
  171. bundle: true,
  172. })
  173. break
  174. default:
  175. }
  176. if (RESTORE) {
  177. uppyDashboard.use(GoldenRetriever, { serviceWorker: true })
  178. }
  179. window.uppy = uppyDashboard
  180. uppyDashboard.on('complete', (result) => {
  181. if (result.failed.length === 0) {
  182. console.log('Upload successful 😀')
  183. } else {
  184. console.warn('Upload failed 😞')
  185. }
  186. console.log('successful files:', result.successful)
  187. console.log('failed files:', result.failed)
  188. if (UPLOADER === 'transloadit') {
  189. console.log('Transloadit result:', result.transloadit)
  190. }
  191. })
  192. const modalTrigger = document.querySelector('#pick-files')
  193. if (modalTrigger) modalTrigger.click()
  194. }