From 1486715c920e7ac2f99a87b2219604d052754b64 Mon Sep 17 00:00:00 2001 From: Dirk Dresch Date: Mon, 6 Mar 2023 16:36:37 +0100 Subject: [PATCH 1/4] Add check for baseUrl in path array --- src/features/upload-file/components/edit.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/features/upload-file/components/edit.tsx b/src/features/upload-file/components/edit.tsx index ac91a13..a3f7e8d 100644 --- a/src/features/upload-file/components/edit.tsx +++ b/src/features/upload-file/components/edit.tsx @@ -7,10 +7,15 @@ const Edit: FC = ({ property, record, onChange }) => { const { params } = record const { custom } = property as unknown as { custom: PropertyCustom } - const path = flat.get(params, custom.filePathProperty) const key = flat.get(params, custom.keyProperty) const file = flat.get(params, custom.fileProperty) + let path = flat.get(params, custom.filePathProperty) + if (custom.opts && custom.opts.baseUrl && path) { + const baseUrl = custom.opts.baseUrl || '' + path = path.map((singlePath, index) => (singlePath != null) ? `${baseUrl}/${key[index]}` : null) + } + const [originalKey, setOriginalKey] = useState(key) const [filesToUpload, setFilesToUpload] = useState>([]) From 27436f348b806d6a8729f7b8576c1ab879c06f2b Mon Sep 17 00:00:00 2001 From: Dirk Dresch Date: Mon, 6 Mar 2023 16:37:24 +0100 Subject: [PATCH 2/4] Add check if property is set --- src/features/upload-file/factories/update-record-factory.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/features/upload-file/factories/update-record-factory.ts b/src/features/upload-file/factories/update-record-factory.ts index d684baa..6e6a810 100644 --- a/src/features/upload-file/factories/update-record-factory.ts +++ b/src/features/upload-file/factories/update-record-factory.ts @@ -49,7 +49,7 @@ export const updateRecordFactory = ( ))) const newParams = DB_PROPERTIES.reduce((params, propertyName: string) => { - if (properties[propertyName]) { + if (properties[propertyName] && record.get(properties[propertyName])) { const filtered = record.get(properties[propertyName]).filter((el, i) => ( !filesToDelete.includes(i.toString()) )) From ef7d867e7f72f3dae6cac21a4b9934afb8f55a3b Mon Sep 17 00:00:00 2001 From: Dirk Dresch Date: Mon, 6 Mar 2023 18:25:34 +0100 Subject: [PATCH 3/4] fix(server): use move instead rename method --- package.json | 3 +++ .../upload-file/providers/local-provider.ts | 3 ++- yarn.lock | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index c8abcef..cd60ee1 100644 --- a/package.json +++ b/package.json @@ -55,5 +55,8 @@ "sinon-chai": "^3.5.0", "ts-node": "^9.0.0", "typescript": "^3.9.7" + }, + "dependencies": { + "fs-extra": "^11.1.0" } } diff --git a/src/features/upload-file/providers/local-provider.ts b/src/features/upload-file/providers/local-provider.ts index 391840c..8dc4d03 100644 --- a/src/features/upload-file/providers/local-provider.ts +++ b/src/features/upload-file/providers/local-provider.ts @@ -1,5 +1,6 @@ import { UploadedFile } from 'adminjs' import fs, { existsSync } from 'fs' +import fse from 'fs-extra' import path from 'path' import { ERROR_MESSAGES } from '../constants' import { BaseProvider, ProviderOpts } from './base-provider' @@ -33,7 +34,7 @@ export class LocalProvider extends BaseProvider { const filePath = process.platform === 'win32' ? this.path(key) : this.path(key).slice(1) // adjusting file path according to OS await fs.promises.mkdir(path.dirname(filePath), { recursive: true }) - await fs.promises.rename(file.path, filePath) + await fse.move(file.path, filePath, { overwrite: true }) } public async delete(key: string, bucket: string): Promise { diff --git a/yarn.lock b/yarn.lock index 0b0eaf3..40d42a7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4787,6 +4787,15 @@ from2@^2.1.0, from2@^2.3.0: inherits "^2.0.1" readable-stream "^2.0.0" +fs-extra@^11.1.0: + version "11.1.0" + resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-11.1.0.tgz#5784b102104433bb0e090f48bfc4a30742c357ed" + integrity sha512-0rcTq621PD5jM/e0a3EJoGC/1TC5ZBCERW82LQuwfGnCa1V8w7dpYH1yNu+SLb6E5dkeCBzKEyLGlFrnr+dUyw== + dependencies: + graceful-fs "^4.2.0" + jsonfile "^6.0.1" + universalify "^2.0.0" + fs-extra@^9.0.0: version "9.0.1" resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc" @@ -9751,6 +9760,11 @@ universalify@^1.0.0: resolved "https://registry.yarnpkg.com/universalify/-/universalify-1.0.0.tgz#b61a1da173e8435b2fe3c67d29b9adf8594bd16d" integrity sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug== +universalify@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717" + integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ== + unpipe@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec" From db7bc6491e5005dab84f099e827c8edc6e6b7ef5 Mon Sep 17 00:00:00 2001 From: Dirk Dresch Date: Wed, 19 Apr 2023 14:01:06 +0200 Subject: [PATCH 4/4] Add check for single or multiple file --- src/features/upload-file/components/edit.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/features/upload-file/components/edit.tsx b/src/features/upload-file/components/edit.tsx index a3f7e8d..2735306 100644 --- a/src/features/upload-file/components/edit.tsx +++ b/src/features/upload-file/components/edit.tsx @@ -13,7 +13,12 @@ const Edit: FC = ({ property, record, onChange }) => { let path = flat.get(params, custom.filePathProperty) if (custom.opts && custom.opts.baseUrl && path) { const baseUrl = custom.opts.baseUrl || '' - path = path.map((singlePath, index) => (singlePath != null) ? `${baseUrl}/${key[index]}` : null) + // check if we deal with single file or multiple (string or array) + if(typeof path === 'string') { + path = `${baseUrl}/${key}` + } else if(Array.isArray(path)) { + path = path.map((singlePath, index) => (singlePath != null) ? `${baseUrl}/${key[index]}` : null) + } } const [originalKey, setOriginalKey] = useState(key)