-
Notifications
You must be signed in to change notification settings - Fork 2
/
filesystem.js
195 lines (185 loc) · 6.76 KB
/
filesystem.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
// This file is a copy from the prototype written in the summer of 2014
// by markdittmer.
// TODO(ebeach): This needs to be majorily refactored to meet Google3 style.
// TODO: We should get a file system DAO for this.
// TODO: We need to clear the directory first, then write files.
// TODO: We need better error handling here.
// TODO: We currently don't provide the user with any feedback when an export
// completes.
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === 'export' && request.sources) {
var cfs = chrome.fileSystem;
var sources = request.sources;
// Let user pick a directory.
cfs.chooseEntry(
{
type: 'openDirectory',
suggestedName: 'chrome-app',
accepts: [{
description: 'Select a folder for Chrome app export'
}]
},
function(sources, dirEntry, fileEntries) {
console.log('Directory loaded');
if (chrome.runtime.lastError) {
sendResponse({ status: 'failed' });
}
// TODO: Is this step necessary?
// Get a writable handle for chosen directory.
cfs.getWritableEntry(dirEntry, function(sources, writableEntry) {
console.log('Writable directory loaded');
if (chrome.runtime.lastError) {
sendResponse({ status: 'failed' });
}
// First, load/create all necessary sub-directories.
loadDirs.call(
this,
sources,
writableEntry,
// Then, load/create all files.
loadFiles.bind(
this,
sources,
function() {
// Entire writing of folder is complete.
console.log('Creating files complete');
},
function() {
console.log('Error creating files');
}
),
function() { console.log('Error creating dirs'); }
);
}.bind(this, sources));
}.bind(this, sources)
);
sendResponse({ status: 'started' });
} else {
sendResponse({ status: 'unknown message' });
}
});
// Load directories needed by path names that are keys of |sources|.
function loadDirs(sources, rootDirEntry, callback, err_callback) {
var dirs = {__dirEntry__: rootDirEntry};
var dirRequests = 0;
var dirResponses = 0;
var dirDoneRequesting = false;
for (var fileName in sources) {
var path = fileName.split('/');
var dirPath = path.splice(0, path.length - 1);
var curDir = dirs;
dirPath.forEach(function(dirName) {
if (!curDir[dirName]) {
curDir[dirName] = {};
}
curDir = curDir[dirName];
});
}
var data = {
requests: 0,
responses: 0,
requestsSent: false,
dirs: dirs
};
loadDirsBF.call(this, data, dirs, callback, err_callback);
data.requestsSent = true;
}
// Given the directory structure we intend to load, is it loaded yet?
function dirsAreLoaded(dirs) {
if (!dirs.__dirEntry__) return false;
for (var dirName in dirs) {
if (dirName === '__dirEntry__') continue;
if (!dirsAreLoaded.call(this, dirs[dirName])) return false;
}
return true;
}
// Load directories in a breadth-first pattern.
function loadDirsBF(data, dirs, callback, err_callback) {
// console.assert(dirs.__dirEntry__);
for (var dirName in dirs) {
if (dirName === '__dirEntry__') continue;
++data.requests;
dirs.__dirEntry__.getDirectory(
dirName,
{ create: true },
function(dirName, dirs, newDirEntry) {
++data.responses;
dirs[dirName].__dirEntry__ = newDirEntry;
if (dirsAreLoaded.call(this, data.dirs)) {
callback(data.dirs);
}
loadDirsBF.call(this, data, dirs[dirName], callback, err_callback);
}.bind(this, dirName, dirs),
err_callback
);
}
}
function loadFiles(sources, callback, err_callback, dirs) {
var numSources = 0;
for (var key in sources) ++numSources;
var sourceWriteCount = 0;
var sourceFailCount = 0;
var onWriteError = function(e) {
console.log('Write error');
++sourceFailCount;
if (sourceWriteCount + sourceFailCount >= numSources)
console.log('Done writing files');
}.bind(this);
var onWriteCompleted = function(e) {
console.log('Write completed');
++sourceWriteCount;
if (sourceWriteCount + sourceFailCount >= numSources) {
console.log('Done writing files');
callback.call(this);
}
}.bind(this);
for (var path in sources) {
var pathParts = path.split('/');
var dirPath = pathParts.splice(0, pathParts.length - 1);
var fileName = pathParts[pathParts.length - 1];
var curDir = dirs;
dirPath.forEach(function(dirName) {
curDir = curDir[dirName];
}.bind(this));
var dirEntry = curDir.__dirEntry__;
dirEntry.getFile(
fileName,
{ create: true },
function(fileName, source, dirEntry, fileEntry) {
console.log('Got file ', fileName);
fileEntry.createWriter(
function(fileName, source, dirEntry, fileTruncateWriter) {
console.log('got file truncate writer', fileName);
fileTruncateWriter.onwriteend = function() {
dirEntry.getFile(
fileName,
{create: true},
function(fileName, source, fileEntry) {
fileEntry.createWriter(
function(fileName, source, fileWriter) {
console.log('Got file writer ', fileName);
fileWriter.onwriteend = onWriteCompleted;
fileWriter.onerror = onWriteError;
var blob;
if (fileName.substr(-3).toLowerCase() == 'png') {
console.log('intercept png');
var arrayBuffer = new Uint8Array(source).buffer;
blob = new Blob([arrayBuffer],
{type: 'image/png'});
} else {
blob = new Blob([source], {type: 'text/plain'});
}
fileWriter.write(blob);
}.bind(this, fileName, source))
}.bind(this, fileName, source));
};
fileTruncateWriter.onerror = onWriteError;
fileTruncateWriter.truncate(0);
}.bind(this, fileName, dirEntry, source),
onWriteError
);
}.bind(this, fileName, dirEntry, sources[path]),
onWriteError
);
}
}