Files
flypic/backend/src/routes/settings.js
T

131 lines
4.4 KiB
JavaScript

/**
* 应用设置路由
* GET /api/settings 读取(apiKey 掩码返回)
* PUT /api/settings 保存
* POST /api/settings/test-ocr 测试 OCR(本地或线上,返回识别文本与耗时)
*/
const express = require('express');
const fs = require('fs');
const path = require('path');
const { getSettings, saveSettings, maskKey } = require('../../utils/settings');
const { ocrWithConfig } = require('../../utils/ocr');
const router = express.Router();
router.get('/', (req, res) => {
const s = getSettings();
const ig = s.imagegen || {};
res.json({
success: true,
data: {
provider: s.ocr.provider,
online: {
url: s.ocr.online.url,
model: s.ocr.online.model,
apiKeyMasked: maskKey(s.ocr.online.apiKey),
configured: !!(s.ocr.online.url && s.ocr.online.apiKey)
},
telemetry: s.telemetry,
imagegen: {
provider: ig.provider === 'custom' ? 'custom' : 'newapi',
newapi: {
url: (ig.newapi && ig.newapi.url) || '',
apiKeyMasked: maskKey((ig.newapi && ig.newapi.apiKey) || ''),
// 真正生效的 key:设置里的或环境变量兜底
hasKey: !!((ig.newapi && ig.newapi.apiKey) || process.env.NEWAPI_KEY)
},
custom: {
url: (ig.custom && ig.custom.url) || '',
model: (ig.custom && ig.custom.model) || '',
apiKeyMasked: maskKey((ig.custom && ig.custom.apiKey) || ''),
configured: !!(ig.custom && ig.custom.url && ig.custom.apiKey)
}
}
}
});
});
router.put('/', (req, res) => {
try {
const saved = saveSettings(req.body || {});
const ig = saved.imagegen || {};
res.json({
success: true,
data: {
provider: saved.ocr.provider,
online: {
url: saved.ocr.online.url,
model: saved.ocr.online.model,
apiKeyMasked: maskKey(saved.ocr.online.apiKey),
configured: !!(saved.ocr.online.url && saved.ocr.online.apiKey)
},
telemetry: saved.telemetry,
imagegen: {
provider: ig.provider,
newapi: {
url: ig.newapi.url,
apiKeyMasked: maskKey(ig.newapi.apiKey),
hasKey: !!(ig.newapi.apiKey || process.env.NEWAPI_KEY)
},
custom: {
url: ig.custom.url,
model: ig.custom.model,
apiKeyMasked: maskKey(ig.custom.apiKey),
configured: !!(ig.custom.url && ig.custom.apiKey)
}
}
}
});
} catch (e) {
res.status(500).json({ success: false, message: e.message });
}
});
/** 测试 OCR:用一张内置样图(sharp 生成带文字 PNG)跑识别 */
router.post('/test-ocr', async (req, res) => {
try {
const body = req.body || {};
const saved = getSettings();
// 未传 apiKey 时回读已保存密钥(测试无需重新输入)
const onlineCfg = {
url: body.url || saved.ocr.online.url,
model: body.model || saved.ocr.online.model,
apiKey: body.apiKey || saved.ocr.online.apiKey
};
const cfg = body.provider === 'online'
? { provider: 'online', online: onlineCfg }
: { provider: 'local' };
if (cfg.provider === 'online' && (!cfg.online.url || !cfg.online.apiKey)) {
return res.status(400).json({ success: false, message: '线上 OCR 需要填写接口地址和 API Key' });
}
// 生成测试图:白底黑字 "FlyPic OCR Test 2026"(英文避免容器缺 CJK 字体出现豆腐块)
const sharp = require('sharp');
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="480" height="120">
<rect width="480" height="120" fill="white"/>
<text x="30" y="75" font-size="40" font-family="sans-serif" fill="black">FlyPic OCR Test 2026</text>
</svg>`;
const tmp = path.join(require('os').tmpdir(), `flypic_ocr_test_${Date.now()}.png`);
await sharp(Buffer.from(svg)).png().toFile(tmp);
const start = Date.now();
try {
const text = await ocrWithConfig(tmp, cfg);
const ms = Date.now() - start;
const ok = text && text.length > 0;
res.json({
success: ok,
data: { text: text || '', ms, provider: cfg.provider },
message: ok ? undefined : '识别成功但未提取到文字'
});
} finally {
fs.unlink(tmp, () => {});
}
} catch (e) {
res.status(500).json({ success: false, message: `OCR 测试失败: ${e.message}` });
}
});
module.exports = router;