Spaces:
Sleeping
Sleeping
Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const { exec } = require('child_process');
|
| 3 |
+
const fs = require('fs');
|
| 4 |
+
const path = require('path');
|
| 5 |
+
const { v4: uuidv4 } = require('uuid');
|
| 6 |
+
const cors = require('cors');
|
| 7 |
+
|
| 8 |
+
const app = express();
|
| 9 |
+
app.use(cors());
|
| 10 |
+
app.use(express.json({ limit: '10mb' }));
|
| 11 |
+
|
| 12 |
+
app.post('/convert', (req, res) => {
|
| 13 |
+
const { svg } = req.body;
|
| 14 |
+
if (!svg) return res.status(400).send('No SVG provided');
|
| 15 |
+
|
| 16 |
+
const id = uuidv4();
|
| 17 |
+
const inputFile = path.join('/tmp', `${id}_in.svg`);
|
| 18 |
+
const outputFile = path.join('/tmp', `${id}_out.svg`);
|
| 19 |
+
|
| 20 |
+
// 1. 将收到的 SVG 写入临时文件
|
| 21 |
+
fs.writeFileSync(inputFile, svg);
|
| 22 |
+
|
| 23 |
+
// 2. 调用 Inkscape 命令行进行轮廓化 (Outline Stroke)
|
| 24 |
+
// 这里的参数 --actions 是 Inkscape 1.x+ 的标准写法
|
| 25 |
+
const command = `inkscape "${inputFile}" --actions="path-outline;export-filename:${outputFile};export-do"`;
|
| 26 |
+
|
| 27 |
+
exec(command, (error) => {
|
| 28 |
+
if (error) {
|
| 29 |
+
console.error(error);
|
| 30 |
+
return res.status(500).send('Inkscape 转换出错');
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
// 3. 读取转换后的文件并返回
|
| 34 |
+
const result = fs.readFileSync(outputFile, 'utf8');
|
| 35 |
+
|
| 36 |
+
// 清理临时文件
|
| 37 |
+
[inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });
|
| 38 |
+
|
| 39 |
+
res.send(result);
|
| 40 |
+
});
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
const PORT = 7860;
|
| 44 |
+
app.listen(PORT, () => console.log(`Inkscape API 运行在端口 ${PORT}`));
|