Spaces:
Sleeping
Sleeping
Update server.js
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201const express = require('express');const { exec } = require('child_process');const fs = require('fs');const path = require('path');const crypto = require('crypto'); // 使用原生模块替代 uuidconst cors = require('cors');const app = express();app.use(cors());app.use(express.json({ limit: '10mb' }));app.post('/expand-stroke', (req, res) => {let { svg } = req.body;if (!svg) return res.status(400).send('No SVG provided');// 预处理:Inkscape 对 currentColor 识别较差,强制转为黑色const processedSvg = svg.replace(/currentColor/g, '#000000');// 使用原生 crypto 生成随机 IDconst id = crypto.randomUUID();const inputFile = path.join('/tmp', `${id}_in.svg`);const outputFile = path.join('/tmp', `${id}_out.svg`);fs.writeFileSync(inputFile, processedSvg);// 复刻你本地成功的指令链const actions = [`file-open:${inputFile}`,'select-all','object-stroke-to-path','path-union',`export-filename:${outputFile}`,'export-do','file-close'].join(';');// 使用 xvfb-run 确保指令集在无头环境被激活const command = `xvfb-run -a inkscape --actions="${actions}"`;exec(command, (error, stdout, stderr) => {if (error) {console.error(`Inkscape Error: ${error.message}`);return res.status(500).send('Inkscape 转换执行失败');}if (!fs.existsSync(outputFile)) {return res.status(500).send('转换成功但未生成文件');}let result = fs.readFileSync(outputFile, 'utf8');// 后处理:适配 Iconfontresult = result.replace(/fill="#000000"/g, 'fill="currentColor"').replace(/stroke=".*?"/g, 'stroke="none"').replace(/stroke-width=".*?"/g, 'stroke-width="0"');res.send(result);// 清理临时文件try {if(fs.existsSync(inputFile)) fs.unlinkSync(inputFile);if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);} catch (e) {console.error('Cleanup error:', e);}});});app.post('/expand-fill', (req, res) => {let { svg } = req.body;if (!svg) return res.status(400).send('No SVG provided');// 预处理:Inkscape 对 currentColor 识别较差,强制转为黑色const processedSvg = svg.replace(/currentColor/g, '#000000');// 使用原生 crypto 生成随机 IDconst id = crypto.randomUUID();const inputFile = path.join('/tmp', `${id}_in.svg`);const outputFile = path.join('/tmp', `${id}_out.svg`);fs.writeFileSync(inputFile, processedSvg);// 复刻你本地成功的指令链const actions = [`file-open:${inputFile}`,'select-all',// ?? 新增:强制填充'object-set-property:fill,#000000','object-set-property:fill-opacity,1','path-union',`export-filename:${outputFile}`,'export-do','file-close'].join(';');// 使用 xvfb-run 确保指令集在无头环境被激活const command = `xvfb-run -a inkscape --actions="${actions}"`;exec(command, (error, stdout, stderr) => {if (error) {console.error(`Inkscape Error: ${error.message}`);return res.status(500).send('Inkscape 转换执行失败');}if (!fs.existsSync(outputFile)) {return res.status(500).send('转换成功但未生成文件');}let result = fs.readFileSync(outputFile, 'utf8');// 后处理:适配 Iconfontresult = result.replace(/fill="#000000"/g, 'fill="currentColor"').replace(/stroke=".*?"/g, 'stroke="none"').replace(/stroke-width=".*?"/g, 'stroke-width="0"');res.send(result);// 清理临时文件try {if(fs.existsSync(inputFile)) fs.unlinkSync(inputFile);if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);} catch (e) {console.error('Cleanup error:', e);}});});app.post('/expand-stroke-fill', (req, res) => {let { svg } = req.body;if (!svg) return res.status(400).send('No SVG provided');// 预处理:Inkscape 对 currentColor 识别较差,强制转为黑色const processedSvg = svg.replace(/currentColor/g, '#000000');// 使用原生 crypto 生成随机 IDconst id = crypto.randomUUID();const inputFile = path.join('/tmp', `${id}_in.svg`);const outputFile = path.join('/tmp', `${id}_out.svg`);fs.writeFileSync(inputFile, processedSvg);// 复刻你本地成功的指令链const actions = [`file-open:${inputFile}`,'selection-duplicate', // 复制'object-stroke-to-path', // 转轮廓'select-all','path-union',`export-filename:${outputFile}`,'export-do','file-close'].join(';');// 使用 xvfb-run 确保指令集在无头环境被激活const command = `xvfb-run -a inkscape --actions="${actions}"`;exec(command, (error, stdout, stderr) => {if (error) {console.error(`Inkscape Error: ${error.message}`);return res.status(500).send('Inkscape 转换执行失败');}if (!fs.existsSync(outputFile)) {return res.status(500).send('转换成功但未生成文件');}let result = fs.readFileSync(outputFile, 'utf8');// 后处理:适配 Iconfontresult = result.replace(/fill="#000000"/g, 'fill="currentColor"').replace(/stroke=".*?"/g, 'stroke="none"').replace(/stroke-width=".*?"/g, 'stroke-width="0"');res.send(result);// 清理临时文件try {if(fs.existsSync(inputFile)) fs.unlinkSync(inputFile);if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);} catch (e) {console.error('Cleanup error:', e);}});});app.get('/', (req, res) => res.send('Inkscape API is Running with Native Crypto...'));const PORT = 7860;app.listen(PORT, () => console.log(`Server is online at port ${PORT}`));1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980const express = require('express');const { exec } = require('child_process');const fs = require('fs');const path = require('path');const crypto = require('crypto');const cors = require('cors');const app = express();app.use(cors());app.use(express.json({ limit: '10mb' }));/*** 核心引擎:将所有图形转为路径并执行布尔运算*/const runInkscape = (svg, mode) => {return new Promise((resolve, reject) => {const id = crypto.randomUUID();const inputFile = path.join('/tmp', `${id}_in.svg`);const outputFile = path.join('/tmp', `${id}_out.svg`);// 预处理:转黑并确保有基础属性const processedSvg = svg.replace(/currentColor/g, '#000000');fs.writeFileSync(inputFile, processedSvg);// 构建指令let coreActions = ['object-to-path'];if (mode === 'stroke') {coreActions.push('object-stroke-to-path', 'path-union');} else if (mode === 'fill') {coreActions.push('object-set-property:fill,#000000', 'path-union');} else if (mode === 'stroke-fill') {coreActions.push('selection-duplicate', // 复制'object-stroke-to-path', // 转轮廓'select-all', // 选中原填充 + 新轮廓'path-union', // 第一次合并'path-simplify' // 第二次平滑,闭合微小缝隙);}const actions = [`file-open:${inputFile}`,'select-all',...coreActions,`export-filename:${outputFile}`,'export-plain-svg','export-do','file-close'].join(';');const command = `xvfb-run -a inkscape --actions="${actions}"`;exec(command, (error) => {if (error || !fs.existsSync(outputFile)) return reject(error || new Error('Fail'));const result = fs.readFileSync(outputFile, 'utf8');// 提取所有路径的 dconst dValues = [];const pathMatch = /<path[^>]+d="([^"]+)"/g;let match;while ((match = pathMatch.exec(result)) !== null) {dValues.push(match[1]);}// 清理[inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });resolve(dValues.join(' '));});});};app.post('/convert/:mode', async (req, res) => {try {const d = await runInkscape(req.body.svg, req.params.mode);res.send(d);} catch (err) { res.status(500).send(err.message); }});app.listen(7860);
server.js+64-185