split-file
Advanced tools
+1
-1
| { | ||
| "name": "split-file", | ||
| "version": "2.2.2", | ||
| "version": "2.3.0", | ||
| "private": false, | ||
@@ -5,0 +5,0 @@ "description": "Splitting and merging files with NodeJS", |
@@ -57,3 +57,3 @@ #!/usr/bin/env node | ||
| var parts = parseInt(process.argv[4]); | ||
| if (isNaN(parts)) { | ||
@@ -60,0 +60,0 @@ return this.help(); |
+145
-135
@@ -10,4 +10,5 @@ /*! | ||
| */ | ||
| var Promise = require('bluebird'); | ||
| var fs = require('fs'); | ||
| var Promise = require("bluebird"); | ||
| var fs = require("fs"); | ||
| const { basename, resolve } = require("path"); | ||
@@ -26,51 +27,51 @@ /** | ||
| */ | ||
| SplitFile.prototype.splitFile = function(file, parts) { | ||
| var self = this; | ||
| SplitFile.prototype.splitFile = function (file, parts, dest) { | ||
| var self = this; | ||
| // Validate parameters. | ||
| if (parts < 1) { | ||
| return Promise.reject(new Error("Parameter 'parts' is invalid, must contain an integer value.")); | ||
| // Validate parameters. | ||
| if (parts < 1) { | ||
| return Promise.reject(new Error("Parameter 'parts' is invalid, must contain an integer value.")); | ||
| } | ||
| return Promise.promisify(fs.stat)(file).then(function (stat) { | ||
| if (!stat.isFile) { | ||
| return Promise.reject(new Error("Given file is not valid")); | ||
| } | ||
| if (!stat.size) { | ||
| return Promise.reject(new Error("File is empty")); | ||
| } | ||
| return Promise.promisify(fs.stat)(file).then(function (stat) { | ||
| if (! stat.isFile) { | ||
| return Promise.reject(new Error("Given file is not valid")); | ||
| } | ||
| if (! stat.size) { | ||
| return Promise.reject(new Error("File is empty")); | ||
| } | ||
| var totalSize = stat.size; | ||
| var splitSize = Math.floor(totalSize / parts); | ||
| var totalSize = stat.size; | ||
| var splitSize = Math.floor(totalSize / parts); | ||
| // If size of the parts is 0 then you have more parts than bytes. | ||
| if (splitSize < 1) { | ||
| return Promise.reject(new Error("Too many parts, or file too small!")); | ||
| } | ||
| // If size of the parts is 0 then you have more parts than bytes. | ||
| if(splitSize < 1) { | ||
| return Promise.reject(new Error("Too many parts, or file too small!")); | ||
| } | ||
| // Get last split size, this is different from the others because it uses scrap value. | ||
| var lastSplitSize = splitSize + (totalSize % parts); | ||
| // Get last split size, this is different from the others because it uses scrap value. | ||
| var lastSplitSize = splitSize + totalSize % parts; | ||
| // Capture the partinfo in here: | ||
| var partInfo = []; | ||
| // Capture the partinfo in here: | ||
| var partInfo = []; | ||
| // Iterate the parts | ||
| for (var i = 0; i < parts; i++) { | ||
| partInfo[i] = { | ||
| number: i + 1, | ||
| // Iterate the parts | ||
| for (var i = 0; i < parts; i ++) { | ||
| partInfo[i] = { | ||
| number: i + 1, | ||
| // Set buffer read start position | ||
| start: i * splitSize, | ||
| // Set buffer read start position | ||
| start: i * splitSize, | ||
| // Set total ending position | ||
| end: i * splitSize + splitSize, | ||
| }; | ||
| // Set total ending position | ||
| end: (i * splitSize) + splitSize | ||
| }; | ||
| if (i === parts - 1) { | ||
| partInfo[i].end = i * splitSize + lastSplitSize; | ||
| } | ||
| } | ||
| if (i === (parts - 1)) { | ||
| partInfo[i].end = (i * splitSize) + lastSplitSize; | ||
| } | ||
| } | ||
| return self.__splitFile(file, partInfo); | ||
| }); | ||
| return self.__splitFile(file, partInfo, dest); | ||
| }); | ||
| }; | ||
@@ -84,45 +85,45 @@ | ||
| */ | ||
| SplitFile.prototype.splitFileBySize = function(file, maxSize) { | ||
| var self = this; | ||
| SplitFile.prototype.splitFileBySize = function (file, maxSize, dest) { | ||
| var self = this; | ||
| return Promise.promisify(fs.stat)(file).then(function (stat) { | ||
| if (! stat.isFile) { | ||
| return Promise.reject(new Error("Given file is not valid")); | ||
| } | ||
| if (! stat.size) { | ||
| return Promise.reject(new Error("File is empty")); | ||
| } | ||
| return Promise.promisify(fs.stat)(file).then(function (stat) { | ||
| if (!stat.isFile) { | ||
| return Promise.reject(new Error("Given file is not valid")); | ||
| } | ||
| if (!stat.size) { | ||
| return Promise.reject(new Error("File is empty")); | ||
| } | ||
| var totalSize = stat.size; | ||
| var totalSize = stat.size; | ||
| // Number of parts (exclusive last part!) | ||
| var parts = Math.ceil(totalSize / maxSize); | ||
| var splitSize = Math.round(maxSize); | ||
| // Number of parts (exclusive last part!) | ||
| var parts = Math.ceil(totalSize / maxSize); | ||
| var splitSize = Math.round(maxSize); | ||
| // If size of the parts is 0 then you have more parts than bytes. | ||
| if(splitSize < 1) { | ||
| return Promise.reject(new Error("Too many parts, or file too small!")); | ||
| } | ||
| // If size of the parts is 0 then you have more parts than bytes. | ||
| if (splitSize < 1) { | ||
| return Promise.reject(new Error("Too many parts, or file too small!")); | ||
| } | ||
| // Capture the partinfo in here: | ||
| var partInfo = []; | ||
| // Capture the partinfo in here: | ||
| var partInfo = []; | ||
| // Iterate the parts | ||
| for (var i = 0; i < parts; i ++) { | ||
| partInfo[i] = { | ||
| number: i + 1, | ||
| // Iterate the parts | ||
| for (var i = 0; i < parts; i++) { | ||
| partInfo[i] = { | ||
| number: i + 1, | ||
| // Set buffer read start position | ||
| start: i * splitSize, | ||
| // Set buffer read start position | ||
| start: i * splitSize, | ||
| // Set total ending position | ||
| end: (i * splitSize) + splitSize | ||
| }; | ||
| } | ||
| // Set total ending position | ||
| end: i * splitSize + splitSize, | ||
| }; | ||
| } | ||
| // recalculate the size of the last chunk | ||
| partInfo[partInfo.length - 1].end = totalSize; | ||
| // recalculate the size of the last chunk | ||
| partInfo[partInfo.length - 1].end = totalSize; | ||
| return self.__splitFile(file, partInfo); | ||
| }); | ||
| return self.__splitFile(file, partInfo, dest); | ||
| }); | ||
| }; | ||
@@ -137,23 +138,23 @@ | ||
| */ | ||
| SplitFile.prototype.mergeFiles = function(inputFiles, outputFile) { | ||
| // Validate parameters. | ||
| if (inputFiles.length <= 0) { | ||
| return Promise.reject(new Error("Make sure you input an array with files as first parameter!")); | ||
| } | ||
| SplitFile.prototype.mergeFiles = function (inputFiles, outputFile) { | ||
| // Validate parameters. | ||
| if (inputFiles.length <= 0) { | ||
| return Promise.reject(new Error("Make sure you input an array with files as first parameter!")); | ||
| } | ||
| var writer = fs.createWriteStream(outputFile, { | ||
| encoding: null | ||
| }); | ||
| var writer = fs.createWriteStream(outputFile, { | ||
| encoding: null, | ||
| }); | ||
| return Promise.mapSeries(inputFiles, function (file) { | ||
| return new Promise(function (resolve, reject) { | ||
| var reader = fs.createReadStream(file, { encoding: null }); | ||
| reader.pipe( writer, { end: false }); | ||
| reader.on('error', reject); | ||
| reader.on('end', resolve); | ||
| }); | ||
| }).then(function() { | ||
| writer.close(); | ||
| return Promise.resolve(outputFile); | ||
| return Promise.mapSeries(inputFiles, function (file) { | ||
| return new Promise(function (resolve, reject) { | ||
| var reader = fs.createReadStream(file, { encoding: null }); | ||
| reader.pipe(writer, { end: false }); | ||
| reader.on("error", reject); | ||
| reader.on("end", resolve); | ||
| }); | ||
| }).then(function () { | ||
| writer.close(); | ||
| return Promise.resolve(outputFile); | ||
| }); | ||
| }; | ||
@@ -169,51 +170,60 @@ | ||
| */ | ||
| SplitFile.prototype.__splitFile = function (file, partInfo) { | ||
| // Now the magic. Read buffers with length.. | ||
| var partFiles = []; | ||
| SplitFile.prototype.__splitFile = function (file, partInfo, dest) { | ||
| // Now the magic. Read buffers with length.. | ||
| var partFiles = []; | ||
| return Promise.mapSeries(partInfo, function (info) { | ||
| return new Promise(function (resolve, reject) { | ||
| // Open up a reader | ||
| var reader = fs.createReadStream(file, { | ||
| encoding: null, | ||
| start: info.start, | ||
| end: info.end - 1 | ||
| }); | ||
| return Promise.mapSeries(partInfo, function (info) { | ||
| return new Promise(function (resolve, reject) { | ||
| // Open up a reader | ||
| var reader = fs.createReadStream(file, { | ||
| encoding: null, | ||
| start: info.start, | ||
| end: info.end - 1, | ||
| }); | ||
| // Part name (file name of part) | ||
| // get the max number of digits to generate for part number | ||
| // ex. if original file is split into 4 files, then it will be 1 | ||
| // ex. if original file is split into 14 files, then it will be 2 | ||
| // etc. | ||
| var maxPaddingCount = String(partInfo.length).length; | ||
| // initial part number | ||
| // ex. '0', '00', '000', etc. | ||
| var currentPad = ''; | ||
| for (var i = 0; i < maxPaddingCount; i++) { | ||
| currentPad += '0'; | ||
| } | ||
| // construct part number for current file part | ||
| // <file>.sf-part01 | ||
| // ... | ||
| // <file>.sf-part14 | ||
| var unpaddedPartNumber = '' + info.number; | ||
| var partNumber = currentPad.substring(0, currentPad.length - unpaddedPartNumber.length) + unpaddedPartNumber; | ||
| var partName = file + '.sf-part' + partNumber; | ||
| // Part name (file name of part) | ||
| // get the max number of digits to generate for part number | ||
| // ex. if original file is split into 4 files, then it will be 1 | ||
| // ex. if original file is split into 14 files, then it will be 2 | ||
| // etc. | ||
| var maxPaddingCount = String(partInfo.length).length; | ||
| // initial part number | ||
| // ex. '0', '00', '000', etc. | ||
| var currentPad = ""; | ||
| for (var i = 0; i < maxPaddingCount; i++) { | ||
| currentPad += "0"; | ||
| } | ||
| // construct part number for current file part | ||
| // <file>.sf-part01 | ||
| // ... | ||
| // <file>.sf-part14 | ||
| var unpaddedPartNumber = "" + info.number; | ||
| var partNumber = currentPad.substring(0, currentPad.length - unpaddedPartNumber.length) + unpaddedPartNumber; | ||
| var partName = file + ".sf-part" + partNumber; | ||
| partFiles.push(partName); | ||
| const outputFile = (filename) => { | ||
| const writer = fs.createWriteStream(filename); | ||
| const pipe = reader.pipe(writer); | ||
| pipe.on("error", reject); | ||
| pipe.on("finish", resolve); | ||
| }; | ||
| // Open up writer | ||
| var writer = fs.createWriteStream(partName); | ||
| if (dest) { | ||
| const filename = basename(partName); | ||
| if (dest.charAt(dest.length - 1) !== "/") { | ||
| dest += "/"; | ||
| } | ||
| outputFile(dest + filename); | ||
| partFiles.push(dest + filename); | ||
| } else { | ||
| outputFile(partName); | ||
| partFiles.push(partName); | ||
| } | ||
| // Pipe reader to writer | ||
| }); | ||
| }).then(function () { | ||
| return Promise.resolve(partFiles); | ||
| }); | ||
| }; | ||
| // Pipe reader to writer | ||
| var pipe = reader.pipe(writer); | ||
| pipe.on('error', reject); | ||
| pipe.on('finish', resolve); | ||
| }); | ||
| }).then(function () { | ||
| return Promise.resolve(partFiles); | ||
| });; | ||
| } | ||
| module.exports = new SplitFile(); |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
287
4.36%13364
-1.68%