forked from abaltra/node-unpacker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
inflator.js
209 lines (198 loc) · 7.64 KB
/
inflator.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
var fs = require('fs');
var uuid = require('node-uuid');
var tarball = require('tarball-extract');
var async = require('async');
var Q = require('q');
var zlib = require('zlib');
var p7zip = require('node-7z');
var unrar = require('unrar');
function extract7Zip (path, outpath, create_random_path, q) {
fs.exists(outpath, function (exists) {
if (exists) {
outpath = create_random_path ? outpath + uuid.v4() + '/' : outpath;
var expander = new p7zip();
expander.test(path, {p: ''})
.then(function () {
expander.extractFull(path, outpath)
.then(function () {
q.resolve(outpath);
})
.catch(function (e) {
q.reject(e);
});
})
.catch(function (e) {
if (!e) e = new Error('Unknown error.');
q.reject(e);
});
} else {
q.reject('Output path doesn\'t exist');
}
});
};
function extractTarGZ (path, outpath, create_random_path, q) {
fs.exists(outpath, function (exists) {
if (exists) {
var uid = uuid.v4();
outpath = create_random_path ? outpath + uid + '/' : outpath;
if (create_random_path) {
fs.mkdirSync(outpath);
}
try {
tarball.extractTarball(path, outpath, function (err) {
if (err) return q.reject(err);
return q.resolve(outpath);
});
} catch (err) {
q.reject(err);
}
} else {
q.reject('Output path doesn\'t exist');
}
});
};
function extractGZ (path, outpath, create_random_path, q) {
var pieces = path.split('/');
var out = pieces[pieces.length - 1].slice(0, pieces[pieces.length - 1].length - 3); // Single files with regular extension prior to .gz
fs.exists(outpath, function (exists) {
if (exists) {
var uid = uuid.v4();
outpath = create_random_path ? outpath + uid + '/' : outpath;
if (create_random_path) {
fs.mkdirSync(outpath);
}
fs.createReadStream(path)
.pipe(zlib.createGunzip()
.on('error', function (e) {
q.reject(e);
}))
.pipe(fs.createWriteStream(outpath + out)
.on('finish', function (e) {
q.resolve(outpath);
})
.on('error', function (e) {
q.reject(e);
}));
} else {
q.reject('Output path doesn\'t exist');
}
});
};
function extractRar (path, outpath, create_random_path, q) {
fs.exists(outpath, function (exists) {
if (exists) {
create_random_path = create_random_path ? true : false;
var uid = uuid.v4();
outpath = create_random_path? outpath + uid + '/' : outpath;
var saved_routes = {};
var dirs = [];
var files = [];
var archive = null;
try {
archive = new unrar({
path: path,
arguments: ['-pNONE'],
failOnPasswords: true
});
if (create_random_path) {
fs.mkdirSync(outpath);
}
archive.list(function (err, entries) {
if (err) {
return q.reject(err);
}
entries.forEach(function (entry) {
if (entry.type === 'Directory') {
var path_pieces = entry.name.split('/');
var saved_pieces = path_pieces[0];
saved_routes[saved_pieces] = true;
dirs.push(saved_pieces);
for (var i = 1; i < path_pieces.length; i++) {
saved_pieces += ('/' + path_pieces[i]);
if (saved_routes[saved_pieces]) continue;
else {
saved_routes[saved_pieces] = true;
dirs.push(saved_pieces);
}
}
} else {
files.push(entry);
}
});
dirs.sort(function (a, b) {
return a.split('/').length - b.split('/').length;
});
async.eachSeries(dirs, function (direntry, cb) {
fs.mkdir(outpath + direntry, function (err) {
cb(err);
});
}, function (err) {
if (err) {
return q.reject(err);
}
async.each(files, function (fileentry, cb) {
var stream = archive.stream(fileentry.name);
try {
stream.pipe(fs.createWriteStream(outpath + fileentry.name))
cb();
} catch (err) {
cb(err);
}
}, function (err) {
if (err) {
q.reject(err);
} else {
q.resolve(outpath);
}
});
});
});
} catch (err) {
return q.reject(err);
}
} else {
q.reject('Output path doesn\'t exist');
}
});
};
exports.unpackFile = function (path, outpath, create_random_path) {
var deferred = Q.defer();
if (!outpath) {
deferred.reject('No output path given');
} else {
if (outpath[outpath.length - 1 ] !== '/') outpath += '/';
async.series([
function (cb) {
fs.exists(path, function (exists) {
if (exists) return cb();
else return cb('Input file not found');
});
},
function (cb) {
fs.access(outpath, fs.R_OK | fs.W_OK, function (err) {
if (err) return cb('Cannot write in output folder');
return cb();
});
},
function (cb) {
if( /(tar|tar\.gz|tgz)$/i.test(path)) {
extractTarGZ(path, outpath, create_random_path, deferred);
} else if (/(gz)$/i.test(path)) {
extractGZ(path, outpath, create_random_path, deferred);
} else if (/(rar)$/i.test(path)) {
extractRar(path, outpath, create_random_path, deferred);
} else if (/(zip|7z|zipx)$/i.test(path)) {
extract7Zip(path, outpath, create_random_path, deferred);
} else {
return cb('File type not supported')
}
return cb();
}
], function (err, results) {
if (err) {
deferred.reject(err);
}
});
}
return deferred.promise;
};