-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.js
65 lines (60 loc) · 1.94 KB
/
storage.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
var fs = require('fs'),
jsonfile = require('jsonfile');
var getFilename = function() {
try {
fs.accessSync('/data/');
return '/data/radio.json';
} catch (err) {
console.log('STORAGE: "/data/" is unavailable:', err);
return '/tmp/radio.json';
}
};
exports.read = function(cb) {
var filename = getFilename();
fs.stat(filename, function(err, stats) {
if (err) {
console.log('STORAGE: No previous data detected, creating new base object "' + filename + '".');
cb({});
} else if (stats.isFile()) {
console.log('Reading previous data "' + filename + '"...');
jsonfile.readFile(filename, function(err, data) {
if (err) {
console.log('STORAGE: JSON ERROR ', err)
} else {
cb(data);
}
});
} else {
console.log("STORAGE: Something's wrong, is '" + filename + "' a directory or something?");
}
});
};
exports.write = function(data) {
var filename = getFilename();
fs.stat(filename, function(err, stats) {
if (err) {
console.log('STORAGE: No previous data detected, creating new file"' + filename + '".');
fs.writeFile(filename, JSON.stringify(data), {}, function(err) {
if (err) {
console.log("Error writing file '" + filename + "':", err);
}
});
} else if (stats.isFile()) {
console.log('Previous file detected, atomically writing new data "' + filename + '".');
var tempFilename = filename + ".tmp";
fs.writeFile(tempFilename, JSON.stringify(data), {}, function(err) {
if (!err) {
fs.rename(tempFilename, filename, function(err) {
if (err) {
console.log("Error writing file '" + filename + "':", err);
}
});
} else {
console.log('File "' + filename + '"write error!');
}
});
} else {
console.log("Something's wrong, is '" + filename + "' a directory or something?");
}
});
};