From 1288e94b90df74323245b5c5f671233713168642 Mon Sep 17 00:00:00 2001 From: M Gilang Januar Date: Mon, 10 Jan 2022 13:35:49 +0700 Subject: [PATCH 1/2] implement upload recursively (#184) --- server/src/api/v1/Files.ts | 1 + web/src/pages/dashboard/components/Upload.tsx | 26 ++++++++++++++++++- web/src/pages/dashboard/index.tsx | 14 ++++++---- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/server/src/api/v1/Files.ts b/server/src/api/v1/Files.ts index 355f9596c..9af42df58 100644 --- a/server/src/api/v1/Files.ts +++ b/server/src/api/v1/Files.ts @@ -268,6 +268,7 @@ export class Files { @Endpoint.POST('/upload/:id?', { middlewares: [Auth, multer().single('upload')] }) public async upload(req: Request, res: Response): Promise { const { name, size, mime_type: mimetype, parent_id: parentId, total_part: totalPart, part } = req.query as Record + if (!name || !size || !mimetype || !part || !totalPart) { throw { status: 400, body: { error: 'Name, size, mimetype, part, and total part are required' } } } diff --git a/web/src/pages/dashboard/components/Upload.tsx b/web/src/pages/dashboard/components/Upload.tsx index c2d3fe37c..d2c74bf9a 100644 --- a/web/src/pages/dashboard/components/Upload.tsx +++ b/web/src/pages/dashboard/components/Upload.tsx @@ -58,9 +58,33 @@ const Upload: React.FC = ({ dataFileList: [fileList, setFileList], parent const data = new FormData() data.append('upload', blobPart) + let parentId = parent?.id + if (file.webkitRelativePath) { + const paths = file.webkitRelativePath.split('/').slice(0, -1) || [] + for (const path of paths) { + const { data: findFolder } = await req.get('/files', { params: { + type: 'folder', + name: path, + ...parentId ? { parent_id: parentId } : { 'parent_id.is': 'null' }, + } }) + if (!findFolder?.length) { + const { data: newFolder } = await req.post('/files/addFolder', { + file: { + name: path, + ...parentId ? { parent_id: parentId } : {}, + } + }) + parentId = newFolder.file.id + } else { + parentId = findFolder.files[0].id + } + } + } + const { data: response } = await req.post(`/files/upload${i > 0 && firstResponse?.file?.id ? `/${firstResponse?.file.id}` : ''}`, data, { params: { - ...parent?.id ? { parent_id: parent.link_id || parent.id || undefined } : {}, + // ...parent?.id ? { parent_id: parent.link_id || parent.id || undefined } : {}, + ...parentId ? { parent_id: parentId } : {}, name: `${file.name}${fileParts > 1 ? `.part${j + 1}` : ''}`, size: fileBlob.size, mime_type: file.type || mime.lookup(file.name) || 'application/octet-stream', diff --git a/web/src/pages/dashboard/index.tsx b/web/src/pages/dashboard/index.tsx index c7000d6fb..d238f17a9 100644 --- a/web/src/pages/dashboard/index.tsx +++ b/web/src/pages/dashboard/index.tsx @@ -151,7 +151,7 @@ const Dashboard: React.FC = ({ match }) useEffect(() => { if (filesUpload?.files) { const list = fileList?.map(file => { - if (!file.response?.file.id) return file + if (!file.response?.file?.id) return file const found = filesUpload.files.find((f: any) => f.id === file.response?.file.id) if (!found) return null return { @@ -160,9 +160,14 @@ const Dashboard: React.FC = ({ match }) } }).filter(file => file && file.status !== 'success') setFileList(list) - setData([...filesUpload.files?.map((file: any) => ({ ...file, key: file.id })), ...data].reduce((res, row) => [ - ...res, !res.filter(Boolean).find((r: any) => r.id === row.id) ? row : null - ], []).filter(Boolean)) + // setData([...filesUpload.files?.map((file: any) => ({ ...file, key: file.id })), ...data].reduce((res, row) => [ + // ...res, !res.filter(Boolean).find((r: any) => r.id === row.id) ? row : null + // ], []).filter(Boolean)) + if ((dataChanges?.pagination?.current || 0) > 1) { + change({ ...dataChanges?.pagination, current: 1 }, dataChanges?.filters, dataChanges?.sorter) + } else { + refetch() + } } }, [filesUpload]) @@ -173,7 +178,6 @@ const Dashboard: React.FC = ({ match }) ...keyword ? { 'name.ilike': `'%${keyword}%'` } : {}, ...tab === 'shared' ? { shared: 1, 'parent_id.is': undefined } : {}, limit: PAGE_SIZE, - // skip: ((pagination?.current || 1) - 1) * PAGE_SIZE, offset: pagination?.current === 1 || actions?.action || keyword && params?.offset ? 0 : data?.length, ...Object.keys(filters || {})?.reduce((res, key: string) => { if (!filters) return res From e66e2b023366944d36f587080fd8418a37639a35 Mon Sep 17 00:00:00 2001 From: M Gilang Januar Date: Mon, 10 Jan 2022 13:48:22 +0700 Subject: [PATCH 2/2] 1.6.1 --- package.json | 2 +- server/package.json | 2 +- web/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 77283c42c..ee00c67e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "teledrive", - "version": "1.6.0", + "version": "1.6.1", "repository": "git@github.com:mgilangjanuar/teledrive.git", "author": "M Gilang Januar ", "license": "MIT", diff --git a/server/package.json b/server/package.json index 972891a9e..8d7ec9926 100644 --- a/server/package.json +++ b/server/package.json @@ -1,6 +1,6 @@ { "name": "server", - "version": "1.6.0", + "version": "1.6.1", "main": "dist/index.js", "license": "MIT", "private": true, diff --git a/web/package.json b/web/package.json index 8f94668d7..74f6d65c6 100644 --- a/web/package.json +++ b/web/package.json @@ -1,6 +1,6 @@ { "name": "web", - "version": "1.6.0", + "version": "1.6.1", "private": true, "dependencies": { "@craco/craco": "^6.3.0",