-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleFileSystem.js
219 lines (197 loc) · 6.69 KB
/
SimpleFileSystem.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var SimpleFileSystem = function(size, type) {
//Make sure we're using the correct request call
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
//Store this in self so we can store data in object
var self = this;
//Request that the filesystem is created
navigator.webkitPersistentStorage.requestQuota(size, function(size) {
window.requestFileSystem(type, size, function(fileSystem) {
self.fileSystem = fileSystem;
//Create and dispatch custom event so we know the file system is ready
var event = new CustomEvent('FileSystemReady', {
detail: {
files: {
create: self.files.create,
write: self.files.write,
remove: self.files.remove,
read: self.files.read
},
self: self
}
});
document.dispatchEvent(event);
});
});
//Add event listener to document so we know when file system is ready
document.addEventListener('FileSystemReady', SimpleFileSystem.runStacks, false);
//Array to store list of files to be created after file system is ready
this.files = {
create: [],
write: [],
remove: [],
read: []
};
};
SimpleFileSystem.prototype.create = function(file) {
//Check to see if the file system has been created yet
if (typeof this.fileSystem == "undefined") {
//File system hasn't been created, store the file to be created later
SimpleFileSystem.push(this, "create", {
name: file,
cb: create
});
} else {
//Create file using String
create(this, {
data: {
name: file
}
});
}
function create(self, ret) {
//Create the file
self.fileSystem.root.getFile(ret.data.name, {create: true}, function(fileEntry) {});
}
};
SimpleFileSystem.prototype.write = function(fileName, data, position, create, callback) {
//Set default action of position to append
//Can't use shorthand for this due to FileSystem.OVERWRITE being 0
if (typeof position == "undefined") {
position = position || SimpleFileSystem.APPEND;
}
//Set default action of create to true
create = create || true;
if (typeof this.fileSystem == "undefined") {
//File system hasn't been created yet. Store the data to be saved later
SimpleFileSystem.push(this, "write", {
name: fileName,
data: data,
position: position,
create: create,
cb: write
});
} else {
//File system is ready. Write to the file
write(this, {
data: {
create: create,
data: data,
name: fileName,
position: position
}
});
}
function write(self, ret) {
//Get the file that we're about to write to
self.fileSystem.root.getFile(ret.data.name, {create: ret.data.create}, function(fileEntry) {
//Create the file writer
fileEntry.createWriter(function(writer) {
//Make sure the file writer is in the right position
if (ret.data.position == SimpleFileSystem.END) {
//Move the writer to the EOF
writer.seek(writer.length);
} else {
//Put the writer at the start of the file
writer.seek(0);
}
//Write the required data into the file
writer.write(new Blob([ret.data.data]));
//When the writer is done, destroy it
writer.onwriteend = function() {
writer.abort();
if (typeof callback != "undefined") {
callback();
}
}
});
});
}
};
SimpleFileSystem.prototype.remove = function(file, callback) {
//Check to see if the file system has been created yet
if (typeof this.fileSystem == "undefined") {
//File system hasn't been created, store the file to be created later
SimpleFileSystem.push(this, "remove", {
file: file,
callback: callback,
cb: remove
})
} else {
//Create file using String
remove(this, {
data: {
file: file,
callback: callback
}
});
}
function remove(self, ret) {
//Remove the file
self.fileSystem.root.getFile(ret.data.file, {create: true}, function(fileEntry) {
fileEntry.remove(function(e){
if (typeof callback != "undefined") {
callback();
}
});
});
}
};
SimpleFileSystem.prototype.read = function(file, callback) {
//Check to see if the file system has been created
if (typeof this.fileSystem == "undefined") {
//File system hasn't been created, store the file to be created later
SimpleFileSystem.push(this, "read", {
file: file,
callback: callback,
cb: read
});
} else {
read(this, {
data: {
file: file,
callback: callback
}
});
}
function read(self, ret) {
//Get the file to read
self.fileSystem.root.getFile(ret.data.file, {create: true}, function(fileEntry) {
fileEntry.file(function(file) {
//Create reader to read file
var reader = new FileReader();
//When done reading, call the callback, passing the text
reader.onloadend = function() {
ret.data.callback(this.result);
};
//Read the file
reader.readAsText(file);
});
});
}
};
SimpleFileSystem.PERSISTENT = window.PERSISTENT;
SimpleFileSystem.TEMPORARY = window.TEMPORARY;
SimpleFileSystem.BEGINNING = 0;
SimpleFileSystem.END = 1;
SimpleFileSystem.runStacks = function(data) {
var methods = [
"create",
"write",
"remove",
"read"
];
for (var i = 0; i < methods.length; i++) {
SimpleFileSystem.stack(data.detail.self, methods[i], data.detail.files[methods[i]]);
}
};
SimpleFileSystem.push = function(self, func, data) {
var store = {
data: data
};
self.files[func].push(store)
};
SimpleFileSystem.stack = function(self, func, data, cb) {
for (var i = 0; i < data.length; i++) {
data[i].data.cb(self, data[i]);
}
};