Skip to content
Browse files

Finished owner db and created convenience files for initializing the …

…database
  • Loading branch information...
1 parent 67f5f0c commit e0692330ecd93f1d17d00a052c860e4394364f56 @beatgammit committed Mar 3, 2011
Showing with 83 additions and 29 deletions.
  1. +21 −2 README.md
  2. +2 −2 client/runner.js
  3. +21 −0 initDb.js
  4. +5 −0 regUsers.sh
  5. +30 −21 server/lib/dbaccess.js
  6. +2 −2 server/lib/import.js
  7. +2 −2 server/server.js
View
23 README.md
@@ -42,6 +42,24 @@ The server communicates changes through open websockets with clients and serves
* Responds to watched file data requests (all files watched)
+Supported commands:
+
+* /register- Register a user using either POST or GET request
+ * Formatted url query or JSON
+ * Must have 'user' and 'pass' field w/ optional data field
+* /file- Upload one or more files using formidable on a POST request
+ * 'fields' must have 2 properties, 'statsHeader' and 'stats'
+ * 'statsHeader' has all of the fields in stats in the same order
+ * 'stats' has all of the data for each FileStat object
+* /check- Check to see if a file or files are already uploaded
+ * POST request only
+ * Must be formatted JSON array of one or more FileStat objects (must have tmd5)
+ * Returns an array of FileStat objects with a new 'exists' property and possibly an 'err' property
+* /meta- Search for Files of a certain type
+ * format- /meta/mime/type where mime/type is any mimetype in the database
+ * mimetype group search will be supported in the future
+
+
State of the project:
=====================
@@ -54,16 +72,17 @@ Finished:
* Listening to filesystem events given a parent directory usind inotify
* Hashing on server and client
* Directory structure on server
+* Server database backend to track uploaded files
+* Client database to track file synchronization with server
To Do:
------
* File downloads
* Websocket connection between client and server
* Priority queue on client and basic server support for priority
-* Server database backend to track uploaded files
-* Client database to track file synchronization with server
* Settings manager to add or remove directories to synchronize
+* Expand searching for documents
Ideas for expansion:
--------------------
View
4 client/runner.js
@@ -13,8 +13,8 @@
batch,
options = {
start: './tests',
- host: '192.168.1.29',
- //host: 'localhost',
+ //host: '192.168.1.101',
+ host: 'localhost',
port: 8022,
user: 'coolaj86',
password: 'cr4zym0nk3y'
View
21 initDb.js
@@ -0,0 +1,21 @@
+var couchdb = require('couchdb-tmp'),
+ client = couchdb.createClient(5984, 'www.beatgammit.com', "filesync", "Wh1t3Ch3dd3r"),
+ db = client.db('filesync'),
+ tmd5Proto = function(doc){
+ emit(doc.tmd5, doc);
+ };
+
+function initViews(){
+ var tDesign = {};
+ tDesign.views = {};
+ tDesign.views.tmd5 = {};
+ tDesign.views.tmd5.map = tmd5Proto.toString();
+
+ db.saveDesign('basic', tDesign);
+}
+
+db.exists(function(exists){
+ if(!exists){
+ db.create(initViews);
+ }
+});
View
5 regUsers.sh
@@ -0,0 +1,5 @@
+curl 'http://beatgammit:crazyawes0me@localhost:8022/register?name=coolaj86&pass=cr4zym0nk3y'
+
+curl 'http://beatgammit:crazyawes0me@localhost:8022/register?name=beatgammit&pass=crazyawes0me'
+
+curl 'http://beatgammit:crazyawes0me@localhost:8022/register?name=mvndaai&pass=lefthanded'
View
51 server/lib/dbaccess.js
@@ -8,7 +8,7 @@
auth: {username: "filesync", password: "Wh1t3Ch3dd3r"}
}),
filesyncdb = client.database('filesync'),
- viewPrototype = function(doc){if(doc.value.type === '{0}'){emit(doc.mtime, doc);}}.toString(),
+ viewPrototype = function(doc){if(doc.type === '{0}'){emit(doc.mtime, doc);}}.toString(),
viewRegex = /\{0\}/;
function saveToDb(fileStat, username){
@@ -22,14 +22,14 @@
tFileDoc = doc;
}
- if(tFileDoc.indexOf(username) < 0){
- tFileDoc.push(username);
+ if(tFileDoc.owners.indexOf(username) < 0){
+ tFileDoc.owners.push(username);
}
filesyncdb.save(tFileDoc.md5, tFileDoc);
});
- userDb = client.db(username);
+ userDb = client.database(username);
userDb.get(fileStat.qmd5, function(err, doc){
if(err){
// remove the functions from fileStat
@@ -38,9 +38,13 @@
});
}
- function getByMimeType(mimeType, callback){
+ function getByMimeType(username, mimeType, callback){
+ console.log("User: " + username);
+ console.log("Mime: " + mimeType);
+ var userDb = client.database(username);
+
mimeType = mimeType.replace('/', '%2F');
- filesyncdb.view('type/' + mimeType, function(error, response){
+ userDb.view('type/' + mimeType, function(error, response){
console.log(response);
var docArray = [];
if(error){
@@ -49,8 +53,6 @@
return;
}
- console.log(JSON.stringify(response));
-
response['rows'].forEach(function(tDoc){
console.log(JSON.stringify(tDoc.value));
docArray.push(tDoc.value);
@@ -60,11 +62,12 @@
});
}
- function createViews(data){
- console.log("Create Views");
- console.log(JSON.stringify(data));
+ function createViews(username, data){
+ var userDb;
+ console.log("Create Views: " + username);
if(data && data.length){
- filesyncdb.get('_design/type', function(error, doc){
+ userDb = client.database(username);
+ userDb.get('_design/type', function(error, doc){
var tDesign = {};
if(!error){
tDesign = doc;
@@ -84,15 +87,14 @@
}
});
- console.log(tDesign);
- filesyncdb.save('_design/type', tDesign.views);
+ userDb.save('_design/type', tDesign.views);
});
}
}
function registerUser(name, pass, userData, callback){
var userDB;
- userDB = client.db('_users');
+ userDB = client.database('_users');
userDB.get('org.couchdb.user:' + name, function(err, doc){
var userDoc, newDb;
@@ -109,36 +111,43 @@
userDoc.salt = crypto.createHash('sha1').update(new Date()).digest('hex');
userDoc.password_sha = crypto.createHash('sha1').update(pass + userDoc.salt).digest('hex');
- userDB.saveDoc('org.couchdb.user:' + name, userDoc);
+ userDB.save('org.couchdb.user:' + name, userDoc);
// create a new db for this user
- newDb = client.db(name);
+ newDb = client.database(name);
newDb.create();
callback();
});
}
function fileExists(filestat, filedata, username, callback){
- filesyncdb.view('tmd5/' + 'tmd5', {startKey: filestat.tmd5, limit: 1},
+ filesyncdb.view('basic/' + 'tmd5', {startKey: filestat.tmd5, limit: 1},
function(err, response){
console.log("Filestat:");
console.log(filestat);
var tDb;
if(err || response.total_rows == 0){
- callback({exists: false, err: err});
+ filestat.exists = false;
+ filestat.err = err;
+ callback(filestat);
return;
}
tDb = client.database(username);
tDb.get(filedata.qmd5, function(error, doc){
if(err){
tDb.save(tDb._id, filedata);
- callback({exists: true, err: err});
+
+ filestat.exists = true;
+ filestat.err = err;
+
+ callback(filestat);
return;
}
- callback({exists: true});
+ filestat.exists = true;
+ callback(filestat);
return;
});
});
View
4 server/lib/import.js
@@ -139,7 +139,7 @@
fields.stats = JSON.parse(fields.stats);
fileStats = addKeysToFileStats(fields.statsHeader, fields.stats);
- dbaccess.createViews(fileStats);
+ dbaccess.createViews(req.remoteUser, fileStats);
res.writeHead(200, {'Content-Type': 'application/json'});
@@ -167,7 +167,7 @@
if (qmd5Error) {
return finishReq(qmd5Error);
}
- importFile(fileStat, files[qmd5], finishReq);
+ importFile(fileStat, files[qmd5], req.remoteUser, finishReq);
});
}
View
4 server/server.js
@@ -66,7 +66,7 @@ var require;
if(req.params.field && req.params.value){
mimeType = req.params.field + "/" + req.params.value;
- dbaccess.getByMimeType(mimeType, function(err, docArray){
+ dbaccess.getByMimeType(req.remoteUser, mimeType, function(err, docArray){
console.log(JSON.stringify(docArray));
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(docArray));
@@ -78,10 +78,10 @@ var require;
}
function handleRegister(req, res){
- console.log(req.url);
var qData;
if(req.method == "GET"){
qData = qs.parse(url.parse(req.url).query);
+ console.log(qData);
db.registerUser(qData.name, qData.pass, qData.data, function(cess){
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(cess));

0 comments on commit e069233

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