mokee commited on
Commit
7c89f99
·
verified ·
1 Parent(s): 7343c6b

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +31 -25
server.js CHANGED
@@ -9,54 +9,60 @@ const app = express();
9
  app.use(cors());
10
  app.use(express.json({ limit: '10mb' }));
11
 
12
- app.get('/', (req, res) => {
13
- res.send('🚀 Inkscape SVG 轮廓转换 API 已启动!请使用 POST 请求 /convert 接口。');
14
- });
15
  app.post('/convert', (req, res) => {
16
  let { svg } = req.body;
17
  if (!svg) return res.status(400).send('No SVG provided');
18
 
19
- // --- 新增:预处理 ---
20
- // 1. 将 currentColor 替换为固定色,否则 Inkscape 命令行不识别描边
21
- // 2. 确保 stroke-width 存在
22
- svg = svg.replace(/currentColor/g, '#000000');
23
 
24
  const id = uuidv4();
25
  const inputFile = path.join('/tmp', `${id}_in.svg`);
26
  const outputFile = path.join('/tmp', `${id}_out.svg`);
27
 
28
- fs.writeFileSync(inputFile, svg);
29
 
30
- // --- 修改后的命令 ---
31
- // select-all: 选中对象
32
- // object-to-path: 把基本的 line, circle 转为 path
33
- // path-outline: 核心动作:描边转轮廓
34
- // 这个组合是专门针对 Inkscape 1.2+ 在 Linux 环境下的高成功率写法
35
- const command = `inkscape "${inputFile}" --actions="select-all;object-to-path;select-all;path-outline;export-filename:${outputFile};export-do"`;
 
 
 
 
 
36
 
37
- exec(command, (error) => {
38
- if (error) {
39
- console.error('Inkscape error:', error);
40
- return res.status(500).send('转换出错');
41
- }
 
42
 
43
  if (!fs.existsSync(outputFile)) {
44
- return res.status(500).send('文件生成失败,请检查SVG格式');
 
45
  }
46
 
47
  let result = fs.readFileSync(outputFile, 'utf8');
48
 
49
- // --- 新增:后处理 ---
50
- // 转换后的结果是黑色填充,我们可以把 fill 改回 currentColor 方便你在 iconfont 中使用
51
- result = result.replace(/fill="#000000"/g, 'fill="currentColor"');
52
- // 移除多余的描边属性(防止 Inkscape 残留)
53
  result = result.replace(/stroke=".*?"/g, 'stroke="none"');
 
54
 
55
  res.send(result);
56
 
 
57
  [inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });
58
  });
59
  });
60
 
 
 
61
  const PORT = 7860;
62
- app.listen(PORT, () => console.log(`Inkscape API 运行在端口 ${PORT}`));
 
9
  app.use(cors());
10
  app.use(express.json({ limit: '10mb' }));
11
 
 
 
 
12
  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`);
23
  const outputFile = path.join('/tmp', `${id}_out.svg`);
24
 
25
+ fs.writeFileSync(inputFile, processedSvg);
26
 
27
+ // 2. 改进后的命令链:
28
+ // - 使用 --export-plain-svg 确保输出纯净
29
+ // - 组合命令:选中所有 -> 对象转路径 -> 描边转轮廓
30
+ const actions = [
31
+ 'select-all:all',
32
+ 'object-to-path',
33
+ 'select-all:all',
34
+ 'path-outline',
35
+ `export-filename:${outputFile}`,
36
+ 'export-do'
37
+ ].join(';');
38
 
39
+ const command = `inkscape "${inputFile}" --actions="${actions}"`;
40
+
41
+ console.log('Executing:', command); // 在 Runtime Logs 中打印命令
42
+
43
+ exec(command, (error, stdout, stderr) => {
44
+ if (stderr) console.log('Inkscape Stderr:', stderr);
45
 
46
  if (!fs.existsSync(outputFile)) {
47
+ console.error('File not found at:', outputFile);
48
+ return res.status(500).send('Inkscape 转换动作未执行,请检查 Logs');
49
  }
50
 
51
  let result = fs.readFileSync(outputFile, 'utf8');
52
 
53
+ // 3. 后处理:清理 Inkscape 产生的杂质
54
+ result = result.replace(/stroke-width=".*?"/g, 'stroke-width="0"');
 
 
55
  result = result.replace(/stroke=".*?"/g, 'stroke="none"');
56
+ result = result.replace(/fill="#000000"/g, 'fill="currentColor"');
57
 
58
  res.send(result);
59
 
60
+ // 清理
61
  [inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });
62
  });
63
  });
64
 
65
+ app.get('/', (req, res) => res.send('Inkscape API is Running...'));
66
+
67
  const PORT = 7860;
68
+ app.listen(PORT, () => console.log(`Server started on port ${PORT}`));