Dashboard.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* eslint-disable import/no-extraneous-dependencies */
  2. import Uppy, { debugLogger } from '@uppy/core'
  3. import Dashboard from '@uppy/dashboard'
  4. import RemoteSources from '@uppy/remote-sources'
  5. import Webcam from '@uppy/webcam'
  6. import ScreenCapture from '@uppy/screen-capture'
  7. import GoldenRetriever from '@uppy/golden-retriever'
  8. import Tus from '@uppy/tus'
  9. import AwsS3 from '@uppy/aws-s3'
  10. import XHRUpload from '@uppy/xhr-upload'
  11. import Transloadit from '@uppy/transloadit'
  12. import Form from '@uppy/form'
  13. import ImageEditor from '@uppy/image-editor'
  14. import DropTarget from '@uppy/drop-target'
  15. import Audio from '@uppy/audio'
  16. import Compressor from '@uppy/compressor'
  17. import GoogleDrive from '@uppy/google-drive'
  18. import english from '@uppy/locales/lib/en_US.js'
  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 =
  33. 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. locale: english,
  81. logger: debugLogger,
  82. meta: {
  83. username: 'John',
  84. license: 'Creative Commons',
  85. },
  86. allowMultipleUploadBatches: false,
  87. restrictions,
  88. })
  89. .use(Dashboard, {
  90. trigger: '#pick-files',
  91. // inline: true,
  92. target: '.foo',
  93. metaFields: [
  94. { id: 'license', name: 'License', placeholder: 'specify license' },
  95. { id: 'caption', name: 'Caption', placeholder: 'add caption' },
  96. ],
  97. showProgressDetails: true,
  98. proudlyDisplayPoweredByUppy: true,
  99. note: `${JSON.stringify(restrictions)}`,
  100. })
  101. .use(GoogleDrive, {
  102. target: Dashboard,
  103. companionUrl: COMPANION_URL,
  104. companionAllowedHosts,
  105. ...getCompanionKeysParams('GOOGLE_DRIVE'),
  106. })
  107. // .use(Instagram, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  108. // .use(Dropbox, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  109. // .use(Box, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  110. // .use(Facebook, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  111. // .use(OneDrive, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  112. // .use(Zoom, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  113. // .use(Url, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  114. // .use(Unsplash, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
  115. .use(RemoteSources, {
  116. companionUrl: COMPANION_URL,
  117. sources: [
  118. 'GooglePhotos',
  119. 'Box',
  120. 'Dropbox',
  121. 'Facebook',
  122. 'Instagram',
  123. 'OneDrive',
  124. 'Unsplash',
  125. 'Zoom',
  126. 'Url',
  127. ],
  128. companionAllowedHosts,
  129. })
  130. .use(Webcam, {
  131. target: Dashboard,
  132. showVideoSourceDropdown: true,
  133. showRecordingLength: true,
  134. })
  135. .use(Audio, {
  136. target: Dashboard,
  137. showRecordingLength: true,
  138. })
  139. .use(ScreenCapture, { target: Dashboard })
  140. .use(Form, { target: '#upload-form' })
  141. .use(ImageEditor, { target: Dashboard })
  142. .use(DropTarget, {
  143. target: document.body,
  144. })
  145. if (COMPRESS) {
  146. uppyDashboard.use(Compressor)
  147. }
  148. switch (UPLOADER) {
  149. case 'tus':
  150. uppyDashboard.use(Tus, { endpoint: TUS_ENDPOINT, limit: 6 })
  151. break
  152. case 's3':
  153. uppyDashboard.use(AwsS3, { companionUrl: COMPANION_URL, limit: 6 })
  154. break
  155. case 's3-multipart':
  156. uppyDashboard.use(AwsS3, { companionUrl: COMPANION_URL, shouldUseMultipart: true })
  157. break
  158. case 'xhr':
  159. uppyDashboard.use(XHRUpload, {
  160. endpoint: XHR_ENDPOINT,
  161. limit: 6,
  162. bundle: false,
  163. })
  164. break
  165. case 'transloadit':
  166. uppyDashboard.use(Transloadit, {
  167. service: TRANSLOADIT_SERVICE_URL,
  168. waitForEncoding: true,
  169. assemblyOptions,
  170. })
  171. break
  172. case 'transloadit-s3':
  173. uppyDashboard.use(AwsS3, { companionUrl: COMPANION_URL })
  174. uppyDashboard.use(Transloadit, {
  175. waitForEncoding: true,
  176. importFromUploadURLs: true,
  177. assemblyOptions,
  178. })
  179. break
  180. case 'transloadit-xhr':
  181. uppyDashboard.setMeta({
  182. params: JSON.stringify({
  183. auth: { key: TRANSLOADIT_KEY },
  184. template_id: TRANSLOADIT_TEMPLATE,
  185. }),
  186. })
  187. uppyDashboard.use(XHRUpload, {
  188. method: 'POST',
  189. endpoint: `${TRANSLOADIT_SERVICE_URL}/assemblies`,
  190. allowedMetaFields: ['params'],
  191. bundle: true,
  192. })
  193. break
  194. default:
  195. }
  196. if (RESTORE) {
  197. uppyDashboard.use(GoldenRetriever, { serviceWorker: true })
  198. }
  199. window.uppy = uppyDashboard
  200. uppyDashboard.on('complete', (result) => {
  201. if (result.failed.length === 0) {
  202. console.log('Upload successful 😀')
  203. } else {
  204. console.warn('Upload failed 😞')
  205. }
  206. console.log('successful files:', result.successful)
  207. console.log('failed files:', result.failed)
  208. if (UPLOADER === 'transloadit') {
  209. console.log('Transloadit result:', result.transloadit)
  210. }
  211. })
  212. const modalTrigger = document.querySelector('#pick-files')
  213. if (modalTrigger) modalTrigger.click()
  214. }