-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.js
102 lines (86 loc) · 2.92 KB
/
worker.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
self.requestFileSystemSync = function (type, size, successCallback,
errorCallback) {
var serviceType = new ServiceType("http://webinos.org/api/file")
webinos.discovery.findServices(serviceType,
{ onFound : function (service) {
service.bindService({
onBind : function () {
service.requestFileSystem(type, size, function (filesystem) {
successCallback(filesystem)
}, errorCallback)
}
})
}
, onError : errorCallback
})
}
var paths = [];
var fs = null;
self.onmessage = function(e) {
var data = e.data;
if ('type' in data && 'size' in data) {
fs = requestFileSystemSync(data.type, data.size);
}
switch (data.cmd) {
case 'init':
var dirs = {
'/usr/local/bin/': ['helloworld.cpp'],
'/usr/local/': [
'a.txt', 'b.txt', 'c.txt', 'd.txt', 'e.txt', 'f.txt', 'g.txt', 'h.txt',
'i.txt', 'j.txt','k.txt','l.txt','m.txt','n.txt'],
'/pics/': ['one.png', 'two.jpg', 'three.png', 'four.jpg', 'five.png'],
'/var/www/': ['index.html'],
'/var/www/htdocs/css/': ['main.css'],
'/var/www/htdocs/js/': ['main.js'],
'/var/www/htdocs/': ['blank.html', 'index.html'],
'/Users/eric/cupcakes/': [
'chocolate.png', 'white.png', 'pink.png', 'mini.png'],
'Users/arne/TOP_SECRET/': ['html4_rocks.txt']
};
for (var dir in dirs) {
var folders = dir.split('/');
// Throw out './' or '/' if present on the beginning of our path.
if (folders[0] == '.' || folders[0] == '') {
folders = folders.slice(1);
}
createDir(fs.root, folders);
dirs[dir].forEach(function(file, i) {
var fileEntry = createFile(fs.root, dir + file);
});
};
createFile
self.postMessage({msg: 'Files loaded!'});
break;
case 'read':
paths = [];
getAllFileEntries(fs.root.createReader(), fs.root.name);
self.postMessage({entries: paths});
//self.close();
break;
default:
// noop
}
};
function getAllFileEntries(dirReader, name) {
var entries = dirReader.readEntries();
for (var i = 0, entry; entry = entries[i]; ++i) {
if (entry.isDirectory) {
//paths.push('root' + entry.fullPath + '/.');//.substring(1));
paths.push((entry.fullPath + '/.').substring(1));
getAllFileEntries(entry.createReader(), entry.name);
} else {
//paths.push('root' + entry.fullPath);//.substring(1));
paths.push(entry.fullPath.substring(1));
}
}
}
function createFile(rootDirEntry, path) {
return rootDirEntry.getFile(path, {create: true});
}
function createDir(rootDirEntry, folders) {
if (folders.length) {
var dirEntry = rootDirEntry.getDirectory(folders[0], {create: true});
// Recursively add the new subfolder if we still have a subfolder to create.
createDir(dirEntry, folders.slice(1));
}
}