Created
December 3, 2015 09:46
-
-
Save RobertWHurst/77412085ba8b7b9bf323 to your computer and use it in GitHub Desktop.
Recover files by extension from a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var path = require('path'); | |
var mkdirp = require('mkdirp'); | |
var rimraf = require('rimraf'); | |
var through = require('through2'); | |
var SOURCE_PATH = '/media/roberthurst/CE543276543260FF'; | |
var DEST_PATH = '/media/roberthurst/478FC40638130E7A/recovered-files'; | |
var MANIFEST_FILE = '/media/roberthurst/478FC40638130E7A/recovered-files.txt'; | |
var DOCUMENT_EXT = ['doc', 'docx', 'wpd', 'wps', 'csv', 'pps', 'ppt', 'xlr', 'xls', 'xlsx', 'indd', 'pdf', 'accdb']; | |
var IMAGE_EXT = ['bmp', 'gif', 'jpg', 'png', 'psd', 'tif']; | |
var AUDIO_EXT = ['aif', 'iff', 'm3u', 'm4a', 'mid', 'mp3', 'mpa', 'ra', 'wav', 'wma', 'ai', 'drw', 'eps', 'ps', 'svg']; | |
var VIDEO_EXT = ['3g2', '3gp', 'asf', 'asx', 'avi', 'flv', 'mov', 'mp4', 'mpg', 'rm', 'swf', 'vob', 'wmv']; | |
var SPECIAL_EXT = []; | |
// gather the extensions whitelist | |
var extensions = [].concat(DOCUMENT_EXT, IMAGE_EXT, AUDIO_EXT, VIDEO_EXT, SPECIAL_EXT); | |
extensions = extensions.filter(function(extension) { | |
return extensions.indexOf(extension) === extensions.lastIndexOf(extension); | |
}); | |
// create the write stream | |
var pathStream = through(); | |
var newLineStream = through(function(chunk, _, cb) { cb(null, chunk + '\n'); }); | |
var writeStream = fs.createWriteStream(MANIFEST_FILE); | |
// link the streams | |
pathStream.pipe(newLineStream).pipe(writeStream); | |
// create the working path pointers | |
var workingPathIndexPointers = []; | |
var workingPathIndexes = []; | |
// load the target path to kick things off | |
fs.readdir(SOURCE_PATH, function(err, index) { | |
if (err) { throw err; } | |
// add the base path data to the working path data | |
workingPathIndexPointers.unshift(0); | |
workingPathIndexes.unshift(index.map(function(filename) { | |
return path.join(SOURCE_PATH, filename); | |
})); | |
// clear the destination path | |
rimraf(path.join(DEST_PATH, '*'), function(err) { | |
if (err) { throw err; } | |
// begin searching the target | |
(function rec() { | |
var workingPathIndexPointer = workingPathIndexPointers[0]; | |
var workingPathIndex = workingPathIndexes[0]; | |
var workingPath = workingPathIndex[workingPathIndexPointer]; | |
// advance the pointer | |
if (workingPathIndexPointer + 1 < workingPathIndex.length) { | |
workingPathIndexPointers[0] += 1; | |
} else { | |
workingPathIndexPointers.shift(); | |
workingPathIndexes.shift(); | |
if (workingPathIndexes.length === 0) { | |
return writeStream.end(); | |
} | |
return rec(); | |
} | |
// check the file type | |
fs.lstat(workingPath, function(err, stat) { | |
if (err) { throw err; } | |
// if a file then push it into the stream | |
if (stat.isFile() && extensions.indexOf(path.extname(workingPath).slice(1)) !== -1) { | |
var filePath = workingPath.slice(SOURCE_PATH.length); | |
var writePath = path.join(DEST_PATH, filePath); | |
return mkdirp(path.dirname(writePath), function(err) { | |
if (err) { throw err; } | |
var readStream = fs.createReadStream(workingPath); | |
var writeStream = fs.createWriteStream(path.join(DEST_PATH, filePath)); | |
pathStream.write(filePath); | |
readStream.pipe(writeStream).on('finish', function() { | |
console.log(filePath); | |
rec(); | |
}); | |
}); | |
} | |
// if a directory then dive into it | |
if (stat.isDirectory()) { | |
return fs.readdir(workingPath, function(err, index) { | |
if (err) { throw err; } | |
workingPathIndexPointers.unshift(0); | |
workingPathIndexes.unshift(index.map(function(filename) { | |
return path.join(workingPath, filename); | |
})); | |
rec(); | |
}); | |
} | |
rec(); | |
}); | |
})(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment