(src, dst, opts)
| 201 | // - keepArchive: true to keep the archive afterwards (defaults to false) |
| 202 | // - keepStructure: true to keep the extracted structure unchanged (defaults to false) |
| 203 | function extract(src, dst, opts) { |
| 204 | var extractor; |
| 205 | var promise; |
| 206 | |
| 207 | opts = opts || {}; |
| 208 | extractor = getExtractor(src); |
| 209 | |
| 210 | // Try to get extractor from mime type |
| 211 | if (!extractor && opts.mimeType) { |
| 212 | extractor = getExtractor(opts.mimeType); |
| 213 | } |
| 214 | |
| 215 | // If extractor is null, then the archive type is unknown |
| 216 | if (!extractor) { |
| 217 | return Q.reject( |
| 218 | createError( |
| 219 | 'File ' + src + ' is not a known archive', |
| 220 | 'ENOTARCHIVE' |
| 221 | ) |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | // Extract to a temporary directory in case of file name clashes |
| 226 | return Q.nfcall(tmp.dir, { |
| 227 | template: dst + '-XXXXXX', |
| 228 | mode: 0777 & ~process.umask() |
| 229 | }) |
| 230 | .then(function(tempDir) { |
| 231 | // nfcall may return multiple callback arguments as an array |
| 232 | return Array.isArray(tempDir) ? tempDir[0] : tempDir; |
| 233 | }) |
| 234 | .then(function(tempDir) { |
| 235 | // Check archive file size |
| 236 | promise = Q.nfcall(fs.stat, src).then(function(stat) { |
| 237 | if (stat.size <= 8) { |
| 238 | throw createError( |
| 239 | 'File ' + src + ' is an invalid archive', |
| 240 | 'ENOTARCHIVE' |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | // Extract archive |
| 245 | return extractor(src, tempDir); |
| 246 | }); |
| 247 | |
| 248 | // Remove archive |
| 249 | if (!opts.keepArchive) { |
| 250 | promise = promise.then(function() { |
| 251 | return Q.nfcall(fs.unlink, src); |
| 252 | }); |
| 253 | } |
| 254 | |
| 255 | // Move contents from the temporary directory |
| 256 | // If the contents are a single directory (and we're not preserving structure), |
| 257 | // move its contents directly instead. |
| 258 | promise = promise |
| 259 | .then(function() { |
| 260 | return isSingleDir(tempDir); |
no test coverage detected
searching dependent graphs…