mokee commited on
Commit
1ea5cda
·
verified ·
1 Parent(s): cb3ded2

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +173 -54
server.js CHANGED
@@ -1,41 +1,153 @@
 
1
  const express = require('express');
2
  const { exec } = require('child_process');
3
  const fs = require('fs');
4
  const path = require('path');
5
- const crypto = require('crypto');
6
  const cors = require('cors');
7
 
8
  const app = express();
9
  app.use(cors());
10
  app.use(express.json({ limit: '10mb' }));
11
 
12
- /**
13
- * 核心引擎:将所有图形转为路径并执行布尔运算
14
- */
15
- const runInkscape = (svg, mode) => {
16
- return new Promise((resolve, reject) => {
17
- const id = crypto.randomUUID();
18
- const inputFile = path.join('/tmp', `${id}_in.svg`);
19
- const outputFile = path.join('/tmp', `${id}_out.svg`);
20
-
21
- // 预处理:转黑并确保有基础属性
22
- const processedSvg = svg.replace(/currentColor/g, '#000000');
23
- fs.writeFileSync(inputFile, processedSvg);
24
-
25
- // 构建指令
26
- let coreActions = [];
27
-
28
- if (mode === 'stroke') {
29
- coreActions.push('object-stroke-to-path', 'path-union');
30
- } else if (mode === 'fill') {
31
- coreActions.push('object-set-property:fill,#000000', 'path-union');
32
- } else if (mode === 'stroke-fill') {
33
- // 先复制一份保留填充,再把另一份转轮廓,最后合并
34
- coreActions.push('object-set-property:fill,#000000','object-stroke-to-path','path-combine', 'path-union');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  }
 
 
 
 
 
 
 
 
 
36
 
37
- const actions = [
38
- `file-open:${inputFile}`,
 
 
 
 
 
 
 
 
39
  'select-all',
40
 
41
 
@@ -54,34 +166,41 @@ const runInkscape = (svg, mode) => {
54
  `export-filename:${outputFile}`,
55
  'export-do',
56
  'file-close'
57
- ].join(';');
58
-
59
- const command = `xvfb-run -a inkscape --actions="${actions}"`;
60
-
61
- exec(command, (error) => {
62
- if (error || !fs.existsSync(outputFile)) return reject(error || new Error('Fail'));
63
- const result = fs.readFileSync(outputFile, 'utf8');
64
-
65
- // 提取所有路径的 d
66
- const dValues = [];
67
- const pathMatch = /<path[^>]+d="([^"]+)"/g;
68
- let match;
69
- while ((match = pathMatch.exec(result)) !== null) {
70
- dValues.push(match[1]);
71
- }
72
-
73
- // 清理
74
- [inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });
75
- resolve(dValues.join(' '));
76
- });
77
- });
78
- };
 
79
 
80
- app.post('/convert/:mode', async (req, res) => {
81
- try {
82
- const d = await runInkscape(req.body.svg, req.params.mode);
83
- res.send(d);
84
- } catch (err) { res.status(500).send(err.message); }
 
 
 
85
  });
86
 
87
- app.listen(7860);
 
 
 
 
1
+
2
  const express = require('express');
3
  const { exec } = require('child_process');
4
  const fs = require('fs');
5
  const path = require('path');
6
+ const crypto = require('crypto'); // 使用原生模块替代 uuid
7
  const cors = require('cors');
8
 
9
  const app = express();
10
  app.use(cors());
11
  app.use(express.json({ limit: '10mb' }));
12
 
13
+ app.post('/expand-stroke', (req, res) => {
14
+ let { svg } = req.body;
15
+ if (!svg) return res.status(400).send('No SVG provided');
16
+
17
+ // 预处理:Inkscape 对 currentColor 识别较差,强制转为黑色
18
+ const processedSvg = svg.replace(/currentColor/g, '#000000');
19
+
20
+ // 使用原生 crypto 生成随机 ID
21
+ const id = crypto.randomUUID();
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
+ // 复刻你本地成功的指令链
28
+ const actions = [
29
+ `file-open:${inputFile}`,
30
+ 'select-all',
31
+ 'object-stroke-to-path',
32
+ 'path-union',
33
+ `export-filename:${outputFile}`,
34
+ 'export-do',
35
+ 'file-close'
36
+ ].join(';');
37
+
38
+ // 使用 xvfb-run 确保指令集在无头环境被激活
39
+ const command = `xvfb-run -a inkscape --actions="${actions}"`;
40
+
41
+ exec(command, (error, stdout, stderr) => {
42
+ if (error) {
43
+ console.error(`Inkscape Error: ${error.message}`);
44
+ return res.status(500).send('Inkscape 转换执行失败');
45
+ }
46
+
47
+ if (!fs.existsSync(outputFile)) {
48
+ return res.status(500).send('转换成功但未生成文件');
49
+ }
50
+
51
+ let result = fs.readFileSync(outputFile, 'utf8');
52
+
53
+ // 后处理:适配 Iconfont
54
+ result = result.replace(/fill="#000000"/g, 'fill="currentColor"')
55
+ .replace(/stroke=".*?"/g, 'stroke="none"')
56
+ .replace(/stroke-width=".*?"/g, 'stroke-width="0"');
57
+
58
+ res.send(result);
59
+
60
+ // 清理临时文件
61
+ try {
62
+ if(fs.existsSync(inputFile)) fs.unlinkSync(inputFile);
63
+ if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);
64
+ } catch (e) {
65
+ console.error('Cleanup error:', e);
66
+ }
67
+ });
68
+ });
69
+
70
+
71
+ app.post('/expand-fill', (req, res) => {
72
+ let { svg } = req.body;
73
+ if (!svg) return res.status(400).send('No SVG provided');
74
+
75
+ // 预处理:Inkscape 对 currentColor 识别较差,强制转为黑色
76
+ const processedSvg = svg.replace(/currentColor/g, '#000000');
77
+
78
+ // 使用原生 crypto 生成随机 ID
79
+ const id = crypto.randomUUID();
80
+ const inputFile = path.join('/tmp', `${id}_in.svg`);
81
+ const outputFile = path.join('/tmp', `${id}_out.svg`);
82
+
83
+ fs.writeFileSync(inputFile, processedSvg);
84
+
85
+ // 复刻你本地成功的指令链
86
+ const actions = [
87
+ `file-open:${inputFile}`,
88
+ 'select-all',
89
+
90
+
91
+ // ?? 新增:强制填充
92
+ 'object-set-property:fill,#000000',
93
+ 'object-set-property:fill-opacity,1',
94
+
95
+ 'path-union',
96
+
97
+ `export-filename:${outputFile}`,
98
+ 'export-do',
99
+ 'file-close'
100
+ ].join(';');
101
+
102
+ // 使用 xvfb-run 确保指令集在无头环境被激活
103
+ const command = `xvfb-run -a inkscape --actions="${actions}"`;
104
+
105
+ exec(command, (error, stdout, stderr) => {
106
+ if (error) {
107
+ console.error(`Inkscape Error: ${error.message}`);
108
+ return res.status(500).send('Inkscape 转换执行失败');
109
+ }
110
+
111
+ if (!fs.existsSync(outputFile)) {
112
+ return res.status(500).send('转换成功但未生成文件');
113
+ }
114
+
115
+ let result = fs.readFileSync(outputFile, 'utf8');
116
+
117
+ // 后处理:适配 Iconfont
118
+ result = result.replace(/fill="#000000"/g, 'fill="currentColor"')
119
+ .replace(/stroke=".*?"/g, 'stroke="none"')
120
+ .replace(/stroke-width=".*?"/g, 'stroke-width="0"');
121
+
122
+ res.send(result);
123
+
124
+ // 清理临时文件
125
+ try {
126
+ if(fs.existsSync(inputFile)) fs.unlinkSync(inputFile);
127
+ if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);
128
+ } catch (e) {
129
+ console.error('Cleanup error:', e);
130
  }
131
+ });
132
+ });
133
+
134
+ app.post('/expand-stroke-fill', (req, res) => {
135
+ let { svg } = req.body;
136
+ if (!svg) return res.status(400).send('No SVG provided');
137
+
138
+ // 预处理:Inkscape 对 currentColor 识别较差,强制转为黑色
139
+ const processedSvg = svg.replace(/currentColor/g, '#000000');
140
 
141
+ // 使用原生 crypto 生成随机 ID
142
+ const id = crypto.randomUUID();
143
+ const inputFile = path.join('/tmp', `${id}_in.svg`);
144
+ const outputFile = path.join('/tmp', `${id}_out.svg`);
145
+
146
+ fs.writeFileSync(inputFile, processedSvg);
147
+
148
+ // 复刻你本地成功的指令链
149
+ const actions = [
150
+ `file-open:${inputFile}`,
151
  'select-all',
152
 
153
 
 
166
  `export-filename:${outputFile}`,
167
  'export-do',
168
  'file-close'
169
+ ].join(';');
170
+
171
+ // 使用 xvfb-run 确保指令集在无头环境被激活
172
+ const command = `xvfb-run -a inkscape --actions="${actions}"`;
173
+
174
+ exec(command, (error, stdout, stderr) => {
175
+ if (error) {
176
+ console.error(`Inkscape Error: ${error.message}`);
177
+ return res.status(500).send('Inkscape 转换执行失败');
178
+ }
179
+
180
+ if (!fs.existsSync(outputFile)) {
181
+ return res.status(500).send('转换成功但未生成文件');
182
+ }
183
+
184
+ let result = fs.readFileSync(outputFile, 'utf8');
185
+
186
+ // 后处理:适配 Iconfont
187
+ result = result.replace(/fill="#000000"/g, 'fill="currentColor"')
188
+ .replace(/stroke=".*?"/g, 'stroke="none"')
189
+ .replace(/stroke-width=".*?"/g, 'stroke-width="0"');
190
+
191
+ res.send(result);
192
 
193
+ // 清理临时文件
194
+ try {
195
+ if(fs.existsSync(inputFile)) fs.unlinkSync(inputFile);
196
+ if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);
197
+ } catch (e) {
198
+ console.error('Cleanup error:', e);
199
+ }
200
+ });
201
  });
202
 
203
+ app.get('/', (req, res) => res.send('Inkscape API is Running with Native Crypto...'));
204
+
205
+ const PORT = 7860;
206
+ app.listen(PORT, () => console.log(`Server is online at port ${PORT}`));