-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscanimage-web.js
73 lines (52 loc) · 1.91 KB
/
scanimage-web.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var fs = require('fs');
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
function startScanning( socket, fileName, resolution, colorMode ) {
// start the scan and report any progress on the supplied socket
var jpegFileStream = fs.createWriteStream('../scans/' + fileName, {
flags : 'a'
});
var spawn = require('child_process').spawn, scanimage = spawn('scanimage', [
'--mode', colorMode, '--resolution', resolution, '--progress' ]);
pnmtojpeg = spawn('pnmtojpeg');
scanimage.stdout.pipe(pnmtojpeg.stdin);
scanimage.stderr.on('data', function (data) {
//console.log('(si)stderr: ' + data);
socket.emit('progressEvent',String(data));
});
scanimage.on('close', function (code) {
if (code !== 0) {
console.log('scanimage exited with code ' + code);
}
pnmtojpeg.stdin.end();
socket.emit('scanDoneEvent','1');
});
pnmtojpeg.stdout.pipe(jpegFileStream);
}
// app.use(express.static(__dirname + '/public'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.get('/', function(req, res) {
res.sendfile('index.html');
});
app.get('/scanimage-client.js', function(req, res) {
res.sendfile('scanimage-client.js');
});
app.get('/scanimage.css', function(req, res) {
res.sendfile('scanimage.css');
});
io.on('connection', function(socket) {
console.log('got connection.');
socket.on('parameterEvent', function(data) {
console.log('parameters received. initiate scanning...');
console.log('filename: ' + data.fileName + ', resolution: ' + data.resolution + ', colormode: ' + data.colorMode);
startScanning(socket, data.fileName, data.resolution, data.colorMode);
});
socket.on('stopScanEvent', function(data) {
console.log('stop scan event received. Trying to abort...');
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});