tachybase_todo/packages/plugins/duplicator/src/server/server.ts
ChengLei Shao 0d92e59985
chore: tsx (#2329)
* chore: upgrade jest

* fix: eslint

* chore: github action backend test

* fix: import

* chore: export

* fix: test

* chore: install tsx

* chore: type

* chore: replace @koa/multer

* chore: replace ts-node-dev with tsx
2023-07-27 10:29:07 +08:00

51 lines
1.6 KiB
TypeScript

import { Plugin } from '@nocobase/server';
import addDumpCommand from './commands/dump-command';
import addRestoreCommand from './commands/restore-command';
import { koaMulter as multer } from '@nocobase/utils';
import * as os from 'os';
import dumpAction from './actions/dump-action';
import dumpableCollections from './actions/dumpable-collections-action';
import getDictAction from './actions/get-dict-action';
import { getPackageContent, restoreAction } from './actions/restore-action';
import zhCN from './locale/zh-CN';
export default class Duplicator extends Plugin {
beforeLoad() {
this.app.i18n.addResources('zh-CN', 'duplicator', zhCN);
addDumpCommand(this.app);
addRestoreCommand(this.app);
}
async load() {
this.app.resourcer.define({
name: 'duplicator',
middleware: async (ctx, next) => {
if (ctx.action.actionName !== 'upload') {
return next();
}
const storage = multer.diskStorage({
destination: os.tmpdir(), // 获取临时目录
filename: function (req, file, cb) {
const randomName = Date.now().toString() + Math.random().toString().slice(2); // 随机生成文件名
cb(null, randomName);
},
});
const upload = multer({ storage }).single('file');
return upload(ctx, next);
},
actions: {
restore: restoreAction,
upload: getPackageContent,
dump: dumpAction,
dumpableCollections: dumpableCollections,
getDict: getDictAction,
},
});
this.app.acl.allow('duplicator', 'getDict');
}
}