Skip to content

Commit

Permalink
Change some components to levelup from noDB
Browse files Browse the repository at this point in the history
Way too much code duplication in the added lib/* files. Also need to
move sessions away from noDB.
  • Loading branch information
davidbanham committed Apr 11, 2013
1 parent 548406d commit aa3c73e
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 134 deletions.
18 changes: 10 additions & 8 deletions app.js
Expand Up @@ -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']});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
};
Expand Down
26 changes: 26 additions & 0 deletions 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));
};
33 changes: 33 additions & 0 deletions 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);
})
}
};
33 changes: 33 additions & 0 deletions 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);
})
}
}
33 changes: 33 additions & 0 deletions 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);
})
}
}
34 changes: 34 additions & 0 deletions 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;
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
12 changes: 7 additions & 5 deletions 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});
Expand Down Expand Up @@ -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'});
});
});

};

0 comments on commit aa3c73e

Please sign in to comment.