2020-11-11 15:23:39 +08:00
|
|
|
import path from 'path';
|
2020-12-23 12:46:13 +08:00
|
|
|
import Database from '@nocobase/database';
|
2020-11-11 15:23:39 +08:00
|
|
|
import Resourcer from '@nocobase/resourcer';
|
2021-12-04 07:58:31 +08:00
|
|
|
import { PluginOptions } from '@nocobase/server';
|
2020-11-11 15:23:39 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
import { action as uploadAction, middleware as uploadMiddleware } from './actions/upload';
|
2021-12-04 07:58:31 +08:00
|
|
|
import { getStorageConfig } from './storages';
|
|
|
|
import { STORAGE_TYPE_LOCAL } from './constants';
|
2020-12-19 08:45:19 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
export default {
|
|
|
|
name: 'file-manager',
|
|
|
|
async load() {
|
|
|
|
const database: Database = this.app.db;
|
|
|
|
const resourcer: Resourcer = this.app.resourcer;
|
2021-12-06 21:23:34 +08:00
|
|
|
|
|
|
|
await database.import({
|
2021-09-23 00:16:04 +08:00
|
|
|
directory: path.resolve(__dirname, 'collections'),
|
2021-09-11 18:53:26 +08:00
|
|
|
});
|
2021-12-06 21:23:34 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
// 暂时中间件只能通过 use 加进来
|
|
|
|
resourcer.use(uploadMiddleware);
|
|
|
|
resourcer.registerActionHandler('upload', uploadAction);
|
2021-12-03 07:31:22 +08:00
|
|
|
|
2021-12-04 07:58:31 +08:00
|
|
|
const { DEFAULT_STORAGE_TYPE } = process.env;
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
if (process.env.NOCOBASE_ENV !== 'production') {
|
2021-12-04 07:58:31 +08:00
|
|
|
await getStorageConfig(STORAGE_TYPE_LOCAL).middleware(this.app);
|
2021-12-03 07:31:22 +08:00
|
|
|
}
|
2021-12-06 21:23:34 +08:00
|
|
|
|
2021-09-23 00:16:04 +08:00
|
|
|
this.app.on('db.init', async () => {
|
2021-12-04 07:58:31 +08:00
|
|
|
const defaultStorageConfig = getStorageConfig(DEFAULT_STORAGE_TYPE);
|
|
|
|
if (defaultStorageConfig) {
|
2021-12-06 21:23:34 +08:00
|
|
|
const Storage = database.getCollection('storages');
|
|
|
|
await Storage.repository.create({
|
|
|
|
values: {
|
|
|
|
...defaultStorageConfig.defaults(),
|
|
|
|
type: DEFAULT_STORAGE_TYPE,
|
|
|
|
default: true,
|
|
|
|
},
|
2021-12-04 07:58:31 +08:00
|
|
|
});
|
|
|
|
}
|
2021-09-11 18:53:26 +08:00
|
|
|
});
|
2021-09-23 00:16:04 +08:00
|
|
|
},
|
|
|
|
} as PluginOptions;
|