Mercurial > psgxs
comparison backends/directory.js @ 27:a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 01 Nov 2010 02:41:04 +0100 |
parents | |
children | 7cfcd7d5796c |
comparison
equal
deleted
inserted
replaced
26:f2e9365bc8ec | 27:a36a514e8be8 |
---|---|
1 /* | |
2 * Copyright (C) 2010 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
3 * | |
4 * This file is part of PSĜS, a PubSub server written in JavaScript. | |
5 * | |
6 * PSĜS is free software: you can redistribute it and/or modify | |
7 * it under the terms of the GNU Affero General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License. | |
10 * | |
11 * PSĜS is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU Affero General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Affero General Public License | |
17 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>. | |
18 */ | |
19 | |
20 var fs = require('fs'); | |
21 var backend = exports; | |
22 | |
23 backend.save = function(dir) { | |
24 if (!dir) | |
25 dir = 'storage'; | |
26 | |
27 fs.stat(dir, function(err, stats) { | |
28 if (err) | |
29 fs.mkdir(dir, 0755); | |
30 else if (!stats.isDirectory()) { | |
31 console.log('\033[1;41mThe file “' + dir + '” isn’t a directory. Exiting.\033[0m'); | |
32 process.exit(1); | |
33 } | |
34 | |
35 fs.readdir(dir, function(err, files) { | |
36 function sanitize(o) { | |
37 var n = {}; | |
38 for (var i in o) { | |
39 if (i == 'content' || o[i].setISO8601) | |
40 n[i] = o[i].toString(); | |
41 else if (o[i] instanceof Array) | |
42 n[i] = o[i]; | |
43 else if (typeof o[i] == 'object') | |
44 n[i] = sanitize(o[i]); | |
45 else if (typeof o[i] == 'function') | |
46 ; | |
47 else | |
48 n[i] = o[i]; | |
49 } | |
50 return n; | |
51 } | |
52 var data = sanitize(backend.list); | |
53 | |
54 var inspect = require('sys').inspect; | |
55 | |
56 var toDelete = []; | |
57 | |
58 for (var i in files) { | |
59 var nodeID = files[i]; | |
60 if (nodeID in data) { | |
61 fs.writeFile(dir+'/'+nodeID+'.json', inspect(data[nodeID], false, null)); | |
62 delete data[nodeID]; | |
63 } else | |
64 toDelete.push(nodeID); | |
65 } | |
66 | |
67 for (var nodeID in data) { | |
68 inspect(nodeID); | |
69 inspect(data[nodeID]); | |
70 fs.writeFile(dir+'/'+nodeID+'.json', inspect(data[nodeID], false, null)); | |
71 } | |
72 | |
73 for (var nodeID in toDelete) { | |
74 fs.unlink(toDelete[nodeID]); | |
75 } | |
76 }); | |
77 }); | |
78 } | |
79 | |
80 backend.load = function(dir) { | |
81 if (!dir) | |
82 dir = 'storage'; | |
83 | |
84 fs.stat(dir, function(err, stats) { | |
85 if (err) | |
86 fs.mkdir(dir, 0755); | |
87 else if (!stats.isDirectory()) { | |
88 console.log('\033[1;41mThe file “' + dir + '” isn’t a directory. Exiting.\033[0m'); | |
89 process.exit(1); | |
90 } | |
91 | |
92 fs.readdir(dir, function(err, files) { | |
93 var xmpp = require('xmpp'); | |
94 function parseStanza(path, content) { | |
95 var stanza = null; | |
96 var stream = new xmpp.Stream({ | |
97 stanza: function (stanza) { | |
98 path[content] = stanza; | |
99 } | |
100 }); | |
101 stream.opened = true; | |
102 stream.data(path[content]); | |
103 } | |
104 | |
105 function endParsing(o) { | |
106 var regexp = /\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ/; | |
107 for (var i in o) { | |
108 if (typeof o[i] == 'string' && i == 'content') | |
109 parseStanza(o, i); | |
110 else if (typeof o[i] == 'string' && regexp(o[i])) { | |
111 var today = new Date(); | |
112 today.setISO8601(o[i]); | |
113 o[i] = today; | |
114 } else if (typeof o[i] == 'object') | |
115 endParsing(o[i]); | |
116 } | |
117 return o; | |
118 } | |
119 | |
120 backend.list = {}; | |
121 | |
122 for (var i in files) { | |
123 var file = files[i]; | |
124 var nodeID = file.replace(/\.json$/, ''); | |
125 | |
126 var data = fs.readFileSync(dir+'/'+file); | |
127 | |
128 var obj = eval('('+data+')'); | |
129 backend.list[nodeID] = endParsing(obj); | |
130 | |
131 for (var j in Node.prototype) | |
132 backend.list[nodeID][j] = Node.prototype[j]; | |
133 } | |
134 }); | |
135 }); | |
136 } |