import { Plugin } from '@nocobase/server'; import { resolve } from 'path'; import initActions from './actions'; import { getStorageConfig } from './storages'; export { default as storageTypes } from './storages'; export default class PluginFileManager extends Plugin { storageType() { return process.env.DEFAULT_STORAGE_TYPE ?? 'local'; } async install() { const defaultStorageConfig = getStorageConfig(this.storageType()); if (defaultStorageConfig) { const Storage = this.db.getCollection('storages'); if ( await Storage.repository.findOne({ filter: { name: defaultStorageConfig.defaults().name, }, }) ) { return; } await Storage.repository.create({ values: { ...defaultStorageConfig.defaults(), type: this.storageType(), default: true, }, }); } } async load() { await this.importCollections(resolve(__dirname, './collections')); this.app.acl.registerSnippet({ name: `pm.${this.name}.storages`, actions: ['storages:*'], }); this.db.addMigrations({ namespace: 'file-manager', directory: resolve(__dirname, 'migrations'), context: { plugin: this, }, }); initActions(this); this.app.acl.allow('attachments', 'upload', 'loggedIn'); this.app.acl.allow('attachments', 'create', 'loggedIn'); // this.app.resourcer.use(uploadMiddleware); // this.app.resourcer.use(createAction); // this.app.resourcer.registerActionHandler('upload', uploadAction); const defaultStorageName = getStorageConfig(this.storageType()).defaults().name; this.app.acl.addFixedParams('storages', 'destroy', () => { return { filter: { 'name.$ne': defaultStorageName }, }; }); const ownMerger = () => { return { filter: { createdById: '{{ctx.state.currentUser.id}}', }, }; }; this.app.acl.addFixedParams('attachments', 'update', ownMerger); this.app.acl.addFixedParams('attachments', 'create', ownMerger); this.app.acl.addFixedParams('attachments', 'destroy', ownMerger); } }