| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- import { useTranslation } from 'react-i18next'
- import { useCallback, useEffect, useRef, useState } from 'react'
- import Uppy from '@uppy/core'
- import XHRUpload from '@uppy/xhr-upload'
- import { Dashboard } from '@uppy/react'
- import { useFileTreeActionable } from '../../../contexts/file-tree-actionable'
- import { useProjectContext } from '../../../../../shared/context/project-context'
- import * as eventTracking from '../../../../../infrastructure/event-tracking'
- import '@uppy/core/dist/style.css'
- import '@uppy/dashboard/dist/style.css'
- import { refreshProjectMetadata } from '../../../util/api'
- import ErrorMessage from '../error-message'
- import { debugConsole } from '@/utils/debugging'
- import { isAcceptableFile } from '@/features/file-tree/util/is-acceptable-file'
- import {
- findFileByNameInFolder,
- findFolderByNameInFolder,
- } from '@/features/file-tree/util/is-name-unique-in-folder'
- import { useFileTreeData } from '@/shared/context/file-tree-data-context'
- import {
- Conflict,
- FileUploadConflicts,
- FolderUploadConflicts,
- } from '@/features/file-tree/components/file-tree-create/file-tree-upload-conflicts'
- import getMeta from '@/utils/meta'
- export default function FileTreeUploadDoc() {
- const { parentFolderId, cancel, droppedFiles, setDroppedFiles } =
- useFileTreeActionable()
- const { fileTreeData } = useFileTreeData()
- const { projectId } = useProjectContext()
- const [error, setError] = useState<string>()
- const [conflicts, setConflicts] = useState<Conflict[]>([])
- const [folderConflicts, setFolderConflicts] = useState<Conflict[]>([])
- const [overwrite, setOverwrite] = useState(false)
- const dashboardRef = useRef<HTMLDivElement>(null)
- const maxNumberOfFiles = 180
- const maxFileSize = getMeta('ol-ExposedSettings').maxUploadSize
- // calculate conflicts
- const buildConflicts = (files: Record<string, any>) => {
- const conflicts: Conflict[] = []
- for (const file of Object.values(files)) {
- const { name, relativePath } = file.meta
- if (!relativePath) {
- const targetFolderId = file.meta.targetFolderId ?? parentFolderId
- const duplicateFile = findFileByNameInFolder(
- fileTreeData,
- targetFolderId,
- name
- )
- if (duplicateFile) {
- conflicts.push({
- entity: duplicateFile,
- type: 'file',
- })
- }
- const duplicateFolder = findFolderByNameInFolder(
- fileTreeData,
- targetFolderId,
- name
- )
- if (duplicateFolder) {
- conflicts.push({
- entity: duplicateFolder,
- type: 'folder',
- })
- }
- }
- }
- return conflicts
- }
- const buildFolderConflicts = (files: Record<string, any>) => {
- const conflicts: Conflict[] = []
- for (const file of Object.values(files)) {
- const { relativePath } = file.meta
- if (relativePath) {
- const [rootName] = relativePath.replace(/^\//, '').split('/')
- const targetFolderId = file.meta.targetFolderId ?? parentFolderId
- const duplicateFile = findFileByNameInFolder(
- fileTreeData,
- targetFolderId,
- rootName
- )
- if (duplicateFile) {
- conflicts.push({
- entity: duplicateFile,
- type: 'file',
- })
- }
- const duplicateFolder = findFolderByNameInFolder(
- fileTreeData,
- targetFolderId,
- rootName
- )
- if (duplicateFolder) {
- conflicts.push({
- entity: duplicateFolder,
- type: 'folder',
- })
- }
- }
- }
- return conflicts
- }
- const buildEndpoint = (projectId: string, targetFolderId: string) => {
- let endpoint = `/project/${projectId}/upload`
- if (targetFolderId) {
- endpoint += `?folder_id=${targetFolderId}`
- }
- return endpoint
- }
- // initialise the Uppy object
- const [uppy] = useState(() => {
- const endpoint = buildEndpoint(projectId, parentFolderId)
- return (
- new Uppy<{ relativePath?: string; targetFolderId: string }>({
- // logger: Uppy.debugLogger,
- allowMultipleUploadBatches: false,
- restrictions: {
- maxNumberOfFiles,
- maxFileSize: maxFileSize || null,
- },
- onBeforeFileAdded(file) {
- if (
- !isAcceptableFile(
- file.name,
- file.meta.relativePath as string | undefined
- )
- ) {
- return false
- }
- },
- onBeforeUpload(files) {
- const conflicts = buildConflicts(files)
- const folderConflicts = buildFolderConflicts(files)
- setConflicts(conflicts)
- setFolderConflicts(folderConflicts)
- return conflicts.length === 0 && folderConflicts.length === 0
- },
- autoProceed: true,
- locale: {
- strings: {
- youCanOnlyUploadX:
- 'You can only upload %{smart_count} files at a time',
- },
- },
- })
- // use the basic XHR uploader
- .use(XHRUpload, {
- endpoint,
- headers: {
- 'X-CSRF-TOKEN': getMeta('ol-csrfToken'),
- },
- // limit: maxConnections || 1,
- limit: 1,
- fieldName: 'qqfile', // "qqfile" field inherited from FineUploader
- })
- // close the modal when all the uploads completed successfully
- .on('complete', result => {
- if (!result.failed.length) {
- cancel()
- }
- })
- // broadcast doc metadata after each successful upload
- .on('upload-success', (file, response) => {
- eventTracking.sendMB('new-file-created', {
- method: 'upload',
- extension:
- file?.name && file?.name.split('.').length > 1
- ? file?.name.split('.').pop()
- : '',
- })
- if (response.body.entity_type === 'doc') {
- window.setTimeout(() => {
- refreshProjectMetadata(projectId, response.body.entity_id)
- }, 250)
- }
- })
- // handle upload errors
- .on('upload-error', (file, error, response) => {
- switch (response?.status) {
- case 429:
- setError('rate-limit-hit')
- break
- case 403:
- setError('not-logged-in')
- break
- default:
- debugConsole.error(error)
- setError(response?.body?.error || 'generic_something_went_wrong')
- break
- }
- })
- )
- })
- useEffect(() => {
- if (uppy && droppedFiles) {
- uppy.setOptions({
- autoProceed: false,
- })
- for (const file of droppedFiles.files) {
- const fileId = uppy.addFile({
- name: file.name,
- type: file.type,
- data: file,
- source: 'Local',
- isRemote: false,
- meta: {
- relativePath: (file as any).relativePath,
- targetFolderId: droppedFiles.targetFolderId,
- },
- })
- const uppyFile = uppy.getFile(fileId)
- uppy.setFileState(fileId, {
- xhrUpload: {
- ...(uppyFile as any).xhrUpload,
- endpoint: buildEndpoint(projectId, droppedFiles.targetFolderId),
- },
- })
- }
- }
- return () => {
- setDroppedFiles(null)
- }
- }, [uppy, droppedFiles, setDroppedFiles, projectId])
- // handle forced overwriting of conflicting files
- const handleOverwrite = useCallback(() => {
- setOverwrite(true)
- uppy.setOptions({
- onBeforeUpload() {
- // don't check for file conflicts
- return true
- },
- })
- uppy.upload()
- }, [uppy])
- const showFolderUploadConflicts = !overwrite && folderConflicts.length > 0
- const showFileUploadConfilcts =
- !overwrite && !showFolderUploadConflicts && conflicts.length > 0
- const showDashboard = !showFileUploadConfilcts && !showFolderUploadConflicts
- useEffect(() => {
- if (!showDashboard) {
- return
- }
- const frameId = window.requestAnimationFrame(() => {
- dashboardRef.current
- ?.querySelector<HTMLButtonElement>('.uppy-Dashboard-browse')
- ?.focus()
- })
- return () => {
- window.cancelAnimationFrame(frameId)
- }
- }, [showDashboard])
- return (
- <>
- {error && (
- <UploadErrorMessage error={error} maxNumberOfFiles={maxNumberOfFiles} />
- )}
- {showFolderUploadConflicts && (
- <FolderUploadConflicts
- cancel={cancel}
- conflicts={folderConflicts}
- handleOverwrite={handleOverwrite}
- setError={setError}
- />
- )}
- {showFileUploadConfilcts && (
- <FileUploadConflicts
- cancel={cancel}
- conflicts={conflicts}
- handleOverwrite={handleOverwrite}
- />
- )}
- {showDashboard && (
- <div ref={dashboardRef}>
- <Dashboard
- uppy={uppy}
- showProgressDetails
- // note={`Up to ${maxNumberOfFiles} files, up to ${maxFileSize / (1024 * 1024)}MB`}
- height={400}
- width="100%"
- showLinkToFileUploadResult={false}
- proudlyDisplayPoweredByUppy={false}
- // allow files or folders to be selected
- fileManagerSelectionType="both"
- locale={{
- strings: {
- // Text to show on the droppable area.
- // `%{browse}` is replaced with a link that opens the system file selection dialog.
- // TODO: 'drag_here' or 'drop_files_here_to_upload'?
- // dropHereOr: `${t('drag_here')} ${t('or')} %{browse}`,
- dropPasteBoth: `Drop or paste your files, folder, or images here. %{browseFiles} or %{browseFolders} from your computer.`,
- // Used as the label for the link that opens the system file selection dialog.
- // browseFiles: t('select_from_your_computer')
- browseFiles: 'Select files',
- browseFolders: 'select a folder',
- },
- }}
- />
- </div>
- )}
- </>
- )
- }
- function UploadErrorMessage({
- error,
- maxNumberOfFiles,
- }: {
- error: string
- maxNumberOfFiles: number
- }) {
- const { t } = useTranslation()
- switch (error) {
- case 'too-many-files':
- return (
- <>
- {t('maximum_files_uploaded_together', {
- max: maxNumberOfFiles,
- })}
- </>
- )
- default:
- return <ErrorMessage error={error} />
- }
- }
|