-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
135 lines (107 loc) · 3.08 KB
/
db.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
var database = (function() {
return function() {
var self = this;
var version = 1;
var request;
var db = undefined;
self.init = function(success, error) {
request = window.indexedDB.open("hushFiles", version);
request.onblocked = function(e) {
if(error) error();
}
request.onupgradeneeded = function(e) {
var db = request.result;
db.createObjectStore("chunks", {keyPath: "filename"})
}
request.onsuccess = function(e){
db = request.result;
if(success) success();
}
request.onerror = function(e) {
if(error) error();
}
}
self.saveChunk = function(filename, chunknumber, contents, success, error) {
var trans = db.transaction(["chunks"], "readwrite");
var store = trans.objectStore("chunks");
var request = store.put({
"filename": filename + chunknumber,
"chunknumber": chunknumber,
"contents": contents
});
request.onsuccess = function(e) {
if(success) success(e);
}
request.onerror = function(e) {
console.log(e);
error;
}
}
self.append = function(fileHandle, contents, success, error) {
lockedFile = fileHandle.open('readwrite');
console.log("Appending");
request = lockedFile.append(contents);
request.onsuccess = function(e) {
console.log("Appended" + contents);
lockedFile.flush();
if(success) success();
}
request.onerror = function(e) {
console.log("verdamt!");
if(error) error(e);
}
}
self.readChunk = function(filename, chunknumber, success, errror) {
var store = db.transaction(["chunks"], "readwrite").objectStore("chunks");
var request = store.get(filename + chunknumber);
request.onsuccess = function(e) {
var data = request.result;
if(success) success(data);
}
request.onerror = function(e) {
if(error) error(e);
}
}
// Returns a file to the concatenated file
self.getFile = function(filename, chunks, success, error) {
console.log("Getting entire file");
console.log(db.mozCreateFileHandle);
var fileRequest = db.mozCreateFileHandle(filename,'text/plain','trololo.txt');
var readAndAppend = function(fh, chunknumber) {
self.readChunk(filename, chunknumber, function(data) {
console.log("Chunk read, now appending");
var fileReader = new FileReader();
fileReader.onload = function(e) {
self.append(fh, fileReader.result, function(){
if(++chunknumber < chunks) {
console.log("Appended and continuing");
readAndAppend(fh, chunknumber);
} else {
request = fh.getFile();
request.onsuccess = function(e) {
if(success) success(request.result);
}
request.onerror = function(e) {
if(error) error(e);
}
}
},
function(e){
if(error) error(e);
});
}
fileReader.readAsArrayBuffer(data.contents);
},
function(e) {
if(error) error(e);
});
}
fileRequest.onsuccess = function(e) {
readAndAppend(fileRequest.result, 0);
}
fileRequest.onerror = function(e) {
if(error) error(e);
}
}
}
})();