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

@ -8,7 +8,7 @@
<body>
<div class="navbar"><a href="/">f0ck.me</a> | <a href="/how">how to</a> | <a href="/contact">Contact</a></div>
<ul id="posts" data-last="{{ last }}">
{% for item in items %}<li class="post"><a href="//f0ck.me/{{ item.id }}" title="{{ item.mime }}"><img class="thumb" src="//f0ck.me/t/{{ item.id }}.png" /></a></li>
{% for item in items %}<li class="post"><a href="./{{ item.id }}" title="{{ item.mime }}"><img class="thumb" src="./t/{{ item.id }}.png" /></a></li>
{% endfor %}
</ul>
<script src="./s/jquery-3.1.0.min.js"></script>
@ -22,7 +22,7 @@
var html = "";
for(var i = 0; i < msg.items.length; i++)
if(msg.items[i].id)
html += "<li class=\"post\"><a href=\"//f0ck.me/"+msg.items[i].id+"\" title=\""+msg.items[i].mime+"\"><img class=\"thumb\" src=\"//f0ck.me/t/"+msg.items[i].id+".png\" /></a></li>\n";
html += "<li class=\"post\"><a href=\"./"+msg.items[i].id+"\" title=\""+msg.items[i].mime+"\"><img class=\"thumb\" src=\"./t/"+msg.items[i].id+".png\" /></a></li>\n";
$('#posts').append(html);
$('#posts').data('last', msg.last);
load = false;

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');
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();
//e.reply('debug: sollte Thumbnail generiert haben');
});
}
},
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) );
};