mokee commited on
Commit
6943817
·
verified ·
1 Parent(s): 4ca5472

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +17 -13
server.js CHANGED
@@ -2,7 +2,7 @@ const express = require('express');
2
  const { exec } = require('child_process');
3
  const fs = require('fs');
4
  const path = require('path');
5
- const { v4: uuidv4 } = require('uuid');
6
  const cors = require('cors');
7
 
8
  const app = express();
@@ -13,16 +13,17 @@ app.post('/convert', (req, res) => {
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`);
21
  const outputFile = path.join('/tmp', `${id}_out.svg`);
22
 
23
  fs.writeFileSync(inputFile, processedSvg);
24
 
25
- // 完全参考你本地成功的指令链
26
  const actions = [
27
  `file-open:${inputFile}`,
28
  'select-all',
@@ -33,7 +34,7 @@ app.post('/convert', (req, res) => {
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) => {
@@ -48,21 +49,24 @@ app.post('/convert', (req, res) => {
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.'));
 
 
2
  const { exec } = require('child_process');
3
  const fs = require('fs');
4
  const path = require('path');
5
+ const crypto = require('crypto'); // 使用原生模块替代 uuid
6
  const cors = require('cors');
7
 
8
  const app = express();
 
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
+ // 使用原生 crypto 生成随机 ID
20
+ const id = crypto.randomUUID();
21
  const inputFile = path.join('/tmp', `${id}_in.svg`);
22
  const outputFile = path.join('/tmp', `${id}_out.svg`);
23
 
24
  fs.writeFileSync(inputFile, processedSvg);
25
 
26
+ // 复刻你本地成功的指令链
27
  const actions = [
28
  `file-open:${inputFile}`,
29
  'select-all',
 
34
  'file-close'
35
  ].join(';');
36
 
37
+ // 使用 xvfb-run 确保指令集在无头环境被激活
38
  const command = `xvfb-run -a inkscape --actions="${actions}"`;
39
 
40
  exec(command, (error, stdout, stderr) => {
 
49
 
50
  let result = fs.readFileSync(outputFile, 'utf8');
51
 
52
+ // 后处理:适配 Iconfont
 
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
+ try {
61
+ if(fs.existsSync(inputFile)) fs.unlinkSync(inputFile);
62
+ if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);
63
+ } catch (e) {
64
+ console.error('Cleanup error:', e);
65
+ }
66
  });
67
  });
68
 
69
+ app.get('/', (req, res) => res.send('Inkscape API is Running with Native Crypto...'));
70
 
71
+ const PORT = 7860;
72
+ app.listen(PORT, () => console.log(`Server is online at port ${PORT}`));