fix: clarify upload errors and raise upload limits

This commit is contained in:
john
2026-07-04 00:17:58 +08:00
parent 702871a98c
commit 1ac9ad0277
8 changed files with 122 additions and 26 deletions
+26 -1
View File
@@ -148,6 +148,7 @@ import {
} from './mindspace-chat-docx-package.mjs';
import { scanContent } from './mindspace-content-scan.mjs';
import { renderImageAssetViewerHtml, wantsInlineImageViewer } from './mindspace-asset-preview.mjs';
import { DEFAULT_IMAGE_UPLOAD_MAX_BYTES } from './user-image-normalize.mjs';
import { createRechargeService } from './billing-recharge.mjs';
import { createSubscriptionService, createPlanCatalogService, ensurePlanCatalogSchema, PLAN_CATALOG } from './billing-subscription.mjs';
import {
@@ -268,7 +269,7 @@ const jsonUnlessMultipart = (req, res, next) => {
};
const rawUploadBody = express.raw({
type: 'application/octet-stream',
limit: mindSpaceServerRuntime.maxFileBytes,
limit: Math.max(mindSpaceServerRuntime.maxFileBytes, DEFAULT_IMAGE_UPLOAD_MAX_BYTES),
});
const wikiAuth = createWikiAuth(path.join(resolveMindSpacePublishRoot(__dirname), 'wiki-db'));
@@ -2188,6 +2189,28 @@ function mindSpaceError(res, req, error) {
return sendError(res, req, status, code, message, error?.details);
}
function isRequestBodyTooLarge(error) {
return (
error?.type === 'entity.too.large' ||
error?.status === 413 ||
error?.statusCode === 413
);
}
function apiRequestBodyError(error, req, res, next) {
if (!isRequestBodyTooLarge(error)) return next(error);
const isMindSpaceUpload = req.path.startsWith('/mindspace/v1/uploads/');
return sendError(
res,
req,
413,
isMindSpaceUpload ? 'file_too_large' : 'request_body_too_large',
isMindSpaceUpload
? '上传文件超过单文件大小限制,请压缩后重试'
: '请求内容过大,请缩小后重试',
);
}
function ensureMindSpaceEnabled(res, req, { upload = false, agent = false } = {}) {
try {
assertMindSpaceRoute(msFlags, upload ? 'upload' : agent ? 'agent' : undefined);
@@ -4573,6 +4596,8 @@ api.get('/sessions/:sessionId/events', async (req, res, next) => {
});
});
api.use(apiRequestBodyError);
api.use(async (req, res, next) => {
await userAuthReady;
if (!userAuth || !tkmindProxy) return next();