diff --git a/packages/plugins/@nocobase/plugin-file-manager/src/server/__tests__/action.test.ts b/packages/plugins/@nocobase/plugin-file-manager/src/server/__tests__/action.test.ts index df6c12f697..b458fa1619 100644 --- a/packages/plugins/@nocobase/plugin-file-manager/src/server/__tests__/action.test.ts +++ b/packages/plugins/@nocobase/plugin-file-manager/src/server/__tests__/action.test.ts @@ -97,7 +97,7 @@ describe('action', () => { expect(model.toJSON()).toMatchObject(matcher); }); - it.only('should be custom values', async () => { + it('should be custom values', async () => { const Plugin = app.pm.get(PluginFileManagerServer) as PluginFileManagerServer; const model = await Plugin.createFileRecord({ collectionName: 'attachments', @@ -117,6 +117,22 @@ describe('action', () => { expect(model.toJSON()).toMatchObject(matcher); }); + it('should be upload file', async () => { + const Plugin = app.pm.get(PluginFileManagerServer) as PluginFileManagerServer; + const data = await Plugin.uploadFile({ + filePath: path.resolve(__dirname, './files/text.txt'), + documentRoot: 'storage/backups/test', + }); + const matcher = { + title: 'text', + extname: '.txt', + path: '', + meta: {}, + storageId: 1, + }; + expect(data).toMatchObject(matcher); + }); + it('upload file should be ok', async () => { const { body } = await agent.resource('attachments').create({ [FILE_FIELD_NAME]: path.resolve(__dirname, './files/text.txt'), diff --git a/packages/plugins/@nocobase/plugin-file-manager/src/server/server.ts b/packages/plugins/@nocobase/plugin-file-manager/src/server/server.ts index 5e8deb01d8..b18acbfbe1 100644 --- a/packages/plugins/@nocobase/plugin-file-manager/src/server/server.ts +++ b/packages/plugins/@nocobase/plugin-file-manager/src/server/server.ts @@ -36,6 +36,12 @@ export type FileRecordOptions = { values?: any; } & Transactionable; +export type UploadFileOptions = { + filePath: string; + storageName?: string; + documentRoot?: string; +}; + export default class PluginFileManagerServer extends Plugin { storageTypes = new Registry(); storagesCache = new Map(); @@ -46,15 +52,21 @@ export default class PluginFileManagerServer extends Plugin { if (!collection) { throw new Error(`collection does not exist`); } - const storageRepository = this.db.getRepository('storages'); const collectionRepository = this.db.getRepository(collectionName); const name = storageName || collection.options.storage; + const data = await this.uploadFile({ storageName: name, filePath }); + return await collectionRepository.create({ values: { ...data, ...values }, transaction }); + } + async uploadFile(options: UploadFileOptions) { + const { storageName, filePath, documentRoot } = options; + const storageRepository = this.db.getRepository('storages'); let storageInstance; - if (name) { + + if (storageName) { storageInstance = await storageRepository.findOne({ filter: { - name, + name: storageName, }, }); } @@ -73,6 +85,10 @@ export default class PluginFileManagerServer extends Plugin { throw new Error('[file-manager] no linked or default storage provided'); } + if (documentRoot) { + storageInstance.options['documentRoot'] = documentRoot; + } + const storageConfig = this.storageTypes.get(storageInstance.type); if (!storageConfig) { @@ -97,8 +113,7 @@ export default class PluginFileManagerServer extends Plugin { }); }); - const data = getFileData({ app: this.app, file, storage: storageInstance, request: { body: {} } } as any); - return await collectionRepository.create({ values: { ...data, ...values }, transaction }); + return getFileData({ app: this.app, file, storage: storageInstance, request: { body: {} } } as any); } async loadStorages(options?: { transaction: any }) {