diff --git a/README.md b/README.md index a68bb84..85d96dc 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,13 @@ url varchar(100) not null, primary key(post_id) ); +create table downloads( +download_id mediumint unsigned not null AUTO_INCREMENT, +url varchar(20) not null, +file varchar(20) not null, +primary key(download_id) +); + create table popular_posts( popular_post_id mediumint unsigned not null AUTO_INCREMENT, post_id mediumint unsigned not null, diff --git a/downloads/content/1.jpg b/downloads/content/1.jpg new file mode 100644 index 0000000..7b11512 Binary files /dev/null and b/downloads/content/1.jpg differ diff --git a/downloads/downloads.js b/downloads/downloads.js new file mode 100644 index 0000000..aee0448 --- /dev/null +++ b/downloads/downloads.js @@ -0,0 +1,51 @@ +/** + Utilities is a node modules created to make tasks like + including html files easier for me programming. + */ + +const fs = require('fs'); + +var Promise = require('promise'); + +const utils = require('../utils/utils.js'); + +const sql = require('../utils/sql'); + +module.exports= + { + main: function(res, requestURL, request) + { + res.setHeader('Content-disposition', 'attachment; filename=dramaticpenguin.MOV'); + return new Promise(function(resolve, reject) + { + var urlSplit = requestURL.split("/"); + console.log(urlSplit); + if(urlSplit.length == 3 || urlSplit.length == 4) + { + sql.getDownload(urlSplit[2]).then(function(result) + { + console.log(result); + if(result.length == 1) + { + var file = './downloads/content/' + result[0].file; + res.download(file); + } + else + { + utils.print404(res).then(function() + { + resolve(); + }) + } + }); + } + else + { + utils.print404(res).then(function() + { + resolve(); + }) + } + }); + } + }; \ No newline at end of file diff --git a/server.js b/server.js index ec39400..4617c22 100644 --- a/server.js +++ b/server.js @@ -21,18 +21,7 @@ const utils = require('./utils/utils.js'); var app = express(); -// var https = require('https'); - -//var key = fs.readFileSync('private.key'); -//var cert = fs.readFileSync( 'primary.crt' ); -//var ca = fs.readFileSync( 'encryption/intermediate.crt' ); - -// var options = { -// key: key, -// cert: cert, -// ca: ca -// }; app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }})); app.use(function(request, res) @@ -49,12 +38,16 @@ app.use(function(request, res) { includes.sendCSS(res, filename) } + // else if(filename.includes(/download/)) + // { + // require("./downloads/downloads.js").main(res, filename, request); + // } else { var file = ""; if(filename === '' || filename === '/') - filename = '/category/Java'; + filename = '/web-development/why-i-stopped-using-wordpress'; var urlSplit = filename.split("/"); @@ -67,15 +60,16 @@ app.use(function(request, res) else file = "./posts/posts.js"; + includes.printHeader(res).then(function() { return require(file).main(res, filename, request); }).then(function() { return includes.printFooter(res); - }).then(function() + }).catch(function(err) { - //console.log("fin"); //for debugging + console.log(err); }) } }); diff --git a/utils/sql.js b/utils/sql.js index 0d668e5..7bc6402 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -257,5 +257,12 @@ module.exports= getCategory: function(categoryId) { return fetch("select * from categories where category_id='" + categoryId + "'"); + }, + + getDownload: function(downloadURL) + { + var cleanD = sanitizer.sanitize(downloadURL); + var q = "select * from downloads where url='" + cleanD + "' limit 1"; + return fetch(q); } }; \ No newline at end of file