mokee commited on
Commit
3f513df
·
verified ·
1 Parent(s): 3d0e632

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +34 -33
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
- // 1. 强力预处理:给每个 path 强行注入颜色、宽度,并去掉 fill
17
- // 这是为了确保 Inkscape “一眼就能看到” 描边
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
- // 2. 这里的命令是关键:
28
- // 我们强制使用 --export-type="svg" 并配合特殊的动作
29
- // 如果 actions="path-outline" 还是不行,我们改用 "selection-unstroke" 的变体逻辑
30
- const command = `xvfb-run -a inkscape "${inputFile}" \
31
- --actions="select-all:all;object-to-path;path-outline;export-filename:${outputFile};export-do"`;
 
 
 
 
 
 
 
 
32
 
33
  exec(command, (error, stdout, stderr) => {
34
- // 打印出所有潜在的错误信息,方便我们在 Logs 查看
35
- if (stdout) console.log('Inkscape Stdout:', stdout);
36
- if (stderr) console.log('Inkscape Stderr:', stderr);
 
37
 
38
  if (!fs.existsSync(outputFile)) {
39
- return res.status(500).send('转换失败:输出文件未生成。请检查 Space Logs。');
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
- [inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });
 
 
 
 
 
 
 
 
 
 
58
  });
59
  });
60
 
61
- function cleanup(svg) {
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.'));