feat(controlplane): 定时备份槽位支持流程编排 flows 字段
- POST/GET /api/schedules/backup 增加 flows 支持,schema_version=2 - 老客户端只推 schedules 时保留已有 flows,避免流程编排被抹掉 - 新增上限:flows 200 条、备份体 2MB(413/400 明确报错) - 同步旧 FastAPI 版 schedules.py(仅兼容维护,内网生产为 controlplane-next)
This commit is contained in:
@@ -10,6 +10,11 @@ import { ScheduleBackupEntity } from '../../entity/resource';
|
||||
import { CenterAuthService, ApiError } from '../../service/auth';
|
||||
import { AVATAR_DIR } from '../../comm/conf';
|
||||
|
||||
// 备份上限:1.8 起同一槽位还会存「流程编排」,需要防无限增长
|
||||
const MAX_BACKUP_BYTES = 2 * 1024 * 1024;
|
||||
const MAX_FLOWS = 200;
|
||||
const SCHEMA_VERSION = 2;
|
||||
|
||||
/**
|
||||
* 客户端杂项接口(对齐旧 FastAPI):
|
||||
* - GET /api/file-sources/{category}:内网大文件源最新启用直链
|
||||
@@ -98,7 +103,7 @@ export class ApiMiscController extends BaseController {
|
||||
};
|
||||
}
|
||||
|
||||
/** POST /api/schedules/backup — 覆盖保存某账号定时任务配置 */
|
||||
/** POST /api/schedules/backup — 覆盖保存某账号定时任务 + 流程编排配置 */
|
||||
@Post('/schedules/backup')
|
||||
async schedBackupSave(
|
||||
@Body() b: any,
|
||||
@@ -108,14 +113,42 @@ export class ApiMiscController extends BaseController {
|
||||
@Headers('X-Admin-User') xAdminUser: string = ''
|
||||
) {
|
||||
const account = await this.requireIdentity(authorization, xAdminToken, xApiToken, xAdminUser);
|
||||
const payload = b?.data && typeof b.data === 'object' ? b.data : {};
|
||||
const payload: any = b?.data && typeof b.data === 'object' ? { ...b.data } : {};
|
||||
if (!Array.isArray(payload.schedules)) throw new ApiError(400, 'data.schedules 必须为数组');
|
||||
if (payload.flows !== undefined && !Array.isArray(payload.flows)) {
|
||||
throw new ApiError(400, 'data.flows 必须为数组');
|
||||
}
|
||||
if (Array.isArray(payload.flows) && payload.flows.length > MAX_FLOWS) {
|
||||
throw new ApiError(400, `data.flows 最多 ${MAX_FLOWS} 条`);
|
||||
}
|
||||
// 老客户端(1.8 之前)只推 schedules:保留已有 flows,别把流程备份抹掉
|
||||
if (payload.flows === undefined) {
|
||||
const prev = await this.scheduleBackupRepo.findOneBy({ account } as any);
|
||||
let prevData: any = {};
|
||||
try {
|
||||
prevData = prev?.data ? JSON.parse(prev.data) : {};
|
||||
} catch (e) {
|
||||
prevData = {};
|
||||
}
|
||||
payload.flows = Array.isArray(prevData?.flows) ? prevData.flows : [];
|
||||
}
|
||||
payload.schema_version = SCHEMA_VERSION;
|
||||
const blob = JSON.stringify(payload);
|
||||
if (Buffer.byteLength(blob, 'utf8') > MAX_BACKUP_BYTES) {
|
||||
throw new ApiError(413, '备份内容过大(上限 2MB)');
|
||||
}
|
||||
await this.scheduleBackupRepo.manager.query(
|
||||
`INSERT INTO schedule_backups(account, data, updated_at) VALUES (?, ?, NOW())
|
||||
ON DUPLICATE KEY UPDATE data = VALUES(data), updated_at = VALUES(updated_at)`,
|
||||
[account, JSON.stringify(payload)]
|
||||
[account, blob]
|
||||
);
|
||||
return { ok: true, account };
|
||||
return {
|
||||
ok: true,
|
||||
account,
|
||||
schedules: payload.schedules.length,
|
||||
flows: payload.flows.length,
|
||||
schema_version: SCHEMA_VERSION,
|
||||
};
|
||||
}
|
||||
|
||||
/** GET /api/schedules/backup — 读取某账号定时任务备份 */
|
||||
@@ -127,15 +160,21 @@ export class ApiMiscController extends BaseController {
|
||||
@Headers('X-Admin-User') xAdminUser: string = ''
|
||||
) {
|
||||
const account = await this.requireIdentity(authorization, xAdminToken, xApiToken, xAdminUser);
|
||||
const row = await this.scheduleBackupRepo.findOneBy({ account });
|
||||
if (!row) return { data: { schedules: [] } };
|
||||
const empty = { data: { schedules: [], flows: [], schema_version: SCHEMA_VERSION } };
|
||||
const row = await this.scheduleBackupRepo.findOneBy({ account } as any);
|
||||
if (!row) return empty;
|
||||
try {
|
||||
const data = JSON.parse(row.data);
|
||||
if (data && typeof data === 'object' && Array.isArray(data.schedules)) return { data };
|
||||
if (data && typeof data === 'object' && Array.isArray(data.schedules)) {
|
||||
// 老数据没有 flows:补空数组,客户端不必判空
|
||||
if (!Array.isArray(data.flows)) data.flows = [];
|
||||
if (data.schema_version === undefined) data.schema_version = 1;
|
||||
return { data };
|
||||
}
|
||||
} catch (e) {
|
||||
/* fallthrough */
|
||||
}
|
||||
return { data: { schedules: [] } };
|
||||
return empty;
|
||||
}
|
||||
|
||||
/** GET /api/avatar/{account} — 读取账号头像(前端 img 直接引用) */
|
||||
|
||||
Reference in New Issue
Block a user