Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -2,204 +2,101 @@ 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 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
}
|
| 64 |
-
console.error('Cleanup error:', e);
|
| 65 |
-
}
|
| 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 |
-
|
| 97 |
-
'export-do',
|
| 98 |
-
'file-close'
|
| 99 |
-
].join(';');
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 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 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
const id = crypto.randomUUID();
|
| 142 |
-
const inputFile = path.join('/tmp', `${id}_in.svg`);
|
| 143 |
-
const outputFile = path.join('/tmp', `${id}_out.svg`);
|
| 144 |
-
|
| 145 |
-
fs.writeFileSync(inputFile, processedSvg);
|
| 146 |
-
|
| 147 |
-
// 复刻你本地成功的指令链
|
| 148 |
-
const actions = [
|
| 149 |
-
`file-open:${inputFile}`,
|
| 150 |
-
'select-all',
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
// 👇 新增:强制填充
|
| 154 |
-
'object-set-property:fill,#000000',
|
| 155 |
-
'object-set-property:fill-opacity,1',
|
| 156 |
-
|
| 157 |
-
// 👇 原有逻辑
|
| 158 |
-
'object-stroke-to-path',
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
'path-combine',
|
| 162 |
-
|
| 163 |
-
'path-union',
|
| 164 |
-
|
| 165 |
-
`export-filename:${outputFile}`,
|
| 166 |
-
'export-do',
|
| 167 |
-
'file-close'
|
| 168 |
-
].join(';');
|
| 169 |
-
|
| 170 |
-
// 使用 xvfb-run 确保指令集在无头环境被激活
|
| 171 |
-
const command = `xvfb-run -a inkscape --actions="${actions}"`;
|
| 172 |
-
|
| 173 |
-
exec(command, (error, stdout, stderr) => {
|
| 174 |
-
if (error) {
|
| 175 |
-
console.error(`Inkscape Error: ${error.message}`);
|
| 176 |
-
return res.status(500).send('Inkscape 转换执行失败');
|
| 177 |
-
}
|
| 178 |
-
|
| 179 |
-
if (!fs.existsSync(outputFile)) {
|
| 180 |
-
return res.status(500).send('转换成功但未生成文件');
|
| 181 |
-
}
|
| 182 |
-
|
| 183 |
-
let result = fs.readFileSync(outputFile, 'utf8');
|
| 184 |
-
|
| 185 |
-
// 后处理:适配 Iconfont
|
| 186 |
-
result = result.replace(/fill="#000000"/g, 'fill="currentColor"')
|
| 187 |
-
.replace(/stroke=".*?"/g, 'stroke="none"')
|
| 188 |
-
.replace(/stroke-width=".*?"/g, 'stroke-width="0"');
|
| 189 |
-
|
| 190 |
-
res.send(result);
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
});
|
| 201 |
|
| 202 |
-
app.get('/', (req, res) => res.send('Inkscape
|
| 203 |
|
| 204 |
-
|
| 205 |
-
app.listen(PORT, () => console.log(`Server is online at port ${PORT}`));
|
|
|
|
| 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 |
+
* @param {string} svg 原始SVG代码
|
| 15 |
+
* @param {Array} customActions 针对不同业务逻辑的Inkscape Action数组
|
| 16 |
+
*/
|
| 17 |
+
const runInkscape = (svg, customActions) => {
|
| 18 |
+
return new Promise((resolve, reject) => {
|
| 19 |
+
const id = crypto.randomUUID();
|
| 20 |
+
const inputFile = path.join('/tmp', `${id}_in.svg`);
|
| 21 |
+
const outputFile = path.join('/tmp', `${id}_out.svg`);
|
| 22 |
+
|
| 23 |
+
// 统一预处理颜色
|
| 24 |
+
const processedSvg = svg.replace(/currentColor/g, '#000000');
|
| 25 |
+
fs.writeFileSync(inputFile, processedSvg);
|
| 26 |
+
|
| 27 |
+
// 构造指令链
|
| 28 |
+
const actions = [
|
| 29 |
+
`file-open:${inputFile}`,
|
| 30 |
+
'select-all',
|
| 31 |
+
...customActions,
|
| 32 |
+
`export-filename:${outputFile}`,
|
| 33 |
+
'export-plain-svg', // 导出纯净SVG
|
| 34 |
+
'export-do',
|
| 35 |
+
'file-close'
|
| 36 |
+
].join(';');
|
| 37 |
+
|
| 38 |
+
const command = `xvfb-run -a inkscape --actions="${actions}"`;
|
| 39 |
+
|
| 40 |
+
exec(command, (error) => {
|
| 41 |
+
if (error || !fs.existsSync(outputFile)) {
|
| 42 |
+
return reject(error || new Error('Output file not generated'));
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
const result = fs.readFileSync(outputFile, 'utf8');
|
| 46 |
+
|
| 47 |
+
// --- 精准提取 d 值 ---
|
| 48 |
+
const dValues = [];
|
| 49 |
+
const pathMatch = /<path[^>]+d="([^"]+)"/g;
|
| 50 |
+
let match;
|
| 51 |
+
while ((match = pathMatch.exec(result)) !== null) {
|
| 52 |
+
dValues.push(match[1]);
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
// 清理临时文件
|
| 56 |
+
try {
|
| 57 |
+
fs.unlinkSync(inputFile);
|
| 58 |
+
fs.unlinkSync(outputFile);
|
| 59 |
+
} catch (e) {}
|
| 60 |
+
|
| 61 |
+
// 返回合并后的 d 字符串
|
| 62 |
+
resolve(dValues.join(' '));
|
| 63 |
+
});
|
|
|
|
|
|
|
| 64 |
});
|
| 65 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
// --- 路由定义 ---
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
// 1. 仅转轮廓
|
| 70 |
+
app.post('/expand-stroke', async (req, res) => {
|
| 71 |
+
try {
|
| 72 |
+
const d = await runInkscape(req.body.svg, ['object-stroke-to-path', 'path-union']);
|
| 73 |
+
res.send(d);
|
| 74 |
+
} catch (err) { res.status(500).send(err.message); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
});
|
| 76 |
|
| 77 |
+
// 2. 仅填充合并
|
| 78 |
+
app.post('/expand-fill', async (req, res) => {
|
| 79 |
+
try {
|
| 80 |
+
const actions = ['object-set-property:fill,#000000', 'object-set-property:fill-opacity,1', 'path-union'];
|
| 81 |
+
const d = await runInkscape(req.body.svg, actions);
|
| 82 |
+
res.send(d);
|
| 83 |
+
} catch (err) { res.status(500).send(err.message); }
|
| 84 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
// 3. 描边+填充合并
|
| 87 |
+
app.post('/expand-stroke-fill', async (req, res) => {
|
| 88 |
+
try {
|
| 89 |
+
const actions = [
|
| 90 |
+
'object-set-property:fill,#000000',
|
| 91 |
+
'object-set-property:fill-opacity,1',
|
| 92 |
+
'object-stroke-to-path',
|
| 93 |
+
'path-union'
|
| 94 |
+
];
|
| 95 |
+
const d = await runInkscape(req.body.svg, actions);
|
| 96 |
+
res.send(d);
|
| 97 |
+
} catch (err) { res.status(500).send(err.message); }
|
| 98 |
});
|
| 99 |
|
| 100 |
+
app.get('/', (req, res) => res.send('Inkscape D-Value Extractor is Running...'));
|
| 101 |
|
| 102 |
+
app.listen(7860);
|
|
|