tachybase_todo/packages/plugin-file-manager/src/server.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

import path from 'path';
import { Plugin } from '@nocobase/server';
2021-12-06 21:23:34 +08:00
import { action as uploadAction, middleware as uploadMiddleware } from './actions/upload';
import { getStorageConfig } from './storages';
import { STORAGE_TYPE_LOCAL } from './constants';
export default class PluginFileManager extends Plugin {
storageType() {
return process.env.DEFAULT_STORAGE_TYPE;
}
async beforeLoad() {
this.app.on('installing', async () => {
const defaultStorageConfig = getStorageConfig(this.storageType());
if (defaultStorageConfig) {
const Storage = this.db.getCollection('storages');
2021-12-06 21:23:34 +08:00
await Storage.repository.create({
values: {
...defaultStorageConfig.defaults(),
type: this.storageType(),
2021-12-06 21:23:34 +08:00
default: true,
},
});
}
2021-09-11 18:53:26 +08:00
});
}
async load() {
await this.db.import({
directory: path.resolve(__dirname, 'collections'),
});
// 暂时中间件只能通过 use 加进来
this.app.resourcer.use(uploadMiddleware);
this.app.resourcer.registerActionHandler('upload', uploadAction);
if (process.env.NOCOBASE_ENV !== 'production') {
await getStorageConfig(STORAGE_TYPE_LOCAL).middleware(this.app);
}
}
}