mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-01 18:52:20 +08:00
feat(file-manager): add upload file api
This commit is contained in:
parent
51c6c6dcfa
commit
0e5f8b5e12
@ -97,7 +97,7 @@ describe('action', () => {
|
|||||||
expect(model.toJSON()).toMatchObject(matcher);
|
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 Plugin = app.pm.get(PluginFileManagerServer) as PluginFileManagerServer;
|
||||||
const model = await Plugin.createFileRecord({
|
const model = await Plugin.createFileRecord({
|
||||||
collectionName: 'attachments',
|
collectionName: 'attachments',
|
||||||
@ -117,6 +117,22 @@ describe('action', () => {
|
|||||||
expect(model.toJSON()).toMatchObject(matcher);
|
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 () => {
|
it('upload file should be ok', async () => {
|
||||||
const { body } = await agent.resource('attachments').create({
|
const { body } = await agent.resource('attachments').create({
|
||||||
[FILE_FIELD_NAME]: path.resolve(__dirname, './files/text.txt'),
|
[FILE_FIELD_NAME]: path.resolve(__dirname, './files/text.txt'),
|
||||||
|
@ -36,6 +36,12 @@ export type FileRecordOptions = {
|
|||||||
values?: any;
|
values?: any;
|
||||||
} & Transactionable;
|
} & Transactionable;
|
||||||
|
|
||||||
|
export type UploadFileOptions = {
|
||||||
|
filePath: string;
|
||||||
|
storageName?: string;
|
||||||
|
documentRoot?: string;
|
||||||
|
};
|
||||||
|
|
||||||
export default class PluginFileManagerServer extends Plugin {
|
export default class PluginFileManagerServer extends Plugin {
|
||||||
storageTypes = new Registry<IStorage>();
|
storageTypes = new Registry<IStorage>();
|
||||||
storagesCache = new Map<number, StorageModel>();
|
storagesCache = new Map<number, StorageModel>();
|
||||||
@ -46,15 +52,21 @@ export default class PluginFileManagerServer extends Plugin {
|
|||||||
if (!collection) {
|
if (!collection) {
|
||||||
throw new Error(`collection does not exist`);
|
throw new Error(`collection does not exist`);
|
||||||
}
|
}
|
||||||
const storageRepository = this.db.getRepository('storages');
|
|
||||||
const collectionRepository = this.db.getRepository(collectionName);
|
const collectionRepository = this.db.getRepository(collectionName);
|
||||||
const name = storageName || collection.options.storage;
|
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;
|
let storageInstance;
|
||||||
if (name) {
|
|
||||||
|
if (storageName) {
|
||||||
storageInstance = await storageRepository.findOne({
|
storageInstance = await storageRepository.findOne({
|
||||||
filter: {
|
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');
|
throw new Error('[file-manager] no linked or default storage provided');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (documentRoot) {
|
||||||
|
storageInstance.options['documentRoot'] = documentRoot;
|
||||||
|
}
|
||||||
|
|
||||||
const storageConfig = this.storageTypes.get(storageInstance.type);
|
const storageConfig = this.storageTypes.get(storageInstance.type);
|
||||||
|
|
||||||
if (!storageConfig) {
|
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 getFileData({ app: this.app, file, storage: storageInstance, request: { body: {} } } as any);
|
||||||
return await collectionRepository.create({ values: { ...data, ...values }, transaction });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadStorages(options?: { transaction: any }) {
|
async loadStorages(options?: { transaction: any }) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user