mirror of
https://github.com/nocobase/nocobase.git
synced 2026-10-29 18:16:18 +08:00
Merge branch 'next' into develop
This commit is contained in:
@@ -216,6 +216,33 @@ describe('action', () => {
|
||||
// const res2 = await agent.get(`${DEFAULT_LOCAL_BASE_URL}${body.data.path}/${encodedFilename}`);
|
||||
// expect(res2.text).toBe(rawText);
|
||||
});
|
||||
|
||||
it('create file record should be ok', async () => {
|
||||
db.collection({
|
||||
name: 'customers',
|
||||
fields: [
|
||||
{
|
||||
name: 'avatar',
|
||||
type: 'belongsTo',
|
||||
target: 'attachments',
|
||||
},
|
||||
],
|
||||
});
|
||||
const record = {
|
||||
title: 'text',
|
||||
extname: '.txt',
|
||||
path: '',
|
||||
// size: 13,
|
||||
meta: {},
|
||||
storageId: 1,
|
||||
};
|
||||
const { status, body } = await agent.resource('attachments').create({
|
||||
attachmentField: 'customers.avatar',
|
||||
values: record,
|
||||
});
|
||||
expect(status).toBe(200);
|
||||
expect(body.data).toMatchObject(record);
|
||||
});
|
||||
});
|
||||
|
||||
describe('specific storage', () => {
|
||||
|
||||
@@ -87,14 +87,30 @@ export async function createMiddleware(ctx: Context, next: Next) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const storageName =
|
||||
resourceName === 'attachments'
|
||||
? ctx.db.getFieldByPath(attachmentField)?.options?.storage
|
||||
: collection.options.storage;
|
||||
// const StorageRepo = ctx.db.getRepository('storages');
|
||||
// const storage = await StorageRepo.findOne({ filter: storageName ? { name: storageName } : { default: true } });
|
||||
const plugin = ctx.app.pm.get(Plugin) as Plugin;
|
||||
const storageName = ctx.db.getFieldByPath(attachmentField)?.options?.storage || collection.options.storage;
|
||||
const storages = Array.from(plugin.storagesCache.values());
|
||||
ctx.storage = storages.find((item) => item.name === storageName) || storages.find((item) => item.default);
|
||||
const storage = Array.from(plugin.storagesCache.values()).find((storage) =>
|
||||
storageName ? storage.name === storageName : storage.default,
|
||||
);
|
||||
if (!storage) {
|
||||
ctx.logger.error(`[file-manager] no storage found`);
|
||||
return ctx.throw(500);
|
||||
}
|
||||
ctx.storage = storage;
|
||||
|
||||
if (ctx?.request.is('multipart/*')) {
|
||||
await multipart(ctx, next);
|
||||
} else {
|
||||
ctx.action.mergeParams({
|
||||
values: {
|
||||
storage: { id: storage.id },
|
||||
},
|
||||
});
|
||||
await next();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||
*/
|
||||
|
||||
import { isURL } from '@nocobase/utils';
|
||||
import axios, { AxiosRequestConfig } from 'axios';
|
||||
import { StorageEngine } from 'multer';
|
||||
import Path from 'path';
|
||||
import { StorageEngine } from 'multer';
|
||||
import type { Readable } from 'stream';
|
||||
import urlJoin from 'url-join';
|
||||
import { isURL } from '@nocobase/utils';
|
||||
import { encodeURL, ensureUrlEncoded, getFileKey } from '../utils';
|
||||
export interface StorageModel {
|
||||
id?: number;
|
||||
|
||||
Reference in New Issue
Block a user