From e448db3f5959ed77cdb15f4b6eeed23dbaa92aa7 Mon Sep 17 00:00:00 2001 From: Flummi Date: Sun, 11 Sep 2016 17:08:16 +0200 Subject: [PATCH] new thumbnailer --- s/index.tpl.html | 4 ++-- src/lib.js | 34 +++++++++++++++++++++++++++++++++- src/trigger/thumb.js | 24 +++++++++++++++--------- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/s/index.tpl.html b/s/index.tpl.html index a6db6a5..83a6f45 100644 --- a/s/index.tpl.html +++ b/s/index.tpl.html @@ -8,7 +8,7 @@ @@ -22,7 +22,7 @@ var html = ""; for(var i = 0; i < msg.items.length; i++) if(msg.items[i].id) - html += "
  • \n"; + html += "
  • \n"; $('#posts').append(html); $('#posts').data('last', msg.last); load = false; diff --git a/src/lib.js b/src/lib.js index 215e712..dbcd9dd 100644 --- a/src/lib.js +++ b/src/lib.js @@ -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'); diff --git a/src/trigger/thumb.js b/src/trigger/thumb.js index 5724483..c347459 100644 --- a/src/trigger/thumb.js +++ b/src/trigger/thumb.js @@ -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) ); }; \ No newline at end of file