diff --git a/package.json b/package.json index 9e03e73..bf44c96 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,9 @@ "main": "src/main.js", "dependencies": { "coffea": "^0.4.24", + "fs-extra": "^0.30.0", "mysql": "^2.11.1", - "repl": "^0.1.3", + "repl": "^0.1.3" }, "repository": { "type": "git", diff --git a/src/main.js b/src/main.js index bfb017f..2515c12 100644 --- a/src/main.js +++ b/src/main.js @@ -1,5 +1,5 @@ var cfg = require('../../cfg.json'); -var fs = require('fs'); +var fs = require('fs-extra'); var http = require('http'); var https = require('https'); var mysql = require('mysql'); @@ -9,8 +9,6 @@ var crypto = require('crypto'); var path = require('path'); var exec = require('child_process').exec; -var neu = true; - var sql; var haDC = () => { sql = mysql.createConnection(cfg.mysql); @@ -45,47 +43,49 @@ bot.on('motd', (e) => { bot.on('message', (e) => { var orig = e.message; - if(orig.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi)) { // shitpostcatcher - var tmp = orig.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links - tmp.forEach((entry,i,a) => { - var tmpdest = uuid.v1().split('-')[0]; - dl(entry, "./b/"+tmpdest, (cb) => { // download item - if(cb.status === true) { - var tmpuser = getUser(e.user.getNick(), e.network); - getCheckSum("./b/"+tmpdest+"."+cb.infos.ext, (cbcs) => { - checkRepostCheckSum(cbcs, (cbcrcs) => { - if(cbcrcs) { - sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`thumb`,`active`) values (?,?,?,?,?,?,?,?,?,?,?)", [ - entry, - "./b/"+tmpdest+"."+cb.infos.ext, - cb.infos.mime, - cb.infos.size, - cbcs, - tmpuser['nick'], - e.channel.getName(), - e.network, - Math.floor(new Date() / 1000), - '', - 0 - ]).on('result', (result) => { - neu = true; - e.reply("https://f0ck.me/"+result.insertId+" - "+path.parse(entry).base+" ("+cb.infos.mime+", ~"+formatSize(cb.infos.size)+") from "+tmpuser['nick']+" ("+tmpuser['username']+"@"+tmpuser['hostname']+")"); - }).on('error', (msg) => { - e.reply(msg); - }); - } - else { - fs.unlink("./b/"+tmpdest+"."+cb.infos.ext); // delete repost - e.reply("repost motherf0cker"); - } + if(orig.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi) && e.channel.getName() == "#f0ck") { // shitpostcatcher + if(!orig.match(/\!ignore$/)) { + var tmp = orig.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links + tmp.forEach((entry,i,a) => { + var tmpdest = uuid.v1().split('-')[0]; + dl(entry, "./b/"+tmpdest, (cb) => { // download item + if(cb.status === true) { + var tmpuser = getUser(e.user.getNick(), e.network); + getCheckSum("./b/"+tmpdest+"."+cb.infos.ext, (cbcs) => { + checkRepostCheckSum(cbcs, (cbcrcs) => { + if(cbcrcs) { + sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`thumb`,`active`) values (?,?,?,?,?,?,?,?,?,?,?)", [ + entry, + "./b/"+tmpdest+"."+cb.infos.ext, + cb.infos.mime, + cb.infos.size, + cbcs, + tmpuser['nick'], + e.channel.getName(), + e.network, + Math.floor(new Date() / 1000), + '', + 0 + ]).on('result', (result) => { + generateThumbs(); + e.reply("https://f0ck.me/"+result.insertId+" - "+path.parse(entry).base+" ("+cb.infos.mime+", ~"+formatSize(cb.infos.size)+") from "+tmpuser['nick']+" ("+tmpuser['username']+"@"+tmpuser['hostname']+")"); + }).on('error', (msg) => { + e.reply(msg); + }); + } + else { + fs.unlink("./b/"+tmpdest+"."+cb.infos.ext); // delete repost + e.reply("repost motherf0cker"); + } + }); }); - }); - } - else - if(cb.type == 1) - e.reply(cb.msg); + } + else + if(cb.type == 1) + e.reply(cb.msg); + }); }); - }); + } } else if(orig.match(/^\.user/)) { // (debug) get userinfos var tmp = getUser(e.user.getNick(), e.network); @@ -187,10 +187,10 @@ http.createServer((req, res) => { }; if(filePath == "./index.html") { // mainpage - sql.query("select * from `f0ck`.`items`", (err, rows, fields) => { + sql.query("select * from `f0ck`.`items` order by `id` desc", (err, rows, fields) => { var tmpres = "f0ck me!"; rows.forEach((e,i,a) => { - tmpres += "\n"; + tmpres += "
\n"; }); tmpres += ""; res.writeHead(200, { 'Content-Type': 'text/html' }); @@ -199,30 +199,38 @@ http.createServer((req, res) => { } else if(Number.isInteger(parseInt(url))) { // itempage sql.query("select * from `f0ck`.`items` where `id` = ? limit 1", url, (err, rows, fields) => { - var tmpres = ""; + var tmpres = ""; rows.forEach((e,i,a) => { - tmpres += "ID: "+e.id+"
\n"; - tmpres += "src: "+e.src+"
\n"; - tmpres += "dest: "+e.dest+"
\n"; - tmpres += "mime: "+e.mime+"
\n"; - tmpres += "size: "+formatSize(e.size)+"
\n"; - tmpres += "nick: "+e.username+"
\n"; - tmpres += "channel: "+e.userchannel+"
\n"; - tmpres += "network: "+e.usernetwork+"
\n"; + tmpres = "\n\n\n"+url+" - f0ck.me\n\n\n\n

f0ck.me

\n"; + tmpres += "
[ID: "+e.id+" | by: "+e.username+"]
\n"; + tmpres += "
"; switch(e.mime) { case "image/png": case "image/jpeg": case "image/gif": - tmpres += ""; + tmpres += ""; break; case "video/webm": case "video/mp4": - tmpres += ""; + tmpres += ""; break; case "audio/mpeg": tmpres += ""; break; } + tmpres += "

\n"; + tmpres += "
controls für vor und zurück :^)

\n"; + tmpres += "
"; + + tmpres += "src: "+e.src+"
"; + tmpres += "dest: "+e.dest+"
"; + tmpres += "mime: "+e.mime+" "; + tmpres += "size: "+formatSize(e.size)+"
"; + tmpres += "channel: "+e.userchannel+" "; + tmpres += "network: "+e.usernetwork+""; + + tmpres += "
\n\n"; + }); res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(tmpres, 'utf-8'); @@ -230,6 +238,56 @@ http.createServer((req, res) => { } else if(filePath.match(/^\.\/(b|s|t)\/.*/)) { // file contentType = mimeTypes[extname] || 'application/octect-stream'; + + switch(contentType) { + case "video/webm": + case "video/mp4": + case "audio/mpeg": + var start = 0; + var end = 0; + var range = req.headers['range']; + var stat = fs.statSync(filePath); + if(range != null) { + start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-'))); + end = parseInt(range.slice(range.indexOf('-')+1, range.length)); + } + if(isNaN(end) || end == 0) end = stat.size-1; + if(start > end) return; + res.writeHead(206, { + 'Connection':'close', + 'Content-Type':contentType, + 'Content-Length':end - start, + 'Content-Range':'bytes '+start+'-'+end+'/'+stat.size, + // 'Accept-Ranges':'bytes', + // 'Server':'CustomStreamer/0.0.1', + 'Transfer-Encoding':'chunked' + }); + var stream = fs.createReadStream(filePath, + { flags: 'r', start: start, end: end}); + stream.pipe(res); + break; + default: + fs.readFile(filePath, (error, content) => { + if(error) { + if(error.code == 'ENOENT') { + res.writeHead(200, { 'Content-Type': contentType }); + res.end('404 - file not found', 'utf-8'); + } + else { + res.writeHead(500); + res.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); + res.end(); + } + } + else { + res.writeHead(200, { 'Content-Type': contentType, 'Content-Length': content.length }); + res.end(content, 'utf-8'); + } + }); + break; + } + + fs.readFile(filePath, (error, content) => { if(error) { if(error.code == 'ENOENT') { @@ -243,8 +301,7 @@ http.createServer((req, res) => { } } else { - res.writeHead(200, { 'Content-Type': contentType, 'Content-Length': content.length }); - res.end(content, 'utf-8'); + } }); } @@ -257,28 +314,26 @@ http.createServer((req, res) => { // Thumbnailbackgroundworker -setInterval(()=>{generateThumbs();}, 300000); // 5 minutes +setInterval(()=>{generateThumbs();}, 60000); // 1 minute setTimeout(()=>{generateThumbs();}, 5000); // 5 seconds (start) var generateThumbs = () => { var outdir = './t/'; - - if(neu) { - sql.query("select * from `f0ck`.`items` where `thumb` = ''", (err, rows, fields) => { - rows.forEach((e,i,a) => { - if(!fs.existsSync(outdir+e.id+'.png')) { - exec('ffmpegthumbnailer -i'+e.dest+' -o'+outdir+e.id+'.png -a', (error) => { - if(error) { - //console.log(error); - //bot.send("#f0ck", "failed thumbnail for "+e.id, 'n0xy'); - } - else { - //bot.send("#f0ck", "generated thumbnail for "+e.id, 'n0xy'); - } - }); - } - }); + sql.query("select * from `f0ck`.`items` where `thumb` = ''", (err, rows, fields) => { + rows.forEach((e,i,a) => { + if(!fs.existsSync(outdir+e.id+'.png')) { + exec('ffmpegthumbnailer -i'+e.dest+' -o'+outdir+e.id+'.png -a', (error) => { + if(error) { + bot.send("#f0ck", "failed thumbnail for "+e.id+" ("+e.mime+")", 'n0xy'); + fs.unlink(outdir+e.id+'.png'); + fs.copySync('./s/mp3.png', outdir+e.id+'.png'); + // Standardthumb reinkopieren + } + else { + bot.send("#f0ck", "generated thumbnail for "+e.id+" ("+e.mime+")", 'n0xy'); + } + }); + } }); - - } + }); }; \ No newline at end of file