Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -9,54 +9,60 @@ const app = express();
|
|
| 9 |
app.use(cors());
|
| 10 |
app.use(express.json({ limit: '10mb' }));
|
| 11 |
|
| 12 |
-
app.get('/', (req, res) => {
|
| 13 |
-
res.send('🚀 Inkscape SVG 轮廓转换 API 已启动!请使用 POST 请求 /convert 接口。');
|
| 14 |
-
});
|
| 15 |
app.post('/convert', (req, res) => {
|
| 16 |
let { svg } = req.body;
|
| 17 |
if (!svg) return res.status(400).send('No SVG provided');
|
| 18 |
|
| 19 |
-
//
|
| 20 |
-
//
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
const id = uuidv4();
|
| 25 |
const inputFile = path.join('/tmp', `${id}_in.svg`);
|
| 26 |
const outputFile = path.join('/tmp', `${id}_out.svg`);
|
| 27 |
|
| 28 |
-
fs.writeFileSync(inputFile,
|
| 29 |
|
| 30 |
-
//
|
| 31 |
-
//
|
| 32 |
-
//
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
if (!fs.existsSync(outputFile)) {
|
| 44 |
-
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
let result = fs.readFileSync(outputFile, 'utf8');
|
| 48 |
|
| 49 |
-
//
|
| 50 |
-
//
|
| 51 |
-
result = result.replace(/fill="#000000"/g, 'fill="currentColor"');
|
| 52 |
-
// 移除多余的描边属性(防止 Inkscape 残留)
|
| 53 |
result = result.replace(/stroke=".*?"/g, 'stroke="none"');
|
|
|
|
| 54 |
|
| 55 |
res.send(result);
|
| 56 |
|
|
|
|
| 57 |
[inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });
|
| 58 |
});
|
| 59 |
});
|
| 60 |
|
|
|
|
|
|
|
| 61 |
const PORT = 7860;
|
| 62 |
-
app.listen(PORT, () => console.log(`
|
|
|
|
| 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 |
|
| 16 |
+
// 1. 预处理:强制把所有描边设为 2px 黑色,并加上特定的 ID,方便 Inkscape 抓取
|
| 17 |
+
// 同时把 currentColor 替换掉,防止识别失败
|
| 18 |
+
let processedSvg = svg.replace(/currentColor/g, '#000000');
|
| 19 |
+
processedSvg = processedSvg.replace(/<path/g, '<path id="target_path" fill="none" stroke="#000000" stroke-width="2"');
|
| 20 |
|
| 21 |
const id = uuidv4();
|
| 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 |
+
// 2. 改进后的命令链:
|
| 28 |
+
// - 使用 --export-plain-svg 确保输出纯净
|
| 29 |
+
// - 组合命令:选中所有 -> 对象转路径 -> 描边转轮廓
|
| 30 |
+
const actions = [
|
| 31 |
+
'select-all:all',
|
| 32 |
+
'object-to-path',
|
| 33 |
+
'select-all:all',
|
| 34 |
+
'path-outline',
|
| 35 |
+
`export-filename:${outputFile}`,
|
| 36 |
+
'export-do'
|
| 37 |
+
].join(';');
|
| 38 |
|
| 39 |
+
const command = `inkscape "${inputFile}" --actions="${actions}"`;
|
| 40 |
+
|
| 41 |
+
console.log('Executing:', command); // 在 Runtime Logs 中打印命令
|
| 42 |
+
|
| 43 |
+
exec(command, (error, stdout, stderr) => {
|
| 44 |
+
if (stderr) console.log('Inkscape Stderr:', stderr);
|
| 45 |
|
| 46 |
if (!fs.existsSync(outputFile)) {
|
| 47 |
+
console.error('File not found at:', outputFile);
|
| 48 |
+
return res.status(500).send('Inkscape 转换动作未执行,请检查 Logs');
|
| 49 |
}
|
| 50 |
|
| 51 |
let result = fs.readFileSync(outputFile, 'utf8');
|
| 52 |
|
| 53 |
+
// 3. 后处理:清理 Inkscape 产生的杂质
|
| 54 |
+
result = result.replace(/stroke-width=".*?"/g, 'stroke-width="0"');
|
|
|
|
|
|
|
| 55 |
result = result.replace(/stroke=".*?"/g, 'stroke="none"');
|
| 56 |
+
result = result.replace(/fill="#000000"/g, 'fill="currentColor"');
|
| 57 |
|
| 58 |
res.send(result);
|
| 59 |
|
| 60 |
+
// 清理
|
| 61 |
[inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });
|
| 62 |
});
|
| 63 |
});
|
| 64 |
|
| 65 |
+
app.get('/', (req, res) => res.send('Inkscape API is Running...'));
|
| 66 |
+
|
| 67 |
const PORT = 7860;
|
| 68 |
+
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
|