68 lines
2.0 KiB
JavaScript
68 lines
2.0 KiB
JavaScript
const spawn = require("child_process").spawn;
|
|
|
|
const EventEmitter = require('events').EventEmitter;
|
|
|
|
const path = require('path');
|
|
|
|
class PNG16Convertor {
|
|
constructor(app) {
|
|
this.app = app
|
|
this.path = this.app.config.png16.PNG16_PYTHON_PATH;
|
|
this.spawnCon = spawn('python', [this.path])
|
|
this.spawnCon.stdin.setDefaultEncoding('utf8');
|
|
this.spawnEvent = new EventEmitter();
|
|
//监听python关闭就再次打开
|
|
this.spawnCon.on('close', code => {
|
|
this.restart()
|
|
})
|
|
this.spawnCon.stdout.on('data', async data => {
|
|
if (this.spawnEvent.listeners('success')) {
|
|
this.spawnEvent.emit('success', data)
|
|
}
|
|
})
|
|
this.spawnCon.stderr.on('data', () => {
|
|
if (this.spawnEvent.listenerCount('error')) {
|
|
this.spawnEvent.emit('error')
|
|
}
|
|
})
|
|
}
|
|
|
|
restart() {
|
|
this.spawnCon = spawn('python', [this.path])
|
|
this.spawnCon.stdin.setDefaultEncoding('utf8')
|
|
}
|
|
|
|
sendMsg(data, image) {
|
|
return new Promise(async (resolve, reject) => {
|
|
try {
|
|
console.log("++++++++",data)
|
|
this.spawnCon.stdin.write(data);
|
|
this.spawnEvent.once('success', async (data) => {
|
|
this.spawnEvent.removeAllListeners();
|
|
await this.success(data, image)
|
|
return resolve(data);
|
|
})
|
|
this.spawnEvent.once('error', () => {
|
|
this.spawnEvent.removeAllListeners();
|
|
return reject();
|
|
})
|
|
} catch (error) {
|
|
return reject(error);
|
|
}
|
|
})
|
|
}
|
|
|
|
async success(createPng, image) {
|
|
createPng = createPng.toString().split("\n");
|
|
if (createPng && createPng.length > 0) {
|
|
// let image_id = createPng[1].match(/\w{24}/)[0];
|
|
image.pngPath = path.normalize(createPng[2].split('app')[1]).replace(/[\r\n]/g, "");
|
|
image.pngType = createPng[3].split(',')[0].replace(/[\r\n]/g, "");
|
|
image.pngWidth = createPng[3].split(',')[1].replace(/[\r\n]/g, "");
|
|
image.pngHeight = createPng[3].split(',')[2].replace(/[\r\n]/g, "");
|
|
await image.save()
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = PNG16Convertor; |