forked from filipmares/-DEPRECATED-Automagical-WYSIWYG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
146 lines (120 loc) · 4.23 KB
/
data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//Redis connection stuff
var redisNode = require('redis'),
//redis = redisNode.createClient(),
parseUrl = require('url').parse;
var db_uri = parseUrl(process.env['DUOSTACK_DB_REDIS']),
db_pwd = db_uri.auth.split(':')[1],
redis = redisNode.createClient(db_uri.port, db_uri.hostname);
redis.auth(db_pwd, function () {
console.log('Redis authenticated');
});
redis.on('error', function(err){
console.log('Error on data layer ' + err);
});
//Data Model
//user == the user's password
//user:numPages == num Pages User Has
//user:page:#num == retrieve Page Defined By #Num
//user:page:#num:processed:html == only the html (dom) elements of the page
//user:page:#num:processed:css == only the css styles associated with the page
//user:page:#num:dirty == set if page was externally fetched
//Saves a page to redis. If no page number is given, a new page will be saved and the
//number of pages incremented. Otherwise the page with the specified number will be
//overwritten.
exports.savePage = function(user, pageData, pageNum, dirty){
//Overwriting a page, or save a new page?
if (pageNum){
redis.set(user + ":page:" + pageNum, pageData);
} else {
redis.get(user + ":numPages", function(err, reply){
if (err || (reply === null)){
console.log('ERROR: while saving page');
return;
}
var pageIndex = parseInt(reply.toString());
redis.set(user + ":page:" + pageIndex, pageData);
redis.incr(user + ":numPages");
//Set dirty bit for externally fetched pages
if (dirty){
redis.set(user + ":page:" + pageIndex + ":dirty", "true");
}
});
}
};
//Returns a previously saved page
exports.getPage = function(user, pageNum, callback){
redis.get(user + ":page:" + pageNum, function(err, reply){
if (err || !reply) {
return callback(new Error("Could not retrieve page"));
}
callback(null, reply.toString());
});
};
exports.addUser = function(username, password){
redis.set(username, password);
redis.set(username + ":numPages", 0)
return makeUser(username, password);
};
exports.getUser = function(userid, callback){
redis.get(userid, function(err, reply){
if (err || !reply) return callback(undefined);
var user = makeUser(userid, reply.toString());
callback(user);
});
};
exports.getPageList = function(user, callback){
redis.get(user.username + ":numPages", function(err, reply){
if (err) return callback(err);
var numPages = parseInt(reply.toString());
var pages = makePages(user, numPages);
callback(null, pages);
})
};
exports.saveProcessedPage = function(user, pageHTML, pageCSS, pageNum){
if (pageNum){
redis.set(user + ":page:" + pageNum + ":processed:html", pageHTML);
redis.set(user + ":page:" + pageNum + ":processed:css", pageCSS);
} else{
redis.get(user + ":numPages", function(err, reply){
if (err || (reply === null)){
console.log('ERROR: while saving processed page');
return;
}
var pageIndex = parseInt(reply.toString());
redis.set(user + ":page:" + pageIndex + ":processed:html", pageHTML);
redis.set(user + ":page:" + pageIndex + ":processed:css", pageCSS);
});
}
};
exports.getProcessedPage = function(user, page, callback){
redis.get(user + ":page:" + page + ":processed:html", function(err, reply){
if (err || !reply) return callback(err);
var html = reply.toString();
redis.get(user + ":page:" + page + ":processed:css", function(err2, reply2){
if (err || !reply) return callback(err);
var css = reply2.toString();
var processed = {canvas: html, style: css};
callback(null, processed);
});
});
};
exports.getDirtyBit = function(user, page, callback){
redis.get(user + ":page:" + page + ":dirty", function(err, reply){
if (err || !reply) return callback(false);
callback(true);
});
};
//Convenience methods
function makeUser(username, password){
var user = {'username' : username, 'password' : password};
return user;
};
function makePages(user, numPages){
var pages = [];
for (var i = 0; i<numPages; i++){
var pageUrl = '<a target="_blank" href="/user/' + user.username + '/' + i + '.html">Page' + i + '</a>';
var editUrl = '<a href="/client/?user=' + user.username + '&page=' + i + '"><i>edit</i></a>';
pages.push({url : pageUrl, edit: editUrl});
}
return pages;
};