Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -13,10 +13,8 @@ app.post('/convert', (req, res) => {
|
|
| 13 |
let { svg } = req.body;
|
| 14 |
if (!svg) return res.status(400).send('No SVG provided');
|
| 15 |
|
| 16 |
-
//
|
| 17 |
-
|
| 18 |
-
let processedSvg = svg.replace(/currentColor/g, '#000000');
|
| 19 |
-
processedSvg = processedSvg.replace(/<path/g, '<path fill="none" stroke="#000000" stroke-width="2" ');
|
| 20 |
|
| 21 |
const id = uuidv4();
|
| 22 |
const inputFile = path.join('/tmp', `${id}_in.svg`);
|
|
@@ -24,44 +22,47 @@ app.post('/convert', (req, res) => {
|
|
| 24 |
|
| 25 |
fs.writeFileSync(inputFile, processedSvg);
|
| 26 |
|
| 27 |
-
//
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
exec(command, (error, stdout, stderr) => {
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
if (!fs.existsSync(outputFile)) {
|
| 39 |
-
return res.status(500).send('转换
|
| 40 |
}
|
| 41 |
|
| 42 |
let result = fs.readFileSync(outputFile, 'utf8');
|
| 43 |
-
|
| 44 |
-
// 3. 结果判断:如果结果里依然包含 stroke-width="2",说明转换没动
|
| 45 |
-
if (result.includes('stroke-width="2"') || result.includes('stroke="#000000"')) {
|
| 46 |
-
// 尝试第二套备选命令:使用 --export-plain-svg 触发旧版解析引擎
|
| 47 |
-
const backupCommand = `xvfb-run -a inkscape "${inputFile}" --export-plain-svg --export-type="svg" --export-filename="${outputFile}" --actions="select-all:all;path-outline"`;
|
| 48 |
-
exec(backupCommand, () => {
|
| 49 |
-
let finalResult = fs.readFileSync(outputFile, 'utf8');
|
| 50 |
-
res.send(cleanup(finalResult));
|
| 51 |
-
});
|
| 52 |
-
} else {
|
| 53 |
-
res.send(cleanup(result));
|
| 54 |
-
}
|
| 55 |
|
| 56 |
-
//
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
});
|
| 59 |
});
|
| 60 |
|
| 61 |
-
|
| 62 |
-
return svg.replace(/fill="#000000"/g, 'fill="currentColor"')
|
| 63 |
-
.replace(/stroke=".*?"/g, 'stroke="none"')
|
| 64 |
-
.replace(/stroke-width=".*?"/g, 'stroke-width="0"');
|
| 65 |
-
}
|
| 66 |
|
| 67 |
-
app.listen(7860);
|
|
|
|
| 13 |
let { svg } = req.body;
|
| 14 |
if (!svg) return res.status(400).send('No SVG provided');
|
| 15 |
|
| 16 |
+
// 预处理:Inkscape 命令行对 currentColor 识别较差,强制转为黑色
|
| 17 |
+
const processedSvg = svg.replace(/currentColor/g, '#000000');
|
|
|
|
|
|
|
| 18 |
|
| 19 |
const id = uuidv4();
|
| 20 |
const inputFile = path.join('/tmp', `${id}_in.svg`);
|
|
|
|
| 22 |
|
| 23 |
fs.writeFileSync(inputFile, processedSvg);
|
| 24 |
|
| 25 |
+
// 完全参考你本地成功的指令链
|
| 26 |
+
const actions = [
|
| 27 |
+
`file-open:${inputFile}`,
|
| 28 |
+
'select-all',
|
| 29 |
+
'object-stroke-to-path',
|
| 30 |
+
'path-union',
|
| 31 |
+
`export-filename:${outputFile}`,
|
| 32 |
+
'export-do',
|
| 33 |
+
'file-close'
|
| 34 |
+
].join(';');
|
| 35 |
+
|
| 36 |
+
// 在服务器环境,我们依然建议带上 xvfb-run 确保指令集被 100% 激活
|
| 37 |
+
const command = `xvfb-run -a inkscape --actions="${actions}"`;
|
| 38 |
|
| 39 |
exec(command, (error, stdout, stderr) => {
|
| 40 |
+
if (error) {
|
| 41 |
+
console.error(`Inkscape Error: ${error.message}`);
|
| 42 |
+
return res.status(500).send('Inkscape 转换执行失败');
|
| 43 |
+
}
|
| 44 |
|
| 45 |
if (!fs.existsSync(outputFile)) {
|
| 46 |
+
return res.status(500).send('转换成功但未生成文件');
|
| 47 |
}
|
| 48 |
|
| 49 |
let result = fs.readFileSync(outputFile, 'utf8');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
// 后处理:为了适配 Iconfont,将 fill 改回 currentColor
|
| 52 |
+
// 并移除可能残留的 stroke 属性
|
| 53 |
+
result = result.replace(/fill="#000000"/g, 'fill="currentColor"')
|
| 54 |
+
.replace(/stroke=".*?"/g, 'stroke="none"')
|
| 55 |
+
.replace(/stroke-width=".*?"/g, 'stroke-width="0"');
|
| 56 |
+
|
| 57 |
+
res.send(result);
|
| 58 |
+
|
| 59 |
+
// 延迟清理临时文件
|
| 60 |
+
setTimeout(() => {
|
| 61 |
+
[inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });
|
| 62 |
+
}, 1000);
|
| 63 |
});
|
| 64 |
});
|
| 65 |
|
| 66 |
+
app.get('/', (req, res) => res.send('Inkscape API Pro is Running...'));
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
app.listen(7860, () => console.log('Server is online with Inkscape Engine.'));
|