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

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +29 -28
server.js CHANGED
@@ -13,10 +13,10 @@ app.post('/convert', (req, res) => {
13
  let { svg } = req.body;
14
  if (!svg) return res.status(400).send('No SVG provided');
15
 
16
- // 1. 预处理:强制把所有描边设为 2px 黑色,并加上特定的 ID,方便 Inkscape 抓取
17
- // 同时把 currentColor 替换掉,防止识别失败
18
  let processedSvg = svg.replace(/currentColor/g, '#000000');
19
- processedSvg = processedSvg.replace(/<path/g, '<path id="target_path" fill="none" stroke="#000000" stroke-width="2"');
20
 
21
  const id = uuidv4();
22
  const inputFile = path.join('/tmp', `${id}_in.svg`);
@@ -24,43 +24,44 @@ app.post('/convert', (req, res) => {
24
 
25
  fs.writeFileSync(inputFile, processedSvg);
26
 
27
- // 修改 server.js 中的 command 变量部分
28
- const actions = [
29
- 'select-all:all',
30
- 'object-to-path',
31
- 'path-outline',
32
- `export-filename:${outputFile}`,
33
- 'export-do'
34
- ].join(';');
35
-
36
- // 使用 xvfb-run -a 自动分配显示屏启动 Inkscape
37
- const command = `xvfb-run -a inkscape "${inputFile}" --actions="${actions}"`;
38
-
39
- console.log('Executing:', command); // 在 Runtime Logs 中打印命令
40
 
41
  exec(command, (error, stdout, stderr) => {
 
 
42
  if (stderr) console.log('Inkscape Stderr:', stderr);
43
 
44
  if (!fs.existsSync(outputFile)) {
45
- console.error('File not found at:', outputFile);
46
- return res.status(500).send('Inkscape 转换动作未执行,请检查 Logs');
47
  }
48
 
49
  let result = fs.readFileSync(outputFile, 'utf8');
50
 
51
- // 3. 后处理:清理 Inkscape 产生的杂质
52
- result = result.replace(/stroke-width=".*?"/g, 'stroke-width="0"');
53
- result = result.replace(/stroke=".*?"/g, 'stroke="none"');
54
- result = result.replace(/fill="#000000"/g, 'fill="currentColor"');
55
-
56
- res.send(result);
 
 
 
 
 
57
 
58
- // 清理
59
  [inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });
60
  });
61
  });
62
 
63
- app.get('/', (req, res) => res.send('Inkscape API is Running...'));
 
 
 
 
64
 
65
- const PORT = 7860;
66
- app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
 
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
 
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);