Dashboard.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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, {
  154. endpoint: COMPANION_URL,
  155. shouldUseMultipart: false,
  156. })
  157. break
  158. case 's3-multipart':
  159. uppyDashboard.use(AwsS3, {
  160. endpoint: COMPANION_URL,
  161. shouldUseMultipart: true,
  162. })
  163. break
  164. case 'xhr':
  165. uppyDashboard.use(XHRUpload, {
  166. endpoint: XHR_ENDPOINT,
  167. limit: 6,
  168. bundle: false,
  169. })
  170. break
  171. case 'transloadit':
  172. uppyDashboard.use(Transloadit, {
  173. service: TRANSLOADIT_SERVICE_URL,
  174. waitForEncoding: true,
  175. assemblyOptions,
  176. })
  177. break
  178. case 'transloadit-s3':
  179. uppyDashboard.use(AwsS3, { companionUrl: COMPANION_URL })
  180. uppyDashboard.use(Transloadit, {
  181. waitForEncoding: true,
  182. importFromUploadURLs: true,
  183. assemblyOptions,
  184. })
  185. break
  186. case 'transloadit-xhr':
  187. uppyDashboard.setMeta({
  188. params: JSON.stringify({
  189. auth: { key: TRANSLOADIT_KEY },
  190. template_id: TRANSLOADIT_TEMPLATE,
  191. }),
  192. })
  193. uppyDashboard.use(XHRUpload, {
  194. method: 'POST',
  195. endpoint: `${TRANSLOADIT_SERVICE_URL}/assemblies`,
  196. allowedMetaFields: ['params'],
  197. bundle: true,
  198. })
  199. break
  200. default:
  201. }
  202. if (RESTORE) {
  203. uppyDashboard.use(GoldenRetriever, { serviceWorker: true })
  204. }
  205. window.uppy = uppyDashboard
  206. uppyDashboard.on('complete', (result) => {
  207. if (result.failed.length === 0) {
  208. console.log('Upload successful 😀')
  209. } else {
  210. console.warn('Upload failed 😞')
  211. }
  212. console.log('successful files:', result.successful)
  213. console.log('failed files:', result.failed)
  214. if (UPLOADER === 'transloadit') {
  215. console.log('Transloadit result:', result.transloadit)
  216. }
  217. })
  218. const modalTrigger = document.querySelector('#pick-files')
  219. if (modalTrigger) modalTrigger.click()
  220. }