forked from mozilla/brackets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchiveUtils.js
More file actions
363 lines (309 loc) · 12.5 KB
/
ArchiveUtils.js
File metadata and controls
363 lines (309 loc) · 12.5 KB
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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, Blob, Worker */
define(function (require, exports, module) {
"use strict";
var CommandManager = require("command/CommandManager");
var Commands = require("command/Commands");
var async = require("filesystem/impls/filer/lib/async");
var StartupState = require("bramble/StartupState");
var JSZip = require("thirdparty/jszip/dist/jszip.min");
var FileSystemCache = require("filesystem/impls/filer/FileSystemCache");
var Filer = require("filesystem/impls/filer/BracketsFiler");
var saveAs = require("thirdparty/FileSaver");
var Buffer = Filer.Buffer;
var Path = Filer.Path;
var fs = Filer.fs();
var Dialogs = require("widgets/Dialogs");
var DefaultDialogs = require("widgets/DefaultDialogs");
var Strings = require("strings");
var StringUtils = require("utils/StringUtils");
// These are const variables
var CANCEL_OPERATION = -1,
OVERWRITE_OPERATION = 1,
KEEP_EXISTING_OPERATION = 2;
// Mac and Windows clutter zip files with extra files/folders we don't need
function skipFile(filename) {
var basename = Path.basename(filename);
// Skip OS X additions we don't care about in the browser fs
if(/^__MACOSX\//.test(filename)) {
// http://superuser.com/questions/104500/what-is-macosx-folder
return true;
}
if(basename === ".DS_Store") {
// https://en.wikipedia.org/wiki/.DS_Store
return true;
}
if(/^\._/.test(basename)) {
// http://apple.stackexchange.com/questions/14980/why-are-dot-underscore-files-created-and-how-can-i-avoid-them
return true;
}
// Skip Windows additions we don't care about in the browser fs
if(/(Tt)humbs\.db/.test(basename)) {
// https://en.wikipedia.org/wiki/Windows_thumbnail_cache
return true;
}
if(basename === "desktop.ini") {
// http://www.computerhope.com/issues/ch001060.htm
return true;
}
// Include this file, don't skip.
return false;
}
function _refreshFilesystem(callback) {
// Update the file tree to show the new files
CommandManager.execute(Commands.FILE_REFRESH).always(function() {
// Generate Blob URLs for all the files we imported
FileSystemCache.refresh(callback);
});
}
function showOverwriteWarning(path, callback) {
var filepath = stripRoot(path);
Dialogs.showModalDialog(
DefaultDialogs.DIALOG_ID_INFO,
Strings.FILE_EXISTS_HEADER,
StringUtils.format(Strings.DND_FILE_REPLACE, filepath),
[
{
className : Dialogs.DIALOG_BTN_CLASS_NORMAL,
id : Dialogs.DIALOG_BTN_CANCEL,
text : Strings.CANCEL
},
{
className : Dialogs.DIALOG_BTN_CLASS_NORMAL,
id : Dialogs.DIALOG_BTN_IMPORT,
text : Strings.USE_IMPORTED
},
{
className : Dialogs.DIALOG_BTN_CLASS_PRIMARY,
id : Dialogs.DIALOG_BTN_OK,
text : Strings.KEEP_EXISTING
}
]
).getPromise().then(function (id) {
var result;
if (id === Dialogs.DIALOG_BTN_IMPORT) {
result = OVERWRITE_OPERATION;
} else if (id === Dialogs.DIALOG_BTN_OK) {
result = KEEP_EXISTING_OPERATION;
} else if (id === Dialogs.DIALOG_BTN_CANCEL) {
result = CANCEL_OPERATION;
}
callback(null, result);
}, callback);
}
function stripRoot(path) {
var root = StartupState.project("root");
var rootRegex = new RegExp("^" + root + "\/?");
return path.replace(rootRegex, "");
}
// zipfile can be a path (string) to a zipfile, or raw binary data.
function unzip(zipfile, options, callback) {
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
if(!zipfile) {
callback(new Error("missing zipfile argument"));
return;
}
var root = options.root || StartupState.project("root");
var destination = Path.resolve(options.destination || root);
function _unzip(data){
// TODO: it would be great to move this bit to a worker.
var archive = new JSZip(data);
var filenames = [];
archive.filter(function(relPath, file) {
if(skipFile(file.name)) {
return;
}
var isDir = file.options.dir;
filenames.push({
absPath: Path.join(destination, file.name),
isDirectory: isDir,
data: isDir ? null : new Buffer(file.asArrayBuffer())
});
});
function decompress(path, callback) {
var basedir = Path.dirname(path.absPath);
if(path.isDirectory) {
fs.mkdirp(path.absPath, callback);
} else {
// XXX: some zip files don't seem to be structured such that dirs
// get created before files. Create base dir if not there yet.
fs.mkdirp(basedir, function (err) {
if (err) {
return callback(err);
}
fs.stat(path.absPath, function(err, stats) {
if(err && err.code !== "ENOENT") {
return callback(err);
}
if (stats.type === "FILE") {
showOverwriteWarning(path.absPath, function (err, result) {
if (err) {
return callback(err);
}
if (result === OVERWRITE_OPERATION) {
fs.writeFile(path.absPath, path.data, callback);
} else if (result === KEEP_EXISTING_OPERATION) {
callback();
} else if (result === CANCEL_OPERATION) {
callback(new Error("Operation Cancelled"));
}
});
}
});
});
}
}
async.eachSeries(filenames, decompress, function(err) {
if(err) {
return callback(err);
}
_refreshFilesystem(callback);
});
}
if(typeof zipfile === "string") {
fs.readFile(Path.resolve(root, zipfile), function(err, data) {
if(err) {
return callback(err);
}
_unzip(data);
});
} else {
// zipfile is raw zip data, process it directly
_unzip(zipfile);
}
}
// Zip the entire project, starting at the project root.
function archive(callback) {
var root = StartupState.project("root");
var rootRegex = new RegExp("^" + root + "\/?");
// TODO: we should try to move this to a worker
var jszip = new JSZip();
function toRelProjectPath(path) {
// Make path relative within the zip, rooted in a `project/` dir
return path.replace(rootRegex, "project/");
}
function addFile(path, callback) {
fs.readFile(path, {encoding: null}, function(err, data) {
if(err) {
return callback(err);
}
jszip.file(toRelProjectPath(path), data.buffer, {binary: true});
callback();
});
}
function addDir(path, callback) {
fs.readdir(path, function(err, list) {
// Add the directory itself
jszip.folder(toRelProjectPath(path));
// Add all children of this dir, too
async.eachSeries(list, function(entry, callback) {
add(Path.join(path, entry), callback);
}, callback);
});
}
function add(path, callback) {
path = Path.resolve(root, path);
fs.stat(path, function(err, stats) {
if(err) {
return callback(err);
}
if(stats.type === "DIRECTORY") {
addDir(path, callback);
} else {
addFile(path, callback);
}
});
}
add(root, function(err) {
if(err) {
return callback(err);
}
var compressed = jszip.generate({type: 'arraybuffer'});
var blob = new Blob([compressed], {type: "application/zip"});
saveAs(blob, "project.zip");
callback();
});
}
function untar(tarArchive, options, callback) {
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
var untarWorker = new Worker("thirdparty/bitjs/bitjs-untar.min.js");
var root = options.root || StartupState.project("root");
var pending = null;
function extract(path, data, callback) {
path = Path.resolve(root, path);
var basedir = Path.dirname(path);
if(skipFile(path)) {
return callback();
}
fs.mkdirp(basedir, function(err) {
if(err) {
return callback(err);
}
fs.stat(path, function (err, stats) {
if (err && err.code !== "ENOENT") {
return callback(err);
}
if (stats.type !== "FILE") {
return callback();
}
showOverwriteWarning(path, function (err, result) {
if (err) {
return callback(err);
}
if (result === OVERWRITE_OPERATION) {
fs.writeFile(path, new Buffer(data), {encoding: null}, callback);
} else if (result === KEEP_EXISTING_OPERATION) {
return callback();
} else if (result === CANCEL_OPERATION) {
callback(new Error("Operation Cancelled"));
}
});
});
});
}
function finish(err) {
untarWorker.terminate();
untarWorker = null;
callback(err);
}
function writeCallback(err) {
if(err) {
if (err.message === "Operation Cancelled") {
finish(err);
return;
}
console.error("[Bramble untar] couldn't extract file", err);
}
pending--;
if(pending === 0) {
_refreshFilesystem(finish);
}
}
untarWorker.addEventListener("message", function(e) {
var data = e.data;
if(data.type === "progress" && pending === null) {
// Set the total number of files we need to deal with so we know when we're done
pending = data.totalFilesInArchive;
} else if(data.type === "extract") {
extract(data.unarchivedFile.filename, data.unarchivedFile.fileData, writeCallback);
} else if(data.type === "error") {
finish(new Error("[Bramble untar]: " + data.msg));
}
});
untarWorker.postMessage({file: tarArchive.buffer});
}
exports.skipFile = skipFile;
exports.archive = archive;
exports.unzip = unzip;
exports.untar = untar;
});