Browse Source

Started working on a downloads page

pull/4/head
jrtechs 6 years ago
parent
commit
c4b83e5fb5
5 changed files with 73 additions and 14 deletions
  1. +7
    -0
      README.md
  2. BIN
      downloads/content/1.jpg
  3. +51
    -0
      downloads/downloads.js
  4. +8
    -14
      server.js
  5. +7
    -0
      utils/sql.js

+ 7
- 0
README.md View File

@ -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,

BIN
downloads/content/1.jpg View File

Before After
Width: 750  |  Height: 914  |  Size: 72 KiB

+ 51
- 0
downloads/downloads.js View File

@ -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();
})
}
});
}
};

+ 8
- 14
server.js View File

@ -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);
})
}
});

+ 7
- 0
utils/sql.js View File

@ -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);
}
};

Loading…
Cancel
Save