Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -2,7 +2,7 @@ const express = require('express');
|
|
| 2 |
const { exec } = require('child_process');
|
| 3 |
const fs = require('fs');
|
| 4 |
const path = require('path');
|
| 5 |
-
const
|
| 6 |
const cors = require('cors');
|
| 7 |
|
| 8 |
const app = express();
|
|
@@ -13,16 +13,17 @@ app.post('/convert', (req, res) => {
|
|
| 13 |
let { svg } = req.body;
|
| 14 |
if (!svg) return res.status(400).send('No SVG provided');
|
| 15 |
|
| 16 |
-
// 预处理:Inkscape
|
| 17 |
const processedSvg = svg.replace(/currentColor/g, '#000000');
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
const inputFile = path.join('/tmp', `${id}_in.svg`);
|
| 21 |
const outputFile = path.join('/tmp', `${id}_out.svg`);
|
| 22 |
|
| 23 |
fs.writeFileSync(inputFile, processedSvg);
|
| 24 |
|
| 25 |
-
//
|
| 26 |
const actions = [
|
| 27 |
`file-open:${inputFile}`,
|
| 28 |
'select-all',
|
|
@@ -33,7 +34,7 @@ app.post('/convert', (req, res) => {
|
|
| 33 |
'file-close'
|
| 34 |
].join(';');
|
| 35 |
|
| 36 |
-
//
|
| 37 |
const command = `xvfb-run -a inkscape --actions="${actions}"`;
|
| 38 |
|
| 39 |
exec(command, (error, stdout, stderr) => {
|
|
@@ -48,21 +49,24 @@ app.post('/convert', (req, res) => {
|
|
| 48 |
|
| 49 |
let result = fs.readFileSync(outputFile, 'utf8');
|
| 50 |
|
| 51 |
-
// 后处理:
|
| 52 |
-
// 并移除可能残留的 stroke 属性
|
| 53 |
result = result.replace(/fill="#000000"/g, 'fill="currentColor"')
|
| 54 |
.replace(/stroke=".*?"/g, 'stroke="none"')
|
| 55 |
.replace(/stroke-width=".*?"/g, 'stroke-width="0"');
|
| 56 |
|
| 57 |
res.send(result);
|
| 58 |
|
| 59 |
-
//
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
});
|
| 64 |
});
|
| 65 |
|
| 66 |
-
app.get('/', (req, res) => res.send('Inkscape API
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
| 2 |
const { exec } = require('child_process');
|
| 3 |
const fs = require('fs');
|
| 4 |
const path = require('path');
|
| 5 |
+
const crypto = require('crypto'); // 使用原生模块替代 uuid
|
| 6 |
const cors = require('cors');
|
| 7 |
|
| 8 |
const app = express();
|
|
|
|
| 13 |
let { svg } = req.body;
|
| 14 |
if (!svg) return res.status(400).send('No SVG provided');
|
| 15 |
|
| 16 |
+
// 预处理:Inkscape 对 currentColor 识别较差,强制转为黑色
|
| 17 |
const processedSvg = svg.replace(/currentColor/g, '#000000');
|
| 18 |
|
| 19 |
+
// 使用原生 crypto 生成随机 ID
|
| 20 |
+
const id = crypto.randomUUID();
|
| 21 |
const inputFile = path.join('/tmp', `${id}_in.svg`);
|
| 22 |
const outputFile = path.join('/tmp', `${id}_out.svg`);
|
| 23 |
|
| 24 |
fs.writeFileSync(inputFile, processedSvg);
|
| 25 |
|
| 26 |
+
// 复刻你本地成功的指令链
|
| 27 |
const actions = [
|
| 28 |
`file-open:${inputFile}`,
|
| 29 |
'select-all',
|
|
|
|
| 34 |
'file-close'
|
| 35 |
].join(';');
|
| 36 |
|
| 37 |
+
// 使用 xvfb-run 确保指令集在无头环境被激活
|
| 38 |
const command = `xvfb-run -a inkscape --actions="${actions}"`;
|
| 39 |
|
| 40 |
exec(command, (error, stdout, stderr) => {
|
|
|
|
| 49 |
|
| 50 |
let result = fs.readFileSync(outputFile, 'utf8');
|
| 51 |
|
| 52 |
+
// 后处理:适配 Iconfont
|
|
|
|
| 53 |
result = result.replace(/fill="#000000"/g, 'fill="currentColor"')
|
| 54 |
.replace(/stroke=".*?"/g, 'stroke="none"')
|
| 55 |
.replace(/stroke-width=".*?"/g, 'stroke-width="0"');
|
| 56 |
|
| 57 |
res.send(result);
|
| 58 |
|
| 59 |
+
// 清理临时文件
|
| 60 |
+
try {
|
| 61 |
+
if(fs.existsSync(inputFile)) fs.unlinkSync(inputFile);
|
| 62 |
+
if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);
|
| 63 |
+
} catch (e) {
|
| 64 |
+
console.error('Cleanup error:', e);
|
| 65 |
+
}
|
| 66 |
});
|
| 67 |
});
|
| 68 |
|
| 69 |
+
app.get('/', (req, res) => res.send('Inkscape API is Running with Native Crypto...'));
|
| 70 |
|
| 71 |
+
const PORT = 7860;
|
| 72 |
+
app.listen(PORT, () => console.log(`Server is online at port ${PORT}`));
|