mokee commited on
Commit
2551fa7
·
verified ·
1 Parent(s): b24d2f6

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +53 -18
server.js CHANGED
@@ -24,6 +24,14 @@ app.use((req, res, next) => {
24
  next();
25
  });
26
 
 
 
 
 
 
 
 
 
27
  /**
28
  * 核心引擎:将所有图形转为路径并执行布尔运算
29
  */
@@ -33,11 +41,20 @@ const runInkscape = (svg, mode) => {
33
  const inputFile = path.join('/tmp', `${id}_in.svg`);
34
  const outputFile = path.join('/tmp', `${id}_out.svg`);
35
 
36
- // 预处理:转黑并确保有基础属性
37
- const processedSvg = svg.replace(/currentColor/g, '#000000');
38
- fs.writeFileSync(inputFile, processedSvg);
 
 
 
 
 
 
 
 
 
 
39
 
40
- // 构建指令
41
  let coreActions = ['object-to-path'];
42
 
43
  if (mode === 'stroke') {
@@ -45,7 +62,12 @@ const runInkscape = (svg, mode) => {
45
  } else if (mode === 'fill') {
46
  coreActions.push('object-set-property:fill,#000000', 'path-union');
47
  } else if (mode === 'stroke-fill') {
48
- coreActions.push('object-set-property:fill,#000000','object-stroke-to-path','select-all','path-union');
 
 
 
 
 
49
  }
50
 
51
  const actions = [
@@ -60,25 +82,38 @@ const runInkscape = (svg, mode) => {
60
 
61
  const command = `xvfb-run -a inkscape --actions="${actions}"`;
62
 
63
- exec(command, (error) => {
64
- if (error || !fs.existsSync(outputFile)) return reject(error || new Error('Fail'));
65
- const result = fs.readFileSync(outputFile, 'utf8');
66
-
67
- // 提取所有路径的 d
68
- const dValues = [];
69
- const pathMatch = /<path[^>]+d="([^"]+)"/g;
70
- let match;
71
- while ((match = pathMatch.exec(result)) !== null) {
72
- dValues.push(match[1]);
73
  }
74
 
75
- // 清理
76
- [inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });
77
- resolve(dValues.join(' '));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  });
79
  });
80
  };
81
 
 
82
  app.post('/convert/:mode', async (req, res) => {
83
  try {
84
  const d = await runInkscape(req.body.svg, req.params.mode);
 
24
  next();
25
  });
26
 
27
+ const safeUnlink = (file) => {
28
+ try {
29
+ if (fs.existsSync(file)) fs.unlinkSync(file);
30
+ } catch (e) {
31
+ console.error('Cleanup failed:', file, e.message);
32
+ }
33
+ };
34
+
35
  /**
36
  * 核心引擎:将所有图形转为路径并执行布尔运算
37
  */
 
41
  const inputFile = path.join('/tmp', `${id}_in.svg`);
42
  const outputFile = path.join('/tmp', `${id}_out.svg`);
43
 
44
+ const cleanup = () => {
45
+ safeUnlink(inputFile);
46
+ safeUnlink(outputFile);
47
+ };
48
+
49
+ try {
50
+ // 预处理
51
+ const processedSvg = svg.replace(/currentColor/g, '#000000');
52
+ fs.writeFileSync(inputFile, processedSvg);
53
+ } catch (err) {
54
+ cleanup();
55
+ return reject(err);
56
+ }
57
 
 
58
  let coreActions = ['object-to-path'];
59
 
60
  if (mode === 'stroke') {
 
62
  } else if (mode === 'fill') {
63
  coreActions.push('object-set-property:fill,#000000', 'path-union');
64
  } else if (mode === 'stroke-fill') {
65
+ coreActions.push(
66
+ 'object-set-property:fill,#000000',
67
+ 'object-stroke-to-path',
68
+ 'select-all',
69
+ 'path-union'
70
+ );
71
  }
72
 
73
  const actions = [
 
82
 
83
  const command = `xvfb-run -a inkscape --actions="${actions}"`;
84
 
85
+ exec(command, (error, stdout, stderr) => {
86
+ if (error) {
87
+ cleanup();
88
+ return reject(new Error(stderr || error.message));
 
 
 
 
 
 
89
  }
90
 
91
+ if (!fs.existsSync(outputFile)) {
92
+ cleanup();
93
+ return reject(new Error('Output SVG not generated'));
94
+ }
95
+
96
+ try {
97
+ const result = fs.readFileSync(outputFile, 'utf8');
98
+
99
+ const dValues = [];
100
+ const pathMatch = /<path[^>]+d="([^"]+)"/g;
101
+ let match;
102
+ while ((match = pathMatch.exec(result)) !== null) {
103
+ dValues.push(match[1]);
104
+ }
105
+
106
+ cleanup();
107
+ resolve(dValues.join(' '));
108
+ } catch (err) {
109
+ cleanup();
110
+ reject(err);
111
+ }
112
  });
113
  });
114
  };
115
 
116
+
117
  app.post('/convert/:mode', async (req, res) => {
118
  try {
119
  const d = await runInkscape(req.body.svg, req.params.mode);