-
Notifications
You must be signed in to change notification settings - Fork 0
/
localstorage.js
150 lines (106 loc) · 2.83 KB
/
localstorage.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
/*
* rosi client plugin - storage database functions
*
* iota wallet and flash channel management
*
* wraps needed database functions to writeFile and readFile equivalents
*
* */
// IndexedDB
if (!self.indexedDB) {
self.alert("Your browser doesn't support a stable version of IndexedDB. The plugin does not work!");
}
var readData;
var db_available; // let undefined...
var db;
var request = self.indexedDB.open("flash_objects", 1);
request.onerror = function(event) {
// Do something with request.errorCode!
console.log("Database creation error!");
db_available = false;
};
request.onsuccess = function(event) {
// Do something with request.result!
db = event.target.result;
db_available = true;
};
request.onupgradeneeded = function(event) {
// Save the IDBDatabase interface
console.log("Database upgrade needed.");
var db = event.target.result;
// Create an objectStore for this database
var objectStore = db.createObjectStore("flash");
};
// Write flash object to database
// callback (err), err is new error when error has occurred, false if success
var writeFile = function(str_filename, str_data, callback)
{
if(typeof db_available == 'undefined')
{
setTimeout(()=>{ writeFile(str_filename, str_data, callback); }, 10);
}else if(db_available == false){
callback(new Error('Cannot write flashobject file to database!'));
}else{
try
{
// create Transaction
var transaction = db.transaction(["flash"], "readwrite");
var objectStore = transaction.objectStore('flash');
var req = objectStore.put(str_data, str_filename);
transaction.oncomplete = function(event) {
// Finished
callback(false);
};
transaction.onerror = function(event) {
callback(new Error('Error updating DB'));
};
}catch(e)
{
callback(e);
}
}
}
// Get flash object from database
// callback (err, data)
var readFile = function(str_filename, callback)
{
if(typeof db_available == 'undefined')
{
setTimeout(()=>{ readFile(str_filename, callback); }, 10);
}
else if(db_available == false)
{
callback(new Error('Cannot read flashobject file from database!'), false);
}else{
try
{
var transaction = db.transaction(["flash"]);
var objectStore = transaction.objectStore("flash");
var req = objectStore.get(str_filename);
req.onerror = function(event) {
callback(new Error('Error reading DB.'), false);
return;
};
req.onsuccess = function(event) {
if(typeof event.target.result != 'undefined')
{
callback(false, event.target.result.toString());
}else
{
// DB Entry not found, return File not found
callback({code:'ENOENT'}, false);
}
};
}catch(e)
{
callback(e, false);
}
}
}
if(typeof module != 'undefined')
{
module.exports = {
'writeFile' : writeFile,
'readFile' : readFile
}
}