Skip to content
Browse files

Change some components to levelup from noDB

Way too much code duplication in the added lib/* files. Also need to
move sessions away from noDB.
  • Loading branch information...
1 parent 548406d commit aa3c73e8bb5a2b2b92887b401668bb7050018c9a @davidbanham committed
Showing with 311 additions and 134 deletions.
  1. +10 −8 app.js
  2. +26 −0 data/migrate.js
  3. +33 −0 lib/feed.js
  4. +33 −0 lib/hit.js
  5. +33 −0 lib/tag.js
  6. +34 −0 lib/user.js
  7. +2 −1 package.json
  8. +7 −5 routes/auth.js
  9. +78 −81 routes/feeds.js
  10. +27 −21 routes/queue.js
  11. +12 −8 routes/tags.js
  12. +16 −10 routes/users.js
View
18 app.js
@@ -13,12 +13,11 @@ var express = require('express')
, socketio = require('socket.io')
, conf = require('./config/conf.js')
, diff = require('jsondiffpatch')
+, userService = require('./lib/user.js')
, store = new express.session.MemoryStore
, basePath = __dirname+'/'
, freeDiskSpace = ''
, mungedDirectory = require('./lib/middleware/directory.js')
-, tags = JSON.parse(fs.readFileSync('data/tags.json'))
-, users = JSON.parse(fs.readFileSync('data/users.json'));
store.sessions = JSON.parse(fs.readFileSync('data/sessions.json'));
express.static.mime.define({'video/mkv': ['mkv']});
@@ -183,11 +182,15 @@ var stupidApacheBytesToSize = function (bytes, precision)
var authenticate = function(login, password, callback) {
var pass = hashPass(password);
- if (typeof users[login] !== 'undefined') {
- if(users[login].pass === pass) callback(users[login]);
+ userService.keys(function(err, users) {
+ if (users.indexOf(login) > -1 ) {
+ userService.get(login, function(err, user) {
+ if(user.pass === pass) callback(user);
+ else callback(null);
+ });
+ }
else callback(null);
- }
- else callback(null);
+ });
};
var hashPass = function(password) {
@@ -234,10 +237,9 @@ app.util = {
, downloadDir: ''
, getInfoHash: getInfoHash
, createFullPath: createFullPath
- , users: users
+ , userService: userService
, basePath: basePath
, rt: rt
- , tags: tags
, stupidApacheBytesToSize: stupidApacheBytesToSize
, torrentChanges: torrentChanges
};
View
26 data/migrate.js
@@ -0,0 +1,26 @@
+var levelup = require('levelup');
+var userdb = levelup('./users');
+var feeddb = levelup('./feeds');
+var hitdb = levelup('./hits');
+var tagdb = levelup('./tags');
+var fs = require('fs');
+var oldusers = JSON.parse(fs.readFileSync('./users.json').toString());
+var oldfeeds = JSON.parse(fs.readFileSync('./feedTargets.json').toString());
+var oldhits = JSON.parse(fs.readFileSync('./feedHits.json').toString());
+var oldtags = JSON.parse(fs.readFileSync('./tags.json').toString());
+for (user in oldusers) {
+ oldusers[user].email = user;
+ userdb.put(user, JSON.stringify(oldusers[user]));
+}
+for (var i = 0 ; i < oldfeeds.length ; i++ ) {
+ var feed = oldfeeds[i];
+ feeddb.put(feed.yes, JSON.stringify(feed));
+}
+for (var i = 0 ; i < oldhits.length ; i++ ) {
+ var hit = oldhits[i];
+ hitdb.put(hit.url, JSON.stringify(hit));
+};
+for (var i = 0 ; i < oldtags.length ; i++ ) {
+ var tag = oldtags[i];
+ tagdb.put(tag.elements.join('/'), JSON.stringify(tag));
+};
View
33 lib/feed.js
@@ -0,0 +1,33 @@
+var levelup = require('levelup');
+var db = levelup('./data/feeds');
+module.exports = {
+ get: function(key, cb) {
+ db.get(name, function(err, value) {
+ cb(err, JSON.parse(value));
+ });
+ }
+ , store: function(feed, cb) {
+ db.put(feed.yes, JSON.stringify(feed), cb);
+ }
+ , del: function(key, cb) {
+ db.del(key, cb);
+ }
+ , keys: function(cb) {
+ keys = [];
+ db.createKeyStream()
+ .on('data', function(data) {
+ keys.push(data)
+ }).on('end', function() {
+ cb(null, keys);
+ })
+ }
+ , all: function(cb) {
+ var res = {};
+ db.createReadStream()
+ .on('data', function(data) {
+ res[data.key] = JSON.parse(data.value)
+ }).on('end', function() {
+ cb(null, res);
+ })
+ }
+};
View
33 lib/hit.js
@@ -0,0 +1,33 @@
+var levelup = require('levelup');
+var db = levelup('./data/hits');
+module.exports = {
+ get: function(key, cb) {
+ db.get(name, function(err, value) {
+ cb(err, JSON.parse(value));
+ });
+ }
+ , store: function(hit, cb) {
+ db.put(hit.url, JSON.stringify(hit), cb);
+ }
+ , del: function(key, cb) {
+ db.del(key, cb);
+ }
+ , keys: function(cb) {
+ keys = [];
+ db.createKeyStream()
+ .on('data', function(data) {
+ keys.push(data)
+ }).on('end', function() {
+ cb(null, keys);
+ })
+ }
+ , all: function(cb) {
+ var res = {};
+ db.createReadStream()
+ .on('data', function(data) {
+ res[data.key] = JSON.parse(data.value)
+ }).on('end', function() {
+ cb(null, res);
+ })
+ }
+}
View
33 lib/tag.js
@@ -0,0 +1,33 @@
+var levelup = require('levelup');
+var db = levelup('./data/tags');
+module.exports = {
+ get: function(key, cb) {
+ db.get(name, function(err, value) {
+ cb(err, JSON.parse(value));
+ });
+ }
+ , store: function(tag, cb) {
+ db.put(tag.elements.join('/'), JSON.stringify(tag), cb);
+ }
+ , del: function(tag, cb) {
+ db.del(tag.elements.join('/'), cb);
+ }
+ , keys: function(cb) {
+ keys = [];
+ db.createKeyStream()
+ .on('data', function(data) {
+ keys.push(data)
+ }).on('end', function() {
+ cb(null, keys);
+ })
+ }
+ , all: function(cb) {
+ var res = {};
+ db.createReadStream()
+ .on('data', function(data) {
+ res[data.key] = JSON.parse(data.value)
+ }).on('end', function() {
+ cb(null, res);
+ })
+ }
+}
View
34 lib/user.js
@@ -0,0 +1,34 @@
+var levelup = require('levelup');
+var db = levelup('./data/users');
+user = {
+ get: function(name, cb) {
+ db.get(name, function(err, value) {
+ cb(err, JSON.parse(value));
+ });
+ }
+ , store: function(user, cb) {
+ db.put(user.email, JSON.stringify(user), cb);
+ }
+ , del: function(key, cb) {
+ db.del(key, cb);
+ }
+ , keys: function(cb) {
+ keys = [];
+ db.createKeyStream()
+ .on('data', function(data) {
+ keys.push(data)
+ }).on('end', function() {
+ cb(null, keys);
+ })
+ }
+ , all: function(cb) {
+ var res = {};
+ db.createReadStream()
+ .on('data', function(data) {
+ res[data.key] = JSON.parse(data.value)
+ }).on('end', function() {
+ cb(null, res);
+ })
+ }
+};
+module.exports = user;
View
3 package.json
@@ -13,7 +13,8 @@
"socket.io": "~0.9.13",
"cookie": "0.0.5",
"connect": "~2.7.2",
- "jsondiffpatch": "0.0.7"
+ "jsondiffpatch": "0.0.7",
+ "levelup": "~0.7.0"
},
"readmeFilename": "README.md",
"gitHead": "a85e84b0a28b43d55e4076330ce354e362eb924f",
View
12 routes/auth.js
@@ -1,7 +1,7 @@
var fs = require('fs')
, cookie = require('cookie');
module.exports = function(app) {
- var users = app.util.users
+ var userService = app.util.userService
, hashPass = app.util.hashPass;
app.get('/checkAuth', function(req,res) {
if (req.session.user) res.send(200, {sid: cookie.parse(req.headers.cookie)['ntor.sid'], email: req.session.user.email});
@@ -31,11 +31,13 @@ module.exports = function(app) {
app.post('/changePass', app.util.requiresLevel(0), function(req,res) {
var pass = hashPass(req.body.oldPassword);
- if ( pass !== users[req.session.user.email].pass ) return res.send('Old password was wrong', 403);
+ if ( pass !== req.session.user.pass ) return res.send('Old password was wrong', 403);
pass = hashPass(req.body.newPassword);
- users[req.session.user.email].pass = pass;
- fs.writeFileSync('data/users.json', JSON.stringify(users));
- res.send({status: 'success'});
+ req.session.user.pass = pass;
+ userService.store(req.session.user, function(err) {
+ if (err) return res.send(500, {status: 'fail'});
+ res.send({status: 'success'});
+ });
});
};
View
159 routes/feeds.js
@@ -1,7 +1,8 @@
var fs = require('fs')
, nodemailer = require('nodemailer');
-var feedTargets = JSON.parse(fs.readFileSync('data/feedTargets.json'))
-, feedHits = JSON.parse(fs.readFileSync('data/feedHits.json'));
+
+var feedService = require('../lib/feed.js');
+var hitService = require('../lib/hit.js');
module.exports = function(app) {
var basePath = app.util.basePath
@@ -36,51 +37,49 @@ module.exports = function(app) {
var checkFeedWorker = function(worker) {
worker.items(function(err, results){
- var matches = results.filter(function(elem){
- for ( var i = 0 ; i < feedTargets.length ; i++ ) {
- var now = new Date().getTime();
- if (now - feedTargets[i].lastHit < feedTargets[i].frequency) continue;
- if (elem.name.search(feedTargets[i].yes) > -1 ) {
- if (feedTargets[i].no !== '') {
- if (elem.name.search(feedTargets[i].no) < 0) dlFeed(elem.url, worker );
- } else {
- dlFeed(elem, worker, feedTargets[i]);
+ feedService.all(function(err, targets) {
+ var matches = results.filter(function(elem){
+ for (key in targets) {
+ var target = targets[key];
+ var now = new Date().getTime();
+ if (now - target.lastHit < target.frequency) return;
+ if (elem.name.search(target.yes) > -1 ) {
+ if (elem.name.search(target.no) < 0 ) dlFeed(elem, worker, target);
}
}
- }
+ });
});
});
};
- var dlFeed = function(item, engine, feedTarget) {
- for ( var i = 0 ; i < feedHits.length ; i++ ) {
- if ( feedHits[i].url === item.url) return;
- }
- feedHits.push(item);
- fs.writeFileSync('data/feedHits.json', JSON.stringify(feedHits));
- feedTarget.lastHit = new Date().getTime();
- fs.writeFileSync('data/feedTargets.json', JSON.stringify(feedTargets));
- var relativePath = 'torrentBin/'+item.name+'.torrent';
- engine.download(item.url, relativePath, function(error) {
- if (error) console.error('Feed DL error '+JSON.stringify(feedTarget)+' '+JSON.stringify(engine));
- else {
- var fullFilePath = basePath+relativePath;
- getInfoHash(fullFilePath, function(infoHash) {
- rt.upload(fullFilePath, function(error) {
- if (error) return console.error(error);
- var fullDownloadPath = app.util.downloadDir+'/'+feedTarget.tag.elements.join('/');
- app.util.createFullPath(fullDownloadPath, function() {
- if (error) return console.error('Failed to create download directory');
- rt.setPath(infoHash, fullDownloadPath, function(error, data) {
- rt.start(infoHash, function(error, data) {
- if (error) console.error('Hash error!'+error);
+ var dlFeed = function(item, engine, target) {
+ hitService.get(item.url, function(err, hit) {
+ if (typeof hit !== 'undefined') return;
+ hitService.store(item);
+ target.lastHit = new Date().getTime();
+ feedService.store(target);
+ var relativePath = 'torrentBin/'+item.name+'.torrent';
+ engine.download(item.url, relativePath, function(error) {
+ if (error) console.error('Feed DL error '+JSON.stringify(target)+' '+JSON.stringify(engine));
+ else {
+ var fullFilePath = basePath+relativePath;
+ getInfoHash(fullFilePath, function(infoHash) {
+ rt.upload(fullFilePath, function(error) {
+ if (error) return console.error(error);
+ var fullDownloadPath = app.util.downloadDir+'/'+target.tag.elements.join('/');
+ app.util.createFullPath(fullDownloadPath, function() {
+ if (error) return console.error('Failed to create download directory');
+ rt.setPath(infoHash, fullDownloadPath, function(error, data) {
+ rt.start(infoHash, function(error, data) {
+ if (error) console.error('Hash error!'+error);
+ });
});
});
});
});
- });
- notify(feedTarget, item);
- }
+ notify(target, item);
+ }
+ });
});
};
var notify = function(target, item) {
@@ -104,30 +103,29 @@ module.exports = function(app) {
};
app.get('/feed/target', app.util.requiresLevel(0), function(req,res) {
var feedItems = [];
- for ( var i = 0 ; i < feedTargets.length ; i ++ ) {
- var item = {
- yes: feedTargets[i].yes
- , no: feedTargets[i].no
- , tag: feedTargets[i].tag
- , frequency: feedTargets[i].frequency
- , lastHit: feedTargets[i].lastHit
- , id: feedTargets[i].id
+ feedService.all(function(err, feeds) {
+ for (key in feeds) {
+ var feed = feeds[key];
+ var item = {
+ yes: feed.yes
+ , no: feed.no
+ , tag: feed.tag
+ , frequency: feed.frequency
+ , lastHit: feed.lastHit
+ , id: feed.yes
+ };
+ if ( feed.notificationList.indexOf(req.session.user.email) >= 0 ) {
+ item.notifyMe = true;
+ } else {
+ item.notifyMe = false;
+ }
+ feedItems.push(item);
};
- if ( feedTargets[i].notificationList.indexOf(req.session.user.email) >= 0 ) {
- item.notifyMe = true;
- } else {
- item.notifyMe = false;
- }
- feedItems.push(item);
- }
- res.send(feedItems);
+ res.send(feedItems);
+ });
});
app.post('/feed/target', app.util.requiresLevel(0), function(req,res) {
- var maxId = 0;
- for (var i = 0 ; i < feedTargets.length ; i++ ) {
- if ( feedTargets[i].id > maxId ) maxId = feedTargets[i].id;
- }
var notificationList = [];
if (req.body.notify === true) notificationList.push(req.session.user.email);
var newTarget = {
@@ -136,41 +134,40 @@ module.exports = function(app) {
, notificationList: notificationList
, tag: req.body.tag
, frequency: req.body.frequency
- , id: maxId + 1
, lastHit: 0
};
- feedTargets.push(newTarget);
- fs.writeFileSync('data/feedTargets.json', JSON.stringify(feedTargets));
- res.send('success');
+ feedService.store(newTarget, function(err) {
+ if (err) return res.send(500, 'fail');
+ return res.send('success');
+ });
});
app.del('/feed/target/:id', app.util.requiresLevel(0), function(req,res) {
- var filteredTargets = feedTargets.filter(function(x) {
- return ( x.id != req.params.id );
+ feedService.del(req.params.id, function(err) {
+ if (err) return res.send(500, 'fail');
+ return res.send('success');
});
- feedTargets = filteredTargets;
- fs.writeFileSync('data/feedTargets.json', JSON.stringify(filteredTargets));
- res.send('success');
});
app.post('/removeNotificationTarget', app.util.requiresLevel(0), function(req,res) {
- for ( var i = 0 ; i < feedTargets.length ; i++ ) {
- if ( feedTargets[i].id == req.body.targetId ) {
- var notificationTargetPos = feedTargets[i].notificationList.indexOf(req.session.user.email);
- feedTargets[i].notificationList.splice(notificationTargetPos, 1);
- }
- }
- fs.writeFileSync('data/feedTargets.json', JSON.stringify(feedTargets));
- res.send('success');
+ feedService.get(req.body.targetId, function(err, target) {
+ var notificationTargetPos = target.notificationList.indexOf(req.session.user.email);
+ target.notificationList.splice(notificationTargetPos, 1);
+ feedService.store(target, function(err) {
+ if (err) return res.send(500, 'fail');
+ return res.send('success');
+ });
+ });
});
app.post('/addNotificationTarget', app.util.requiresLevel(0), function(req,res) {
- for ( var i = 0 ; i < feedTargets.length ; i++ ) {
- if ( feedTargets[i].id == req.body.targetId ) {
- feedTargets[i].notificationList.push(req.session.user.email);
- }
- }
- fs.writeFileSync('data/feedTargets.json', JSON.stringify(feedTargets));
- res.send('success');
+ feedService.get(req.body.targetId, function(err, target) {
+ if ( target.notificationList.indexOf(req.session.user.email) > -1 ) return res.send(409, 'duplicate');
+ target.notificationList.push(req.session.user.email);
+ feedService.store(target, function(err) {
+ if (err) return res.send(500, 'fail');
+ return res.send('success');
+ });
+ });
});
};
View
48 routes/queue.js
@@ -1,13 +1,14 @@
var fs = require('fs');
module.exports = function(app) {
var users = app.util.users;
+ var userService = app.util.userService;
app.get('/queue', app.util.requiresLevel(0), function(req,res) {
- res.send(users[req.session.user.email].queue);
+ res.send(req.session.user.queue);
});
app.post('/queue/item', app.util.requiresLevel(0), function(req,res) {
- if ( typeof users[req.session.user.email].queue === 'undefined' ) users[req.session.user.email].queue = [];
- var existingQueue = users[req.session.user.email].queue;
+ if ( typeof req.session.user.queue === 'undefined' ) req.session.user.queue = [];
+ var existingQueue = req.session.user.queue;
for (var i = 0 ; i < existingQueue.length ; i++) {
if (existingQueue[i].path === req.body.path) return res.send('duplicate');
}
@@ -29,10 +30,11 @@ module.exports = function(app) {
, downloaded: 0
};
if ( exists ) {
- users[req.session.user.email].queue.push(item);
- fs.writeFileSync('data/users.json', JSON.stringify(users));
- app.util.io.sockets.in(req.session.user.email).emit('queueItem', {action: 'push', item: item});
- res.send({status: 'success'});
+ req.session.user.queue.push(item);
+ userService.store(req.session.user, function(err) {
+ if (err) return res.send(500, {status: "fail"});
+ res.send({status: 'success'});
+ });
} else {
res.send(400, 'path does not exist');
}
@@ -40,7 +42,7 @@ module.exports = function(app) {
});
app.post('/queue/item/claim', app.util.requiresLevel(0), function(req,res) {
- var queue = users[req.session.user.email].queue;
+ var queue = req.session.user.queue;
var found, conflict = false;
if ( typeof queue === 'undefined' ) return res.send('queue undef');
if ( queue.length === 0 ) return res.send('length 0');
@@ -58,13 +60,15 @@ module.exports = function(app) {
});
app.get('/clearQueue', app.util.requiresLevel(0), function(req,res) {
- users[req.session.user.email].queue = [];
- fs.writeFileSync('data/users.json', JSON.stringify(users));
- res.send({status: 'success'});
+ req.session.user.queue = [];
+ userService.store(req.session.user, function(err) {
+ if (err) return res.send(500, {status: "fail"});
+ res.send({status: 'success'});
+ });
});
app.post('/queue/remove', app.util.requiresLevel(0), function(req,res) {
- var queue = users[req.session.user.email].queue;
+ var queue = req.session.user.queue;
if ( typeof queue === 'undefined' ) return res.send(400, {status: 'error', message: 'queue undef'});
if ( queue.length === 0 ) return res.send(400, {status: 'error', message: 'Nothing in the queue'});
var pos = -1;
@@ -76,20 +80,22 @@ module.exports = function(app) {
}
if ( pos < 0 ) return res.send(400, {status: 'error', message: 'item not found'});
queue.splice(pos, 1);
- fs.writeFileSync('data/users.json', JSON.stringify(users));
- app.util.io.sockets.in(req.session.user.email).emit('queueItem', {action: 'delete', path: req.body.path});
- res.send({status: 'success'});
+ userService.store(req.session.user, function(err) {
+ if (err) return res.send(500, {status: "fail"});
+ app.util.io.sockets.in(req.session.user.email).emit('queueItem', {action: 'delete', path: req.body.path});
+ res.send({status: 'success'});
+ });
});
app.get('/queue/item', app.util.requiresLevel(0), function(req,res) {
- if(typeof users[req.session.user.email].queue === 'undefined') return res.send('');
- if(typeof users[req.session.user.email].queue[0] === 'undefined') return res.send('');
- res.send(users[req.session.user.email].queue[0]);
+ if(typeof req.session.user.queue === 'undefined') return res.send('');
+ if(typeof req.session.user.queue[0] === 'undefined') return res.send('');
+ res.send(req.session.user.queue[0]);
});
app.get('/queue/item/path', app.util.requiresLevel(0), function(req,res) {
- if(typeof users[req.session.user.email].queue === 'undefined') return res.send('');
- if(typeof users[req.session.user.email].queue[0] === 'undefined') return res.send('');
- res.send(users[req.session.user.email].queue[0].path);
+ if(typeof req.session.user.queue === 'undefined') return res.send('');
+ if(typeof req.session.user.queue[0] === 'undefined') return res.send('');
+ res.send(req.session.user.queue[0].path);
});
};
View
20 routes/tags.js
@@ -1,19 +1,23 @@
-var fs = require('fs');
+var tagService = require('../lib/tag.js');
module.exports = function(app) {
app.get('/tag/all', app.util.requiresLevel(0), function(req,res) {
- res.send(app.util.tags);
+ tagService.all(function(err, tags) {
+ return res.send(tags);
+ });
});
app.post('/tag/remove', app.util.requiresLevel(0), function(req,res) {
- var pos = app.util.tags.indexOf(req.params.tag);
- app.util.tags.splice(pos, 1);
- res.send({success: true});
+ tagService.del(tag, function(err) {
+ if (err) return res.send(500);
+ return res.send({success: true});
+ });
});
app.post('/tag/add', app.util.requiresLevel(0), function(req,res) {
- app.util.tags.push(req.body.tag);
- fs.writeFileSync('data/tags.json', JSON.stringify(app.util.tags));
- res.send({success: true});
+ tagService.store(tag, function(err) {
+ if (err) return res.send(500);
+ return res.send({success: true});
+ });
});
};
View
26 routes/users.js
@@ -1,24 +1,30 @@
var fs = require('fs');
module.exports = function(app) {
- var users = app.util.users;
+ var userService = app.util.userService;
app.post('/user', app.util.requiresLevel(50), function(req,res) {
var pass = app.util.hashPass(req.body.password);
- users[req.body.email] = { pass: pass, level: req.body.level };
- fs.writeFileSync('data/users.json', JSON.stringify(users));
- res.send({status: 'success'});
+ user = { pass: pass, level: req.body.level };
+ userService.store(user, function(err) {
+ if (err) return res.send(500, {status:"fail"});
+ res.send({status: 'success'});
+ });
});
app.del('/user', app.util.requiresLevel(50), function(req,res) {
delete users[req.query.email];
- fs.writeFileSync('data/users.json', JSON.stringify(users));
- res.send({status: 'success'});
+ userService.del(req.query.email, function(err) {
+ if (err) return res.send(500, {status:"fail"});
+ res.send({status: 'success'});
+ });
});
app.get('/user', app.util.requiresLevel(50), function(req,res) {
var userList = [];
- for ( var x in users ) {
- userList.push({email: x, level: users[x].level});
- }
- res.send(JSON.stringify(userList));
+ var users = userService.all(function(err, users) {
+ for ( var x in users ) {
+ userList.push({email: x, level: users[x].level});
+ }
+ res.send(JSON.stringify(userList));
+ });
});
};

0 comments on commit aa3c73e

Please sign in to comment.
Something went wrong with that request. Please try again.