-
Notifications
You must be signed in to change notification settings - Fork 9
/
vfs.js
479 lines (440 loc) · 17 KB
/
vfs.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
let VFS = function() {
this.FILE = Symbol("FILE");
this.DIRECTORY = Symbol("DIRECTORY");
let _root = { toString: function() { return "/"; }};
this.rootDirectory = function() {
return _root;
};
this.reset = function() {
_root = { toString: function() { return "/"; }};
}
this.fullPath = function(node) {
if (node == _root) { return "/" };
var n = node;
var path = "";
while (n.name) {
path = "/" + n.name + path;
n = n.parent;
}
return path;
};
this.getParentPath = function(path) {
var parts = this.tokenizePath(path);
var path = "";
if (parts.length < 2) {
if (parts.isFullpath) { return "/"; }
else { return ""; }
}
for (var i=0; i < parts.length-1; i++) {
path += "/" + parts[i];
}
return path;
};
this.getFileName = function(path) {
var parts = this.tokenizePath(path);
return parts[parts.length-1];
};
this.createFile = function(path, parent) {
if (parent == undefined) { parent = _root; }
let fsnode = { type: this.FILE, name: path, data: new ArrayBuffer(0), parent: parent, toString: function() { return this.name; }};
parent[path] = fsnode;
return fsnode;
};
this.createDirectory = function(path, parent) {
if (parent == undefined) { parent = _root; }
let fsnode = { type: this.DIRECTORY, name: path, parent: parent, toString: function() { return this.name; }};
parent[path] = fsnode;
return fsnode;
};
this.getChildren = function(parent, type) {
if (parent == undefined) { parent = _root; }
let nodes = [];
for (var e in parent) {
if (type == undefined || (parent[e] && parent[e].type == type)) {
if ((parent[e].type == this.FILE || parent[e].type == this.DIRECTORY) && e != "parent") {
nodes.push(parent[e]);
}
}
}
nodes.sort();
return nodes;
};
this.writeText = function(file, text) {
file.data = appendBuffer(file.data, this.textToData(text));
};
this.textToData = function(text) {
let chars = [];
for (let i=0; i < text.length; i++) {
chars.push(text.charCodeAt(i));
}
return new Uint8Array(chars).buffer;
};
this.readLine = function(file, offset) {
if (offset == undefined) { offset = 0; }
if (offset >= file.data.byteLength) {
throw new Error("Input past end of file");
}
let view = new Uint8Array(file.data);
let c = null;
let str = "";
while (offset < file.data.byteLength && c != "\n") {
c = String.fromCharCode(view[offset]);
if (c != "\n") {
str += c;
}
offset++;
}
return str;
};
this.readText = function(file) {
let offset = 0;
let view = new Uint8Array(file.data);
let c = null;
let str = "";
while (offset < file.data.byteLength) {
c = String.fromCharCode(view[offset]);
str += c
offset++;
}
return str;
};
this.writeData = function(file, data, offset) {
if (offset == undefined) { offset = 0; }
let start = file.data.slice(0, offset);
let end = file.data.slice(offset + data.byteLength, file.data.byteLength);
file.data = start;
if (start.byteLength < offset) {
file.data = appendBuffer(file.data, new ArrayBuffer(offset - start.byteLength));
}
file.data = appendBuffer(file.data, data);
file.data = appendBuffer(file.data, end);
};
this.readData = function(file, offset, length) {
return file.data.slice(offset, offset + length);
};
this.getNode = function(path, parent) {
let parts = this.tokenizePath(path);
if (parts.isRoot) {
return _root;
}
if (parts.isFullpath) {
parent = _root;
}
let node = null;
for (let i=0; i < parts.length; i++) {
if (parts[i] == ".") {
// move along, nothing to see here
}
else if (parts[i] == "..") {
if (node.parent == undefined) {
node = _root;
}
else {
node = node.parent;
parent = node;
}
}
else {
node = parent[parts[i]];
parent = node;
if (node == undefined) {
return null;
}
}
}
return node;
};
this.getDataURL = async function(file) {
let blob = null;
let type = getTypeFromName(file.name);
if (type) {
blob = new Blob([file.data], { type: type });
}
else {
blob = new Blob([file.data]);
}
let dataUrl = await new Promise(r => {let a=new FileReader(); a.onload=r; a.readAsDataURL(blob)}).then(e => e.target.result);
return dataUrl;
};
this.dataURLToBlob = function(dataURL) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
var byteString = atob(dataURL.split(',')[1]);
// separate out the mime component
var mimeString = dataURL.split(',')[0].split(':')[1].split(';')[0]
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
// create a view into the buffer
var ia = new Uint8Array(ab);
// set the bytes of the buffer to the correct values
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
var blob = new Blob([ab], {type: mimeString});
return blob;
};
function getTypeFromName(filename) {
var parts = filename.split(".");
if (parts.length < 2) { return null; }
var ext = parts.pop();
return types[ext];
}
this.renameNode = function(node, newName) {
// TODO: move the file if the newName includes a path
var parent = node.parent;
parent[node.name] = undefined;
node.name = newName;
parent[node.name] = node;
};
this.removeFile = function(file, parent) {
if (typeof file == "string") {
file = this.getNode(file, parent);
}
if (file && file.type == this.FILE) {
let parent = file.parent;
delete parent[file.name];
}
else {
throw new Error("File not found");
}
};
this.removeDirectory = function(directory, parent) {
if (typeof directory == "string") {
directory = this.getNode(directory, parent);
}
if (directory && directory.type == this.DIRECTORY) {
var childNodes = this.getChildren(directory);
if (childNodes.length == 0) {
let parent = directory.parent;
delete parent[directory.name];
}
else {
// TODO: probably throw an exception
}
}
else {
// TODO: probably throw an exception
}
};
this.tokenizePath = function(path) {
path = path.replaceAll("\\","/");
let parts = path.split("/");
parts.isFullpath = false;
if (path.indexOf("/") == 0) {
parts.isFullpath = true;
}
if (parts[0].match(/[A-Z|a-z]:/)) {
parts.shift();
parts.isFullpath = true;
}
if (parts[0] == "") {
while (parts.length > 0 && parts[0] == "") {
parts.shift();
}
}
if (parts.isFullpath && parts.length == 0) {
parts.isRoot = true;
}
else {
parts.isRoot = false;
}
return parts;
};
function appendBuffer(buffer1, buffer2) {
let tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
}
const types = {
// File Extension MIME Type
'abs': 'audio/x-mpeg',
'ai': 'application/postscript',
'aif': 'audio/x-aiff',
'aifc': 'audio/x-aiff',
'aiff': 'audio/x-aiff',
'aim': 'application/x-aim',
'art': 'image/x-jg',
'asf': 'video/x-ms-asf',
'asx': 'video/x-ms-asf',
'au': 'audio/basic',
'avi': 'video/x-msvideo',
'avx': 'video/x-rad-screenplay',
'bcpio': 'application/x-bcpio',
'bin': 'application/octet-stream',
'bmp': 'image/bmp',
'body': 'text/html',
'cdf': 'application/x-cdf',
'cer': 'application/pkix-cert',
'class': 'application/java',
'cpio': 'application/x-cpio',
'csh': 'application/x-csh',
'css': 'text/css',
'dib': 'image/bmp',
'doc': 'application/msword',
'dtd': 'application/xml-dtd',
'dv': 'video/x-dv',
'dvi': 'application/x-dvi',
'eot': 'application/vnd.ms-fontobject',
'eps': 'application/postscript',
'etx': 'text/x-setext',
'exe': 'application/octet-stream',
'gif': 'image/gif',
'gtar': 'application/x-gtar',
'gz': 'application/x-gzip',
'hdf': 'application/x-hdf',
'hqx': 'application/mac-binhex40',
'htc': 'text/x-component',
'htm': 'text/html',
'html': 'text/html',
'ief': 'image/ief',
'jad': 'text/vnd.sun.j2me.app-descriptor',
'jar': 'application/java-archive',
'java': 'text/x-java-source',
'jnlp': 'application/x-java-jnlp-file',
'jpe': 'image/jpeg',
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'js': 'application/javascript',
'jsf': 'text/plain',
'json': 'application/json',
'jspf': 'text/plain',
'kar': 'audio/midi',
'latex': 'application/x-latex',
'm3u': 'audio/x-mpegurl',
'mac': 'image/x-macpaint',
'man': 'text/troff',
'mathml': 'application/mathml+xml',
'me': 'text/troff',
'mid': 'audio/midi',
'midi': 'audio/midi',
'mif': 'application/x-mif',
'mov': 'video/quicktime',
'movie': 'video/x-sgi-movie',
'mp1': 'audio/mpeg',
'mp2': 'audio/mpeg',
'mp3': 'audio/mpeg',
'mp4': 'video/mp4',
'mpa': 'audio/mpeg',
'mpe': 'video/mpeg',
'mpeg': 'video/mpeg',
'mpega': 'audio/x-mpeg',
'mpg': 'video/mpeg',
'mpv2': 'video/mpeg2',
'ms': 'application/x-wais-source',
'nc': 'application/x-netcdf',
'oda': 'application/oda',
'odb': 'application/vnd.oasis.opendocument.database',
'odc': 'application/vnd.oasis.opendocument.chart',
'odf': 'application/vnd.oasis.opendocument.formula',
'odg': 'application/vnd.oasis.opendocument.graphics',
'odi': 'application/vnd.oasis.opendocument.image',
'odm': 'application/vnd.oasis.opendocument.text-master',
'odp': 'application/vnd.oasis.opendocument.presentation',
'ods': 'application/vnd.oasis.opendocument.spreadsheet',
'odt': 'application/vnd.oasis.opendocument.text',
'otg': 'application/vnd.oasis.opendocument.graphics-template',
'oth': 'application/vnd.oasis.opendocument.text-web',
'otp': 'application/vnd.oasis.opendocument.presentation-template',
'ots': 'application/vnd.oasis.opendocument.spreadsheet-template',
'ott': 'application/vnd.oasis.opendocument.text-template',
'ogx': 'application/ogg',
'ogv': 'video/ogg',
'oga': 'audio/ogg',
'ogg': 'audio/ogg',
'otf': 'application/x-font-opentype',
'spx': 'audio/ogg',
'flac': 'audio/flac',
'anx': 'application/annodex',
'axa': 'audio/annodex',
'axv': 'video/annodex',
'xspf': 'application/xspf+xml',
'pbm': 'image/x-portable-bitmap',
'pct': 'image/pict',
'pdf': 'application/pdf',
'pgm': 'image/x-portable-graymap',
'pic': 'image/pict',
'pict': 'image/pict',
'pls': 'audio/x-scpls',
'png': 'image/png',
'pnm': 'image/x-portable-anymap',
'pnt': 'image/x-macpaint',
'ppm': 'image/x-portable-pixmap',
'ppt': 'application/vnd.ms-powerpoint',
'pps': 'application/vnd.ms-powerpoint',
'ps': 'application/postscript',
'psd': 'image/vnd.adobe.photoshop',
'qt': 'video/quicktime',
'qti': 'image/x-quicktime',
'qtif': 'image/x-quicktime',
'ras': 'image/x-cmu-raster',
'rdf': 'application/rdf+xml',
'rgb': 'image/x-rgb',
'rm': 'application/vnd.rn-realmedia',
'roff': 'text/troff',
'rtf': 'application/rtf',
'rtx': 'text/richtext',
'sfnt': 'application/font-sfnt',
'sh': 'application/x-sh',
'shar': 'application/x-shar',
'sit': 'application/x-stuffit',
'snd': 'audio/basic',
'src': 'application/x-wais-source',
'sv4cpio': 'application/x-sv4cpio',
'sv4crc': 'application/x-sv4crc',
'svg': 'image/svg+xml',
'svgz': 'image/svg+xml',
'swf': 'application/x-shockwave-flash',
't': 'text/troff',
'tar': 'application/x-tar',
'tcl': 'application/x-tcl',
'tex': 'application/x-tex',
'texi': 'application/x-texinfo',
'texinfo': 'application/x-texinfo',
'tif': 'image/tiff',
'tiff': 'image/tiff',
'tr': 'text/troff',
'tsv': 'text/tab-separated-values',
'ttf': 'application/x-font-ttf',
'txt': 'text/plain',
'ulw': 'audio/basic',
'ustar': 'application/x-ustar',
'vxml': 'application/voicexml+xml',
'xbm': 'image/x-xbitmap',
'xht': 'application/xhtml+xml',
'xhtml': 'application/xhtml+xml',
'xls': 'application/vnd.ms-excel',
'xml': 'application/xml',
'xpm': 'image/x-xpixmap',
'xsl': 'application/xml',
'xslt': 'application/xslt+xml',
'xul': 'application/vnd.mozilla.xul+xml',
'xwd': 'image/x-xwindowdump',
'vsd': 'application/vnd.visio',
'wav': 'audio/x-wav',
'wbmp': 'image/vnd.wap.wbmp',
'wml': 'text/vnd.wap.wml',
'wmlc': 'application/vnd.wap.wmlc',
'wmls': 'text/vnd.wap.wmlsc',
'wmlscriptc': 'application/vnd.wap.wmlscriptc',
'wmv': 'video/x-ms-wmv',
'woff': 'application/font-woff',
'woff2': 'application/font-woff2',
'wrl': 'model/vrml',
'wspolicy': 'application/wspolicy+xml',
'z': 'application/x-compress',
'zip': 'application/zip'
};
}
if (!ArrayBuffer.prototype.slice) {
ArrayBuffer.prototype.slice = function (start, end) {
let that = new Uint8Array(this);
if (end == undefined) end = that.length;
let result = new ArrayBuffer(end - start);
let resultArray = new Uint8Array(result);
for (let i = 0; i < resultArray.length; i++)
resultArray[i] = that[i + start];
return result;
}
}