|
|
const express = require('express');
|
|
|
const fs = require('fs');
|
|
|
const path = require('path');
|
|
|
const cors = require('cors');
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
|
|
const app = express();
|
|
|
const port = 3000; // 可以根据需要修改端口号
|
|
|
app.use(cors());
|
|
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
|
// window系统下创建快捷方式接口
|
|
|
// app.post('/createShortcut', (req, res) => {
|
|
|
// // folderPath:存放的路径 shortcutName: 快捷方式名称 shortcutURL: 快捷方式的访问地址
|
|
|
// const { folderPath, shortcutName, shortcutURL } = req.body;
|
|
|
|
|
|
// // 检查是否缺少任何一个值
|
|
|
// if (!folderPath || !shortcutName || !shortcutURL) {
|
|
|
// return res.status(400).send('少了参数!');
|
|
|
// }
|
|
|
|
|
|
// // 创建文件夹(如果不存在)
|
|
|
// if (!fs.existsSync(folderPath)) {
|
|
|
// fs.mkdirSync(folderPath, { recursive: true });
|
|
|
// }
|
|
|
|
|
|
// const shortcutFilePath = path.join(folderPath, `${shortcutName}.url`);
|
|
|
|
|
|
// // 删除旧的快捷方式文件(如果存在)
|
|
|
// if (fs.existsSync(shortcutFilePath)) {
|
|
|
// fs.unlinkSync(shortcutFilePath);
|
|
|
// }
|
|
|
|
|
|
// // 创建快捷方式
|
|
|
// const shortcutFolderPath = path.join(folderPath, shortcutName + '.url');
|
|
|
// const shortcutFileContent = `[InternetShortcut]\nURL=${shortcutURL}`;
|
|
|
|
|
|
// fs.writeFile(shortcutFolderPath, shortcutFileContent, (err) => {
|
|
|
// if (err) {
|
|
|
// console.error(err);
|
|
|
// res.status(500).send('安装失败!');
|
|
|
// } else {
|
|
|
// res.status(200).send('安装成功!');
|
|
|
// }
|
|
|
// });
|
|
|
// });
|
|
|
|
|
|
// linux系统下创建快捷方式接口
|
|
|
app.post('/createShortcut', (req, res) => {
|
|
|
const {
|
|
|
folderPath,
|
|
|
shortcutName,
|
|
|
shortcutURL
|
|
|
} = req.body;
|
|
|
|
|
|
if (!folderPath || !shortcutName || !shortcutURL) {
|
|
|
return res.status(400).send('少了参数!');
|
|
|
}
|
|
|
|
|
|
if (!fs.existsSync(folderPath)) {
|
|
|
fs.mkdirSync(folderPath, {
|
|
|
recursive: true
|
|
|
});
|
|
|
}
|
|
|
|
|
|
const shortcutLinkPath = path.join(folderPath, shortcutName);
|
|
|
|
|
|
if (fs.existsSync(shortcutLinkPath)) {
|
|
|
fs.unlinkSync(shortcutLinkPath);
|
|
|
}
|
|
|
|
|
|
// 快捷方式的配置
|
|
|
const config = {
|
|
|
name: shortcutName,
|
|
|
exec: 'x-www-browser',
|
|
|
// icon: '/path/to/favicon.png', // 图标路径,可选
|
|
|
icon: '',
|
|
|
url: shortcutURL,
|
|
|
terminal: false,
|
|
|
type: 'Application',
|
|
|
categories: 'Network;WebBrowser;'
|
|
|
};
|
|
|
|
|
|
// 快捷方式文件内容
|
|
|
let desktopFileContent = `[Desktop Entry]
|
|
|
Name=${config.name}
|
|
|
Exec=${config.exec} ${config.url}
|
|
|
Type=${config.type}
|
|
|
Icon=${config.icon || ''}
|
|
|
Terminal=${config.terminal}
|
|
|
Categories=${config.categories}
|
|
|
Comment=Open ${config.name} in your web browser`;
|
|
|
|
|
|
// 如果有图标路径,确保图标文件存在
|
|
|
// if (config.icon && !fs.existsSync(config.icon)) {
|
|
|
// console.error('Icon file does not exist.');
|
|
|
// process.exit(1);
|
|
|
// }
|
|
|
|
|
|
// 创建快捷方式文件
|
|
|
fs.writeFile(shortcutLinkPath, desktopFileContent, 'utf8', function (err) {
|
|
|
if (err) {
|
|
|
console.error(err);
|
|
|
res.status(500).send('安装失败!');
|
|
|
} else {
|
|
|
res.status(200).send('安装成功!');
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// fs.symlink(shortcutURL, shortcutLinkPath, 'file', (err) => {
|
|
|
// if (err) {
|
|
|
// console.error(err);
|
|
|
// res.status(500).send('安装失败!');
|
|
|
// } else {
|
|
|
// res.status(200).send('安装成功!');
|
|
|
// }
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
// 运行项目 需要进入到当前的文件夹目录打开终端 执行 node nodeService.js
|
|
|
app.listen(port, () => {
|
|
|
console.log(`Server is running on port ${port}`);
|
|
|
}); |