mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 12:54:05 +00:00
fix(plugin-file-manager): fix local serve middleware (#1226)
This commit is contained in:
parent
0480b57db5
commit
41f8e6a285
@ -533,7 +533,7 @@ export default {
|
||||
'Add storage': '添加文件存储',
|
||||
'Edit storage': '编辑文件存储',
|
||||
'Storage base URL': 'Base URL',
|
||||
'Destination': '存储路径(绝对)',
|
||||
'Destination': '存储路径',
|
||||
'Use the built-in static file server': '使用内置静态文件服务',
|
||||
'Local storage': '本地存储',
|
||||
'Aliyun OSS': '阿里云 OSS',
|
||||
|
@ -3,6 +3,7 @@ import serve from 'koa-static';
|
||||
import mkdirp from 'mkdirp';
|
||||
import multer from 'multer';
|
||||
import path from 'path';
|
||||
import { Transactionable } from 'sequelize/types';
|
||||
import { URL } from 'url';
|
||||
import { STORAGE_TYPE_LOCAL } from '../constants';
|
||||
import { getFilename } from '../utils';
|
||||
@ -21,13 +22,14 @@ function match(basePath: string, pathname: string): boolean {
|
||||
return newPath[0] === '/';
|
||||
}
|
||||
|
||||
async function update(app: Application, storages) {
|
||||
async function refresh(app: Application, storages, options?: Transactionable) {
|
||||
const Storage = app.db.getCollection('storages');
|
||||
|
||||
const items = await Storage.repository.find({
|
||||
filter: {
|
||||
type: STORAGE_TYPE_LOCAL,
|
||||
},
|
||||
transaction: options?.transaction
|
||||
});
|
||||
|
||||
const primaryKey = Storage.model.primaryKeyAttribute;
|
||||
@ -39,9 +41,9 @@ async function update(app: Application, storages) {
|
||||
}
|
||||
|
||||
function createLocalServerUpdateHook(app, storages) {
|
||||
return async function (row) {
|
||||
return async function (row, options) {
|
||||
if (row.get('type') === STORAGE_TYPE_LOCAL) {
|
||||
await update(app, storages);
|
||||
await refresh(app, storages, options);
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -52,19 +54,16 @@ function getDocumentRoot(storage): string {
|
||||
return path.resolve(path.isAbsolute(documentRoot) ? documentRoot : path.join(process.cwd(), documentRoot));
|
||||
}
|
||||
|
||||
async function middleware(app: Application, options?) {
|
||||
const LOCALHOST = `http://localhost:${process.env.APP_PORT || '13000'}`;
|
||||
|
||||
async function middleware(app: Application) {
|
||||
const Storage = app.db.getCollection('storages');
|
||||
const storages = new Map<string, any>();
|
||||
|
||||
const localServerUpdateHook = createLocalServerUpdateHook(app, storages);
|
||||
Storage.model.addHook('afterCreate', localServerUpdateHook);
|
||||
Storage.model.addHook('afterUpdate', localServerUpdateHook);
|
||||
Storage.model.addHook('afterSave', localServerUpdateHook);
|
||||
Storage.model.addHook('afterDestroy', localServerUpdateHook);
|
||||
|
||||
app.on('beforeStart', async () => {
|
||||
await update(app, storages);
|
||||
await refresh(app, storages);
|
||||
});
|
||||
|
||||
app.use(async function (ctx, next) {
|
||||
@ -92,13 +91,15 @@ async function middleware(app: Application, options?) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return serve(getDocumentRoot(storage), {
|
||||
// for handle files after any api handlers
|
||||
defer: true,
|
||||
})(ctx, async () => {
|
||||
if (ctx.path.startsWith(basePath)) {
|
||||
ctx.path = ctx.path.replace(basePath, '');
|
||||
|
||||
const documentRoot = getDocumentRoot(storage);
|
||||
|
||||
return serve(documentRoot)(ctx, async () => {
|
||||
if (ctx.status == 404) {
|
||||
return;
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user