new thumbnailer

This commit is contained in:
Flummi
2016-09-11 17:08:16 +02:00
parent c3a063411b
commit e448db3f59
3 changed files with 50 additions and 12 deletions

View File

@ -98,6 +98,38 @@ Lib.prototype.getCheckSum = (file, cbcs) => {
});
};
Lib.prototype.generateThumbs = () => {
var outdir = './t';
Lib.prototype.sql.query("select * from `f0ck`.`items`", (err, rows, fields) => {
rows.forEach((e,i,a) => {
var thumbnail = outdir+'/'+e.id+'.png';
if(!fs.existsSync(thumbnail)) {
var cmd;
switch(e.mime) {
case "video/mp4":
case "video/webm":
case "audio/mpeg":
case "audio/ogg":
case "image/gif":
cmd = 'ffmpeg -i '+e.dest+' -vframes 1 -filter "scale=-1:256,crop=128:128" '+thumbnail;
break;
case "image/png":
case "image/jpeg":
cmd = 'convert -thumbnail 256x256 '+e.dest+' -gravity center -crop 128x128+0+0 +repage '+thumbnail;
break;
}
exec(cmd, (error) => {
if(error) {
Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+') '+JSON.stringify(error));
fs.copySync('./s/mp3.png', thumbnail); // copy standardthumbnail
}
else
Lib.prototype.log("generated thumbnail for "+e.id+" ("+e.mime+")");
});
}
});
});
};
/*Lib.prototype.generateThumbs = () => {
var outdir = './t/';
Lib.prototype.sql.query("select * from `f0ck`.`items`", (err, rows, fields) => {
rows.forEach((e,i,a) => {
@ -120,7 +152,7 @@ Lib.prototype.generateThumbs = () => {
}
});
});
};
};*/
Lib.prototype.log = (msg) => {
if(Lib.prototype.debug)
bot.send(Lib.prototype.cfg.debugchannel, msg, 'n0xy');

View File

@ -3,19 +3,25 @@
module.exports = (bot, lib) => {
lib.trigger.add({
name: 'thumbnailer',
call: /^\!thumb (\d+)$/i,
call: /^\!thumb(.*\d+)?$/i,
level: 100,
active: 1,
func: (e) => {
var id = e.message.split(' ')[1];
if(Number.isInteger(parseInt(id))) {
fs.unlink('./t/'+id+'.png', () => {
//e.reply('debug: Thumbnail gelöscht');
lib.generateThumbs();
//e.reply('debug: sollte Thumbnail generiert haben');
});
var id;
if(id = e.message.split(' ')[1]) {
if(Number.isInteger(parseInt(id)))
fs.unlink('./t/'+id+'.png', () => lib.generateThumbs() );
}
else {
deleteFolderRecursive('./t');
lib.generateThumbs();
}
},
desc: 'regenerate thumbnail'
desc: 'generate thumbnail'
});
};
var deleteFolderRecursive = (path) => {
if(fs.existsSync(path))
fs.readdirSync(path).forEach((file,index) => (fs.lstatSync(path+"/"+file).isDirectory())?deleteFolderRecursive(path+"/"+file):fs.unlinkSync(path+"/"+file) );
};