diff --git a/package.json b/package.json index 41e272d..bf71a7e 100644 --- a/package.json +++ b/package.json @@ -65,5 +65,7 @@ "ts-node": "^10.9.1", "typescript": "^5.0.2" }, - "dependencies": {} + "dependencies": { + "fs-extra": "^11.1.0" + } } diff --git a/src/features/upload-file/components/UploadEditComponent.tsx b/src/features/upload-file/components/UploadEditComponent.tsx index 07d3e53..8b1a8c4 100644 --- a/src/features/upload-file/components/UploadEditComponent.tsx +++ b/src/features/upload-file/components/UploadEditComponent.tsx @@ -7,10 +7,20 @@ 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 || '' + // 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) const [filesToUpload, setFilesToUpload] = useState>([]) diff --git a/src/features/upload-file/factories/update-record-factory.ts b/src/features/upload-file/factories/update-record-factory.ts index 1f6e4d1..1969bbf 100644 --- a/src/features/upload-file/factories/update-record-factory.ts +++ b/src/features/upload-file/factories/update-record-factory.ts @@ -48,7 +48,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()) )) diff --git a/src/features/upload-file/providers/local-provider.ts b/src/features/upload-file/providers/local-provider.ts index 625cd54..fb2f426 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.js' import { BaseProvider, ProviderOpts } from './base-provider.js' @@ -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 {