369 lines
13 KiB
JavaScript
369 lines
13 KiB
JavaScript
/**
|
||
* 图片编辑路由
|
||
* - POST /api/edit/ai 代理 AI 请求到 flypic-ai sidecar(抠图/高清/去水印/换脸)
|
||
* - POST /api/edit/save 保存编辑结果(覆盖原图 / 另存为新图),重建缩略图并更新数据库
|
||
*/
|
||
|
||
const express = require('express');
|
||
const router = express.Router();
|
||
const path = require('path');
|
||
const fs = require('fs');
|
||
const multer = require('multer');
|
||
const sharp = require('sharp');
|
||
const { asyncHandler } = require('../middleware/errorHandler');
|
||
const { processImage } = require('../../utils/scanner');
|
||
const { getSettings } = require('../../utils/settings');
|
||
const { normalizeOnlineUrl } = require('../../utils/ocr');
|
||
const { mapImageForFrontend } = require('../utils/fieldMapper');
|
||
const logger = require('../utils/logger');
|
||
|
||
const upload = multer({
|
||
storage: multer.memoryStorage(),
|
||
limits: { fileSize: 200 * 1024 * 1024 } // 200MB(8K 编辑结果可能较大)
|
||
});
|
||
|
||
const AI_BASE = process.env.AI_BASE_URL || 'http://127.0.0.1:15003';
|
||
|
||
// sidecar 端点映射
|
||
const AI_ENDPOINTS = {
|
||
upscale: '/upscale',
|
||
removebg: '/remove-bg',
|
||
inpaint: '/inpaint',
|
||
faceswap: '/face-swap'
|
||
};
|
||
|
||
/**
|
||
* POST /api/edit/ai
|
||
* FormData: action=upscale|removebg|inpaint|faceswap, image=file, [mask|source=file], [mode|scale|bg_color]
|
||
* 返回:image/png 二进制 或 { error }
|
||
*/
|
||
router.post(
|
||
'/ai',
|
||
upload.any(),
|
||
asyncHandler(async (req, res) => {
|
||
const { action } = req.body;
|
||
const endpoint = AI_ENDPOINTS[action];
|
||
if (!endpoint) {
|
||
return res.status(400).json({ error: `未知 AI 操作: ${action}` });
|
||
}
|
||
if (!req.files || req.files.length === 0) {
|
||
return res.status(400).json({ error: '缺少图片文件' });
|
||
}
|
||
|
||
// 组装转发表单
|
||
const form = new FormData();
|
||
for (const f of req.files) {
|
||
form.append(f.fieldname, new Blob([f.buffer], { type: f.mimetype || 'image/png' }), f.filename || 'image.png');
|
||
}
|
||
for (const [k, v] of Object.entries(req.body)) {
|
||
if (k !== 'action' && v !== undefined && v !== null) form.append(k, String(v));
|
||
}
|
||
|
||
try {
|
||
const upstream = await fetch(`${AI_BASE}${endpoint}`, {
|
||
method: 'POST',
|
||
body: form,
|
||
signal: AbortSignal.timeout(300 * 1000) // AI 处理最长 5 分钟
|
||
});
|
||
|
||
const contentType = upstream.headers.get('content-type') || '';
|
||
if (!upstream.ok) {
|
||
let msg = `AI 服务错误 (${upstream.status})`;
|
||
try {
|
||
const j = await upstream.json();
|
||
msg = j.detail || j.error || msg;
|
||
} catch (_) {
|
||
/* 非 JSON 错误体 */
|
||
}
|
||
logger.warn(`⚠️ AI ${action} 失败: ${msg}`);
|
||
return res.status(502).json({ error: msg });
|
||
}
|
||
|
||
if (contentType.includes('application/json')) {
|
||
const j = await upstream.json();
|
||
return res.status(502).json({ error: j.detail || j.error || 'AI 服务返回异常' });
|
||
}
|
||
|
||
const buf = Buffer.from(await upstream.arrayBuffer());
|
||
res.set('Content-Type', 'image/png');
|
||
res.set('X-AI-Action', action);
|
||
return res.send(buf);
|
||
} catch (err) {
|
||
const timedOut = err.name === 'TimeoutError' || err.name === 'AbortError';
|
||
logger.error(`❌ AI ${action} 代理失败: ${err.message}`);
|
||
return res.status(502).json({
|
||
error: timedOut ? 'AI 处理超时(超过 5 分钟),请缩小图片后重试' : `AI 服务不可达: ${err.message}`
|
||
});
|
||
}
|
||
})
|
||
);
|
||
|
||
const IMAGE_EXTS = new Set(['.jpg', '.jpeg', '.png', '.webp']);
|
||
const SHARP_FORMAT = { '.jpg': 'jpeg', '.jpeg': 'jpeg', '.png': 'png', '.webp': 'webp' };
|
||
|
||
/**
|
||
* POST /api/edit/save
|
||
* FormData: image=file(编辑结果 PNG), libraryId, path(原相对路径), mode=overwrite|new
|
||
* 返回:{ path, filename, mode, regenerated }
|
||
*/
|
||
router.post(
|
||
'/save',
|
||
upload.single('image'),
|
||
asyncHandler(async (req, res) => {
|
||
const { libraryId, path: relPath, mode = 'overwrite' } = req.body;
|
||
if (!req.file || !libraryId || !relPath) {
|
||
return res.status(400).json({ error: '缺少图片、素材库或路径参数' });
|
||
}
|
||
|
||
const configManager = req.app.get('configManager');
|
||
const dbPool = req.app.get('dbPool');
|
||
if (!configManager || !dbPool) {
|
||
return res.status(500).json({ error: '服务未初始化' });
|
||
}
|
||
|
||
const config = configManager.load();
|
||
const library = config.libraries.find((lib) => String(lib.id) === String(libraryId));
|
||
if (!library) {
|
||
return res.status(404).json({ error: '素材库不存在' });
|
||
}
|
||
|
||
const libraryPath = library.path;
|
||
const srcAbs = path.join(libraryPath, relPath);
|
||
const ext = path.extname(srcAbs).toLowerCase();
|
||
const db = dbPool.acquire(libraryPath);
|
||
|
||
let targetAbs;
|
||
let outFormat;
|
||
let finalRel;
|
||
|
||
if (mode === 'overwrite') {
|
||
if (!IMAGE_EXTS.has(ext)) {
|
||
// 原图格式不支持原地转码(gif/bmp/tiff 等)→ 自动降级为另存新图 PNG
|
||
const base = path.basename(relPath, path.extname(relPath));
|
||
const dir = path.dirname(relPath);
|
||
finalRel = uniqueRel(libraryPath, dir, `${base}-编辑`, '.png');
|
||
targetAbs = path.join(libraryPath, finalRel);
|
||
outFormat = 'png';
|
||
} else {
|
||
targetAbs = srcAbs;
|
||
finalRel = relPath;
|
||
outFormat = SHARP_FORMAT[ext];
|
||
}
|
||
} else {
|
||
const base = path.basename(relPath, path.extname(relPath));
|
||
const dir = path.dirname(relPath);
|
||
const preferExt = IMAGE_EXTS.has(ext) ? ext : '.png';
|
||
finalRel = uniqueRel(libraryPath, dir, `${base}-编辑`, preferExt);
|
||
targetAbs = path.join(libraryPath, finalRel);
|
||
outFormat = SHARP_FORMAT[preferExt] || 'png';
|
||
}
|
||
|
||
// 转码写盘
|
||
let pipeline = sharp(req.file.buffer);
|
||
if (outFormat === 'jpeg') {
|
||
pipeline = pipeline.flatten({ background: '#ffffff' }).jpeg({ quality: 92 });
|
||
} else if (outFormat === 'webp') {
|
||
pipeline = pipeline.webp({ quality: 92 });
|
||
} else {
|
||
pipeline = pipeline.png();
|
||
}
|
||
|
||
fs.mkdirSync(path.dirname(targetAbs), { recursive: true });
|
||
await pipeline.toFile(targetAbs);
|
||
|
||
// 重建缩略图 + 更新数据库(文件 hash 变化会触发完整更新)
|
||
let regenerated = false;
|
||
try {
|
||
await processImage(targetAbs, libraryPath, db);
|
||
regenerated = true;
|
||
} catch (err) {
|
||
logger.error(`❌ 编辑保存后处理失败: ${err.message}`);
|
||
}
|
||
|
||
const stat = fs.statSync(targetAbs);
|
||
logger.info(
|
||
`✅ 编辑保存 [${mode}] ${relPath} → ${finalRel} (${(stat.size / 1024).toFixed(0)}KB)`
|
||
);
|
||
|
||
return res.json({
|
||
success: true,
|
||
path: finalRel.replace(/\\/g, '/'),
|
||
filename: path.basename(finalRel),
|
||
mode: mode === 'overwrite' && finalRel === relPath ? 'overwrite' : 'new',
|
||
originalPath: relPath,
|
||
size: stat.size,
|
||
regenerated,
|
||
modifiedAt: Math.floor(stat.mtimeMs / 1000)
|
||
});
|
||
})
|
||
);
|
||
|
||
/**
|
||
* POST /api/edit/find-similar — AI 自动从图库找与种子图相似的图片(合拼功能)
|
||
* body: { libraryId, seedPath, count, excludePaths? }
|
||
* 依赖设置中的线上模型 KEY(与 OCR 线上配置共用)
|
||
* 返回: { success, images: [前端格式图片记录] }
|
||
*/
|
||
router.post(
|
||
'/find-similar',
|
||
asyncHandler(async (req, res) => {
|
||
const { libraryId, seedPath, count = 3, excludePaths = [] } = req.body || {};
|
||
if (!libraryId || !seedPath) {
|
||
return res.status(400).json({ success: false, error: { message: '缺少 libraryId 或 seedPath' } });
|
||
}
|
||
|
||
const s = getSettings();
|
||
const online = s.ocr && s.ocr.online;
|
||
if (!online || !online.url || !online.apiKey) {
|
||
return res.status(400).json({
|
||
success: false,
|
||
error: { message: '未配置模型 KEY:请先在「设置 → OCR 识别」填写线上模型接口和 API Key' }
|
||
});
|
||
}
|
||
|
||
const configManager = req.app.get('configManager');
|
||
const dbPool = req.app.get('dbPool');
|
||
const config = configManager.load();
|
||
const library = (config.libraries || []).find((l) => String(l.id) === String(libraryId));
|
||
if (!library) {
|
||
return res.status(404).json({ success: false, error: { message: '图库不存在' } });
|
||
}
|
||
|
||
const want = Math.max(1, Math.min(9, parseInt(count, 10) || 3));
|
||
const conn = dbPool.acquire(library.path);
|
||
try {
|
||
const raw = conn.db; // better-sqlite3 实例
|
||
const seed = raw.prepare('SELECT * FROM images WHERE path = ?').get(seedPath);
|
||
if (!seed) {
|
||
return res.status(404).json({ success: false, error: { message: '种子图不存在' } });
|
||
}
|
||
|
||
// 候选采样:优先同文件夹,不足再全库随机;排除种子与已选
|
||
const exclude = [seedPath, ...(Array.isArray(excludePaths) ? excludePaths : [])].filter(Boolean);
|
||
const ph = exclude.map(() => '?').join(',');
|
||
const SAMPLE = 20;
|
||
let cands = raw
|
||
.prepare(
|
||
`SELECT * FROM images WHERE folder = ? AND path NOT IN (${ph}) ORDER BY RANDOM() LIMIT ?`
|
||
)
|
||
.all(seed.folder, ...exclude, SAMPLE);
|
||
if (cands.length < SAMPLE) {
|
||
const more = raw
|
||
.prepare(`SELECT * FROM images WHERE path NOT IN (${ph}) ORDER BY RANDOM() LIMIT ?`)
|
||
.all(...exclude, SAMPLE - cands.length);
|
||
cands = cands.concat(more);
|
||
}
|
||
if (!cands.length) {
|
||
return res.status(400).json({ success: false, error: { message: '图库中没有其他图片可供选择' } });
|
||
}
|
||
|
||
// 缩略图 → 小尺寸 JPEG base64(节省 token)
|
||
const toDataURI = async (rec) => {
|
||
const abs = rec.thumbnail_path
|
||
? path.join(library.path, rec.thumbnail_path)
|
||
: path.join(library.path, rec.path);
|
||
const buf = await sharp(abs)
|
||
.resize(384, 384, { fit: 'inside', withoutEnlargement: true })
|
||
.jpeg({ quality: 70 })
|
||
.toBuffer();
|
||
return `data:image/jpeg;base64,${buf.toString('base64')}`;
|
||
};
|
||
|
||
const seedURI = await toDataURI(seed);
|
||
const candURIs = [];
|
||
const validCands = [];
|
||
for (const c of cands) {
|
||
try {
|
||
candURIs.push(await toDataURI(c));
|
||
validCands.push(c);
|
||
} catch (_) { /* 跳过损坏缩略图 */ }
|
||
}
|
||
if (!validCands.length) {
|
||
return res.status(500).json({ success: false, error: { message: '候选图缩略图读取失败' } });
|
||
}
|
||
|
||
const prompt =
|
||
`第一张是参考图。后面有 ${validCands.length} 张候选图,按出现顺序编号 1 到 ${validCands.length}。` +
|
||
`请选出与参考图在内容、主题、风格上最相似的 ${want} 张,按相似度从高到低排列,` +
|
||
`只返回一个 JSON 数组,元素为候选图编号,例如 [3,7,12],不要输出任何其他文字。`;
|
||
|
||
const content = [
|
||
{ type: 'text', text: prompt },
|
||
{ type: 'image_url', image_url: { url: seedURI } },
|
||
...candURIs.map((u) => ({ type: 'image_url', image_url: { url: u } }))
|
||
];
|
||
|
||
const ctrl = new AbortController();
|
||
const timer = setTimeout(() => ctrl.abort(), 90000);
|
||
let text;
|
||
try {
|
||
const r = await fetch(normalizeOnlineUrl(online.url), {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
Authorization: `***}`
|
||
},
|
||
body: JSON.stringify({
|
||
model: online.model || 'gpt-4o-mini',
|
||
max_tokens: 500,
|
||
messages: [{ role: 'user', content }]
|
||
}),
|
||
signal: ctrl.signal
|
||
});
|
||
const data = await r.json();
|
||
if (!r.ok || data.error) {
|
||
throw new Error((data.error && data.error.message) || `HTTP ${r.status}`);
|
||
}
|
||
text = data.choices && data.choices[0] && data.choices[0].message
|
||
? data.choices[0].message.content
|
||
: '';
|
||
} finally {
|
||
clearTimeout(timer);
|
||
}
|
||
|
||
const m = String(text).match(/\[[\s\d,]+\]/);
|
||
if (!m) {
|
||
logger.warn(`find-similar 模型返回无法解析: ${String(text).slice(0, 200)}`);
|
||
return res.status(502).json({ success: false, error: { message: '模型返回格式无法解析,请重试' } });
|
||
}
|
||
let idx;
|
||
try {
|
||
idx = JSON.parse(m[0]);
|
||
} catch (_) {
|
||
return res.status(502).json({ success: false, error: { message: '模型返回格式无法解析,请重试' } });
|
||
}
|
||
const picked = [];
|
||
const seen = new Set();
|
||
for (const i of idx) {
|
||
const n = parseInt(i, 10);
|
||
if (n >= 1 && n <= validCands.length && !seen.has(n)) {
|
||
seen.add(n);
|
||
picked.push(validCands[n - 1]);
|
||
}
|
||
if (picked.length >= want) break;
|
||
}
|
||
if (!picked.length) {
|
||
return res.status(502).json({ success: false, error: { message: '未找到相似图片,请重试' } });
|
||
}
|
||
|
||
logger.info(`find-similar: 种子=${seedPath} 候选=${validCands.length} 命中=${picked.length}`);
|
||
res.json({ success: true, images: picked.map(mapImageForFrontend) });
|
||
} finally {
|
||
dbPool.release(library.path);
|
||
}
|
||
})
|
||
);
|
||
|
||
/** 生成不冲突的相对路径:<base>.<ext> / <base>(1).<ext> ... */
|
||
function uniqueRel(libraryPath, dir, baseName, ext) {
|
||
let candidate = path.join(dir, `${baseName}${ext}`).replace(/\\/g, '/');
|
||
let i = 0;
|
||
while (fs.existsSync(path.join(libraryPath, candidate))) {
|
||
i += 1;
|
||
candidate = path.join(dir, `${baseName}(${i})${ext}`).replace(/\\/g, '/');
|
||
}
|
||
return candidate;
|
||
}
|
||
|
||
module.exports = router;
|