2022-02-07 01:14:00 +08:00
|
|
|
import { Plugin } from '@nocobase/server';
|
2022-02-11 18:13:14 +08:00
|
|
|
import { resolve } from 'path';
|
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 { STORAGE_TYPE_LOCAL } from './constants';
|
2022-02-11 18:13:14 +08:00
|
|
|
import { getStorageConfig } from './storages';
|
2020-12-19 08:45:19 +08:00
|
|
|
|
2022-02-07 01:14:00 +08:00
|
|
|
export default class PluginFileManager extends Plugin {
|
|
|
|
storageType() {
|
|
|
|
return process.env.DEFAULT_STORAGE_TYPE;
|
|
|
|
}
|
2022-02-11 18:13:14 +08:00
|
|
|
|
2022-02-28 21:49:50 +08:00
|
|
|
async install() {
|
|
|
|
const defaultStorageConfig = getStorageConfig(this.storageType());
|
|
|
|
if (defaultStorageConfig) {
|
|
|
|
const Storage = this.db.getCollection('storages');
|
|
|
|
await Storage.repository.create({
|
|
|
|
values: {
|
|
|
|
...defaultStorageConfig.defaults(),
|
|
|
|
type: this.storageType(),
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-02-07 01:14:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
await this.db.import({
|
2022-02-11 18:13:14 +08:00
|
|
|
directory: resolve(__dirname, 'collections'),
|
2022-02-07 01:14:00 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// 暂时中间件只能通过 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);
|
|
|
|
}
|
2022-04-12 12:02:58 +08:00
|
|
|
|
2022-04-24 10:14:46 +08:00
|
|
|
this.app.acl.allow('attachments', 'upload', 'loggedIn');
|
2022-02-07 01:14:00 +08:00
|
|
|
}
|
2022-03-28 22:01:10 +08:00
|
|
|
|
|
|
|
getName(): string {
|
|
|
|
return this.getPackageName(__dirname);
|
|
|
|
}
|
2022-02-07 01:14:00 +08:00
|
|
|
}
|