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-09-23 00:16:04 +08:00
|
|
|
import { PluginOptions, Plugin } from '@nocobase/server';
|
2020-11-11 15:23:39 +08:00
|
|
|
|
2020-12-19 08:45:19 +08:00
|
|
|
import {
|
|
|
|
action as uploadAction,
|
|
|
|
middleware as uploadMiddleware,
|
|
|
|
} from './actions/upload';
|
2020-12-23 12:46:13 +08:00
|
|
|
import {
|
|
|
|
middleware as localMiddleware,
|
|
|
|
} from './storages/local';
|
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;
|
|
|
|
|
|
|
|
database.import({
|
|
|
|
directory: path.resolve(__dirname, 'collections'),
|
2021-09-11 18:53:26 +08:00
|
|
|
});
|
2021-09-23 00:16:04 +08:00
|
|
|
|
|
|
|
// 暂时中间件只能通过 use 加进来
|
|
|
|
resourcer.use(uploadMiddleware);
|
|
|
|
resourcer.registerActionHandler('upload', uploadAction);
|
|
|
|
localMiddleware(this.app);
|
|
|
|
|
|
|
|
const Storage = database.getModel('storages');
|
|
|
|
|
|
|
|
this.app.on('db.init', async () => {
|
|
|
|
await Storage.create({
|
|
|
|
title: '本地存储',
|
|
|
|
name: `local`,
|
|
|
|
type: 'local',
|
|
|
|
baseUrl: process.env.LOCAL_STORAGE_BASE_URL,
|
|
|
|
default: process.env.STORAGE_TYPE === 'local',
|
|
|
|
});
|
|
|
|
await Storage.create({
|
|
|
|
name: `ali-oss`,
|
|
|
|
type: 'ali-oss',
|
|
|
|
baseUrl: process.env.ALI_OSS_STORAGE_BASE_URL,
|
|
|
|
options: {
|
|
|
|
region: process.env.ALI_OSS_REGION,
|
|
|
|
accessKeyId: process.env.ALI_OSS_ACCESS_KEY_ID,
|
|
|
|
accessKeySecret: process.env.ALI_OSS_ACCESS_KEY_SECRET,
|
|
|
|
bucket: process.env.ALI_OSS_BUCKET,
|
|
|
|
},
|
|
|
|
default: process.env.STORAGE_TYPE === 'ali-oss',
|
|
|
|
});
|
2021-09-11 18:53:26 +08:00
|
|
|
});
|
2021-09-23 00:16:04 +08:00
|
|
|
},
|
|
|
|
} as PluginOptions;
|