feat(file-manager): add upload file api
Some checks are pending
Build Docker Image / build-and-push (push) Waiting to run
Build Pro Image / build-and-push (push) Waiting to run
E2E / Build (push) Waiting to run
E2E / Core and plugins (push) Blocked by required conditions
E2E / plugin-workflow (push) Blocked by required conditions
E2E / plugin-workflow-approval (push) Blocked by required conditions
E2E / plugin-data-source-main (push) Blocked by required conditions
E2E / Comment on PR (push) Blocked by required conditions
NocoBase Backend Test / sqlite-test (20, false) (push) Waiting to run
NocoBase Backend Test / sqlite-test (20, true) (push) Waiting to run
NocoBase Backend Test / postgres-test (public, 20, nocobase, false) (push) Waiting to run
NocoBase Backend Test / postgres-test (public, 20, nocobase, true) (push) Waiting to run
NocoBase Backend Test / postgres-test (public, 20, public, false) (push) Waiting to run
NocoBase Backend Test / postgres-test (public, 20, public, true) (push) Waiting to run
NocoBase Backend Test / postgres-test (user_schema, 20, nocobase, false) (push) Waiting to run
NocoBase Backend Test / postgres-test (user_schema, 20, nocobase, true) (push) Waiting to run
NocoBase Backend Test / postgres-test (user_schema, 20, public, false) (push) Waiting to run
NocoBase Backend Test / postgres-test (user_schema, 20, public, true) (push) Waiting to run
NocoBase Backend Test / mysql-test (20, false) (push) Waiting to run
NocoBase Backend Test / mysql-test (20, true) (push) Waiting to run
NocoBase Backend Test / mariadb-test (20, false) (push) Waiting to run
NocoBase Backend Test / mariadb-test (20, true) (push) Waiting to run
Test on Windows / build (push) Waiting to run

This commit is contained in:
chenos 2024-08-20 11:24:35 +08:00
parent 51c6c6dcfa
commit 0e5f8b5e12
2 changed files with 37 additions and 6 deletions

View File

@ -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'),

View File

@ -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<IStorage>();
storagesCache = new Map<number, StorageModel>();
@ -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 }) {