mokee commited on
Commit
0b38401
·
verified ·
1 Parent(s): 0e5793b

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +66 -2
server.js CHANGED
@@ -9,7 +9,7 @@ const app = express();
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
 
@@ -66,7 +66,71 @@ app.post('/convert', (req, res) => {
66
  });
67
  });
68
 
69
- app.post('/fill', (req, res) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  let { svg } = req.body;
71
  if (!svg) return res.status(400).send('No SVG provided');
72
 
 
9
  app.use(cors());
10
  app.use(express.json({ limit: '10mb' }));
11
 
12
+ app.post('/expand-stroke', (req, res) => {
13
  let { svg } = req.body;
14
  if (!svg) return res.status(400).send('No SVG provided');
15
 
 
66
  });
67
  });
68
 
69
+
70
+ app.post('/expand-fill', (req, res) => {
71
+ let { svg } = req.body;
72
+ if (!svg) return res.status(400).send('No SVG provided');
73
+
74
+ // 预处理:Inkscape 对 currentColor 识别较差,强制转为黑色
75
+ const processedSvg = svg.replace(/currentColor/g, '#000000');
76
+
77
+ // 使用原生 crypto 生成随机 ID
78
+ const id = crypto.randomUUID();
79
+ const inputFile = path.join('/tmp', `${id}_in.svg`);
80
+ const outputFile = path.join('/tmp', `${id}_out.svg`);
81
+
82
+ fs.writeFileSync(inputFile, processedSvg);
83
+
84
+ // 复刻你本地成功的指令链
85
+ const actions = [
86
+ `file-open:${inputFile}`,
87
+ 'select-all',
88
+
89
+
90
+ // 👇 新增:强制填充
91
+ 'object-set-property:fill,#000000',
92
+ 'object-set-property:fill-opacity,1',
93
+
94
+ 'path-union',
95
+
96
+ `export-filename:${outputFile}`,
97
+ 'export-do',
98
+ 'file-close'
99
+ ].join(';');
100
+
101
+ // 使用 xvfb-run 确保指令集在无头环境被激活
102
+ const command = `xvfb-run -a inkscape --actions="${actions}"`;
103
+
104
+ exec(command, (error, stdout, stderr) => {
105
+ if (error) {
106
+ console.error(`Inkscape Error: ${error.message}`);
107
+ return res.status(500).send('Inkscape 转换执行失败');
108
+ }
109
+
110
+ if (!fs.existsSync(outputFile)) {
111
+ return res.status(500).send('转换成功但未生成文件');
112
+ }
113
+
114
+ let result = fs.readFileSync(outputFile, 'utf8');
115
+
116
+ // 后处理:适配 Iconfont
117
+ result = result.replace(/fill="#000000"/g, 'fill="currentColor"')
118
+ .replace(/stroke=".*?"/g, 'stroke="none"')
119
+ .replace(/stroke-width=".*?"/g, 'stroke-width="0"');
120
+
121
+ res.send(result);
122
+
123
+ // 清理临时文件
124
+ try {
125
+ if(fs.existsSync(inputFile)) fs.unlinkSync(inputFile);
126
+ if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);
127
+ } catch (e) {
128
+ console.error('Cleanup error:', e);
129
+ }
130
+ });
131
+ });
132
+
133
+ app.post('/expand-stroke-fill', (req, res) => {
134
  let { svg } = req.body;
135
  if (!svg) return res.status(400).send('No SVG provided');
136