fix(api): 位图图标(3D PNG)在 SVG/JSON 响应中内联为 data URI,外部调用无需再取静态资源

This commit is contained in:
sgp
2026-11-05 11:38:28 +08:00
parent fcb70e8c7f
commit 5c18bf864c
+33 -3
View File
@@ -601,9 +601,39 @@ function pickRandom(sources, opts) {
return out; return out;
} }
/** 静态站根目录(位图图标原图在此读取后内联为 data URI) */
const STATIC_ROOT = (() => {
const env = process.env.DRAWLOGO_ROOT;
if (env) return path.resolve(env);
const sibling = path.resolve(ROOT, '..', 'drawlogo');
if (fs.existsSync(path.join(sibling, 'js', 'data', 'sources'))) return sibling;
return path.resolve(ROOT, '..');
})();
/** 位图图标原图 → data URI(按相对路径缓存,限制条目数防内存膨胀) */
const rasterCache = new Map();
function readRasterDataURI(rel) {
if (rasterCache.has(rel)) return rasterCache.get(rel);
const abs = path.join(STATIC_ROOT, rel);
const buf = fs.readFileSync(abs);
const uri = 'data:image/png;base64,' + buf.toString('base64');
if (rasterCache.size > 300) rasterCache.clear();
rasterCache.set(rel, uri);
return uri;
}
/** 把 <image href="js/data/sources/...png"> 内联为 data URI(外部调用方无需再取静态资源) */
function inlineRasterSvg(body) {
const s = String(body || '');
if (s.indexOf('js/data/sources/') === -1) return s;
return s.replace(/href="(js\/data\/sources\/[^"]+\.png)"/g, (m, rel) => {
try { return 'href="' + readRasterDataURI(rel) + '"'; } catch (e) { return m; }
});
}
/** 构造可直接嵌入的 SVG */ /** 构造可直接嵌入的 SVG */
function toSvg(item) { function toSvg(item) {
const body = String(item.svg || ''); const body = inlineRasterSvg(item.svg);
if (!body) return null; if (!body) return null;
const multiColor = item.type === 'emoji' || item.type === 'brand'; const multiColor = item.type === 'emoji' || item.type === 'brand';
const fill = multiColor ? '' : ' fill="currentColor"'; const fill = multiColor ? '' : ' fill="currentColor"';
@@ -755,7 +785,7 @@ async function handle(req, res) {
return res.end(svg); return res.end(svg);
} }
return ok(res, { ...item, source: sourceId, sourceNameCn: src.nameCn || sourceId, license: src.license, commercial: src.commercial }, null, rateHeaders); return ok(res, { ...item, svg: inlineRasterSvg(item.svg), source: sourceId, sourceNameCn: src.nameCn || sourceId, license: src.license, commercial: src.commercial }, null, rateHeaders);
} }
/* ---------- 海报 ---------- */ /* ---------- 海报 ---------- */
@@ -779,7 +809,7 @@ async function handle(req, res) {
if (i < 0) return fail(res, 404, 'NOT_FOUND', `素材不存在: ${sourceId}/${rawName}`); if (i < 0) return fail(res, 404, 'NOT_FOUND', `素材不存在: ${sourceId}/${rawName}`);
const item = readItem(src, i); const item = readItem(src, i);
if (!item) return fail(res, 500, 'INTERNAL', '读取素材失败'); if (!item) return fail(res, 500, 'INTERNAL', '读取素材失败');
return ok(res, { ...item, source: sourceId, sourceNameCn: src.nameCn || sourceId, license: src.license, commercial: src.commercial }, null, rateHeaders); return ok(res, { ...item, svg: inlineRasterSvg(item.svg), source: sourceId, sourceNameCn: src.nameCn || sourceId, license: src.license, commercial: src.commercial }, null, rateHeaders);
} }
/* ---------- 技能(元数据) ---------- */ /* ---------- 技能(元数据) ---------- */