147 lines
4.5 KiB
JavaScript
147 lines
4.5 KiB
JavaScript
|
const express = require('express');
|
||
|
const multer = require('multer');
|
||
|
const path = require('path');
|
||
|
const print = require('./print');
|
||
|
const fs = require('fs');
|
||
|
const util = require('util');
|
||
|
const exec = require('child_process').exec;
|
||
|
|
||
|
const app = express();
|
||
|
const port = 3000;
|
||
|
|
||
|
// 设置multer存储引擎
|
||
|
const storage = multer.diskStorage({
|
||
|
destination: function (req, file, cb) {
|
||
|
cb(null, 'uploads/');
|
||
|
},
|
||
|
filename: function (req, file, cb) {
|
||
|
cb(null, Date.now() + path.extname(file.originalname)); // 重命名文件以避免冲突
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const upload = multer({ storage: storage });
|
||
|
|
||
|
// 创建uploads目录
|
||
|
const dir = './uploads';
|
||
|
if (!fs.existsSync(dir)){
|
||
|
fs.mkdirSync(dir);
|
||
|
}
|
||
|
|
||
|
// 提供静态文件服务
|
||
|
app.use(express.static('public'));
|
||
|
|
||
|
// 定义文件上传路由
|
||
|
app.post('/upload', upload.single('file'), (req, res) => {
|
||
|
if (!req.file) {
|
||
|
return res.status(400).send('No file uploaded.');
|
||
|
}
|
||
|
|
||
|
const filePath = req.file.path;
|
||
|
const printOption = req.body.printOption;
|
||
|
const pages = req.body.pages;
|
||
|
|
||
|
print.printFile(filePath, printOption, pages, (err) => {
|
||
|
if (err) {
|
||
|
return res.status(500).send('Error printing file: ' + err.message);
|
||
|
}
|
||
|
res.json({ message: 'File uploaded and printed successfully.', progress: 0 });
|
||
|
});
|
||
|
});
|
||
|
|
||
|
// 定义打印机配置路由
|
||
|
app.post('/configure-printer', express.json(), (req, res) => {
|
||
|
const printerName = req.body.printerName;
|
||
|
if (!printerName) {
|
||
|
return res.status(400).send('Printer name is required.');
|
||
|
}
|
||
|
|
||
|
// 读取现有配置
|
||
|
let config = {};
|
||
|
const configFilePath = './printerConfig.json';
|
||
|
if (fs.existsSync(configFilePath)) {
|
||
|
const configFileContent = fs.readFileSync(configFilePath, 'utf8');
|
||
|
config = JSON.parse(configFileContent);
|
||
|
}
|
||
|
|
||
|
// 更新配置
|
||
|
config.printerName = printerName;
|
||
|
|
||
|
// 写入配置文件
|
||
|
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
||
|
|
||
|
// 应用配置到系统
|
||
|
const applyConfig = (config) => {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
// 假设使用lpadmin命令来设置默认打印机
|
||
|
exec(`lpadmin -d ${config.printerName}`, (error, stdout, stderr) => {
|
||
|
if (error) {
|
||
|
reject(new Error(`Error applying configuration: ${stderr}`));
|
||
|
return;
|
||
|
}
|
||
|
resolve(`Configuration applied successfully: ${config.printerName}`);
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
|
||
|
applyConfig(config)
|
||
|
.then(message => {
|
||
|
res.json({ message });
|
||
|
})
|
||
|
.catch(error => {
|
||
|
res.status(500).json({ error: error.message });
|
||
|
});
|
||
|
});
|
||
|
|
||
|
// 获取当前配置
|
||
|
app.get('/get-config', (req, res) => {
|
||
|
const configFilePath = './printerConfig.json';
|
||
|
if (fs.existsSync(configFilePath)) {
|
||
|
const configFileContent = fs.readFileSync(configFilePath, 'utf8');
|
||
|
const config = JSON.parse(configFileContent);
|
||
|
res.json(config);
|
||
|
} else {
|
||
|
res.json({});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// 获取可用打印机列表
|
||
|
app.get('/get-printers', (req, res) => {
|
||
|
exec('lpstat -p -d', (error, stdout, stderr) => {
|
||
|
if (error) {
|
||
|
return res.status(500).send('Error getting printers: ' + stderr);
|
||
|
}
|
||
|
const printers = stdout.split('\n').map(line => line.trim()).filter(line => line.startsWith('printer'));
|
||
|
const printerList = printers.map(printer => printer.split(' ')[1]);
|
||
|
res.json(printerList);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
// 获取打印机状态
|
||
|
app.get('/get-printer-status', (req, res) => {
|
||
|
exec('lpstat -p', (error, stdout, stderr) => {
|
||
|
if (error) {
|
||
|
return res.status(500).send('Error getting printer status: ' + stderr);
|
||
|
}
|
||
|
const statusLines = stdout.split('\n').map(line => line.trim());
|
||
|
let status = '未配置';
|
||
|
if (statusLines.some(line => line.includes('disabled'))) {
|
||
|
status = '未开机';
|
||
|
} else if (statusLines.some(line => line.includes('printing'))) {
|
||
|
status = '打印中';
|
||
|
} else if (statusLines.some(line => line.includes('ready'))) {
|
||
|
status = '就绪';
|
||
|
}
|
||
|
res.json({ status });
|
||
|
});
|
||
|
});
|
||
|
|
||
|
// 获取打印进度
|
||
|
app.get('/get-print-progress', (req, res) => {
|
||
|
// 假设有一个全局变量来跟踪打印进度
|
||
|
let printProgress = 0; // 这里需要根据实际情况获取打印进度
|
||
|
res.json({ progress: printProgress });
|
||
|
});
|
||
|
|
||
|
app.listen(port,'0.0.0.0', () => {
|
||
|
console.log(`Server running at http://localhost:${port}`);
|
||
|
});
|