-
Notifications
You must be signed in to change notification settings - Fork 4
/
filestore.js
50 lines (45 loc) · 1 KB
/
filestore.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
function FileStore() {
this.files = null;
}
FileStore.prototype = {
init: function(callback) {
if (this.files) {
callback();
return;
}
var self = this;
var files = new IDBStore({
dbName: 'filestoredb',
dbDescription: 'DB used for storing files',
dbVersion: '1.0',
storeName: 'filestore',
keyPath: 'id',
autoIncrement: true,
onStoreReady: function(){
self.files = files;
callback();
}
});
},
getAll: function(callback) {
var self = this;
this.init(function() { self.files.getAll(callback); });
},
get: function(id, callback) {
var self = this;
this.init(function() { self.files.get(id, callback); });
},
put: function(filename, buffer, callback, id) {
var self = this;
function doPut() {
var file = {
name: filename,
contents: buffer
};
if (id != null)
file.id = id;
self.files.put(file, callback);
}
this.init(doPut);
}
};