Created
December 18, 2014 12:23
-
-
Save IgorDePaula/2dd733752a17a7a1e013 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var kue = require('kue'), | |
jobs = kue.createQueue(); | |
var spawn = require('child_process').spawn; | |
var Apresentacao = require('../models/apresentacao'); | |
var path = require('path'); | |
var fs = require('fs'); | |
module.exports = function (io) { | |
io.on('connection', function (socket) { | |
jobs.process('create slide', 5, function (job, done) { | |
ppt2image(job.data.file, done); | |
}); | |
jobs.on('job enqueue', function (id, type) { | |
console.log('job %s got queued', id); | |
}); | |
jobs.on('job complete', function (id, result) { | |
var jpg = []; | |
Apresentacao.findOne({jobid: id}, function (err, ap) { | |
var dir = path.dirname(ap.file); | |
fs.readdir('public/' + dir, function (err, files) { | |
if (err) | |
throw err; | |
files.filter(function (value) { | |
var ext = value.split('.'); | |
if (ext[ext.length - 1] == 'jpg') { | |
jpg.push('/' + ap.user + '/' + ap.slug + '/' + value); | |
} | |
}) | |
jpg.sort(natcmp); | |
ap.job = true; | |
ap.images = jpg; | |
ap.save(function (err) { | |
if (err) { | |
console.log(err); | |
} | |
socket.emit('free', {id: id}); | |
console.log("Updated " + id); | |
}) | |
}) | |
}); | |
}); | |
}) | |
} | |
function ppt2image(file, done) { | |
var unoconv = spawn("unoconv", ["-f", "pdf", "-o", file+'.pdf', file]); | |
unoconv.stdout.on('data', function (data) { | |
console.log('stdout: ' + data); | |
}); | |
unoconv.stderr.on('data', function (data) { | |
console.log('stderr: ' + data); | |
}); | |
unoconv.on('close', function (code) { | |
var convert = spawn("convert", ["-resize", "1200", "-density", "200", file+'.pdf', file + '.jpg']); | |
convert.stdout.on('data', function (data) { | |
console.log('stdout: ' + data); | |
}); | |
convert.stderr.on('data', function (data) { | |
console.log('stderr: ' + data); | |
}); | |
convert.on('close', function (code) { | |
console.log('child process exited with code ' + code); | |
done && done(0); | |
}) | |
}); | |
} | |
function strcmp(a, b) { | |
return a > b ? 1 : a < b ? -1 : 0; | |
} | |
function natcmp(a, b) { | |
var x = [], y = []; | |
a.replace(/(\d+)|(\D+)/g, function ($0, $1, $2) { | |
x.push([$1 || 0, $2]) | |
}) | |
b.replace(/(\d+)|(\D+)/g, function ($0, $1, $2) { | |
y.push([$1 || 0, $2]) | |
}) | |
while (x.length && y.length) { | |
var xx = x.shift(); | |
var yy = y.shift(); | |
var nn = (xx[0] - yy[0]) || strcmp(xx[1], yy[1]); | |
if (nn) | |
return nn; | |
} | |
if (x.length) | |
return -1; | |
if (y.length) | |
return +1; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment