Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -66,6 +66,69 @@ app.post('/convert', (req, res) => {
|
|
| 66 |
});
|
| 67 |
});
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
app.get('/', (req, res) => res.send('Inkscape API is Running with Native Crypto...'));
|
| 70 |
|
| 71 |
const PORT = 7860;
|
|
|
|
| 66 |
});
|
| 67 |
});
|
| 68 |
|
| 69 |
+
app.post('/fill', (req, res) => {
|
| 70 |
+
let { svg } = req.body;
|
| 71 |
+
if (!svg) return res.status(400).send('No SVG provided');
|
| 72 |
+
|
| 73 |
+
// 预处理:强制设置黑色并确保有描边
|
| 74 |
+
let processedSvg = svg.replace(/currentColor/g, '#000000');
|
| 75 |
+
// 如果原 SVG 没写 fill,默认给它一个黑色填充,这样联合后才是实心的
|
| 76 |
+
if (!processedSvg.includes('fill=')) {
|
| 77 |
+
processedSvg = processedSvg.replace(/<path/g, '<path fill="#000000" ');
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
const id = crypto.randomUUID();
|
| 81 |
+
const inputFile = path.join('/tmp', `${id}_in.svg`);
|
| 82 |
+
const outputFile = path.join('/tmp', `${id}_out.svg`);
|
| 83 |
+
|
| 84 |
+
fs.writeFileSync(inputFile, processedSvg);
|
| 85 |
+
|
| 86 |
+
// 进阶版指令链:
|
| 87 |
+
// 1. 打开文件并全选
|
| 88 |
+
// 2. 复制一份 (此时有两层完全重合的对象)
|
| 89 |
+
// 3. 将其中一层执行描边转路径 (此时是一层填充 + 一层轮廓)
|
| 90 |
+
// 4. 再次全选并执行 union (将填充和轮廓融合成一个复杂路径)
|
| 91 |
+
const actions = [
|
| 92 |
+
`file-open:${inputFile}`,
|
| 93 |
+
'select-all',
|
| 94 |
+
'selection-duplicate',
|
| 95 |
+
'select-all',
|
| 96 |
+
'object-stroke-to-path',
|
| 97 |
+
'select-all',
|
| 98 |
+
'path-union',
|
| 99 |
+
`export-filename:${outputFile}`,
|
| 100 |
+
'export-do',
|
| 101 |
+
'file-close'
|
| 102 |
+
].join(';');
|
| 103 |
+
|
| 104 |
+
const command = `xvfb-run -a inkscape --actions="${actions}"`;
|
| 105 |
+
|
| 106 |
+
exec(command, (error, stdout, stderr) => {
|
| 107 |
+
if (error) {
|
| 108 |
+
console.error(`Inkscape Error: ${error.message}`);
|
| 109 |
+
return res.status(500).send('转换执行失败');
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
if (!fs.existsSync(outputFile)) {
|
| 113 |
+
return res.status(500).send('生成失败');
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
let result = fs.readFileSync(outputFile, 'utf8');
|
| 117 |
+
|
| 118 |
+
// 后处理
|
| 119 |
+
result = result.replace(/fill="#000000"/g, 'fill="currentColor"')
|
| 120 |
+
.replace(/stroke=".*?"/g, 'stroke="none"')
|
| 121 |
+
.replace(/stroke-width=".*?"/g, 'stroke-width="0"');
|
| 122 |
+
|
| 123 |
+
res.send(result);
|
| 124 |
+
|
| 125 |
+
try {
|
| 126 |
+
if(fs.existsSync(inputFile)) fs.unlinkSync(inputFile);
|
| 127 |
+
if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);
|
| 128 |
+
} catch (e) {}
|
| 129 |
+
});
|
| 130 |
+
});
|
| 131 |
+
|
| 132 |
app.get('/', (req, res) => res.send('Inkscape API is Running with Native Crypto...'));
|
| 133 |
|
| 134 |
const PORT = 7860;
|