tiff-to-png
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -6,6 +6,8 @@ var childProcess = require('child_process'), | ||
/** | ||
* Options: | ||
* Options: | ||
* page: 'A4', 'A3' | ||
* type: 'png', 'jpg' | ||
* logLevel: 1 = info, 0, error (default) | ||
* tmpPath: '/path/to/tmp' (optional), | ||
* autoRemoveTmp: true, false (default) | ||
*/ | ||
@@ -23,2 +25,6 @@ | ||
if (options) { | ||
_this.options.autoRemoveTmp = options.autoRemoveTmp || false; | ||
} | ||
/** | ||
@@ -38,2 +44,5 @@ * Callback to show the amount converted. | ||
/** | ||
* Creates the directory the PNG's will sit | ||
*/ | ||
TiffConverter.prototype.createDir = function(target, filename, cb){ | ||
@@ -78,4 +87,12 @@ fs.exists(target, function(exists) { | ||
childProcess.exec('convert ' + original + ' -scene 1 ' + target + '/' + prefix + '%d' + suffix + '.' + type, function(err, stdout, stderr){ | ||
var command = 'convert'; | ||
if (_this.options.tmpPath) { | ||
command += ' -define registry:temporary-path=' + _this.options.tmpPath; | ||
} | ||
command += ' ' + original + ' -scene 1 ' + target + '/' + prefix + '%d' + suffix + '.' + type; | ||
childProcess.exec(command, function(err, stdout, stderr){ | ||
if(err){ | ||
@@ -113,2 +130,15 @@ logger.tabbed('Conversion failed', false); | ||
// Process complete, if there is a tmpPath set, remove any files | ||
if (_this.options.tmpPath && _this.options.autoRemoveTmp) { | ||
fs.readdir(_this.options.tmpPath, function(err, files) { | ||
if (err) logger.error(err); | ||
files.forEach(function(file) { | ||
var pattern = new RegExp('magick-'); | ||
if (pattern.test(file)) { | ||
fs.unlink(_this.options.tmpPath + '/' + file); | ||
} | ||
}); | ||
}); | ||
} | ||
return _this.complete(_this.errors, _this.converted, _this.total); | ||
@@ -115,0 +145,0 @@ } |
{ | ||
"name": "tiff-to-png", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A batch converter for multipage tiff files to png (or any imagemagick supported type).", | ||
@@ -5,0 +5,0 @@ "main": "./lib/convert.js", |
@@ -49,11 +49,15 @@ # TIFF to PNG | ||
prefix: 'page', | ||
suffix: '_foo' | ||
suffix: '_foo', | ||
tmpPath: '/path/to/tmp', | ||
autoRemoveTmp: true | ||
} | ||
``` | ||
| Option | Default | Description | | ||
|----------|---------|-----------------------------------------------------------------------------------------------------------------------------| | ||
| type | 'png' | The file type of the converted files | | ||
| logLevel | 0 | The level of the logs required. 0: Errors only, 1: Information | | ||
| prefix | 'page' | The string that will be prepended to the file names of the pages converted. E.g. 'page': page1.png | | ||
| suffix | '' | The string that will be appended onto the end of the file names of the page converted. E.g. '_invoices': page1_invoices.png | | ||
| Option | Default | Description | | ||
|---------------|---------|-----------------------------------------------------------------------------------------------------------------------------| | ||
| type | 'png' | The file type of the converted files | | ||
| logLevel | 0 | The level of the logs required. 0: Errors only, 1: Information | | ||
| prefix | 'page' | The string that will be prepended to the file names of the pages converted. E.g. 'page': page1.png | | ||
| suffix | '' | The string that will be appended onto the end of the file names of the page converted. E.g. '_invoices': page1_invoices.png | | ||
| tmpPath | null | Overwrites the Imagemagick default tmp directory path | | ||
| autoRemoveTmp | false | Automatically removes all files from tmpPath prefixed with magick-*, this happens on process completion | | ||
@@ -60,0 +64,0 @@ ## Callbacks |
@@ -418,4 +418,112 @@ var ConvertTiff = require('../lib/convert'), | ||
it('Should pass the temporary path when tmpPath option is set', function(done){ | ||
var createDirStub = sinon.stub(ConvertTiff.prototype, 'createDir', function(target, filename, cb){ | ||
cb(); | ||
}); | ||
var execStub = sinon.stub(childProcess, 'exec', function(command, cb){ | ||
command.should.contain('/path/to/tmp'); | ||
cb(); | ||
}); | ||
var convertSpy = sinon.spy(ConvertTiff.prototype, 'convert'); | ||
var converter = new ConvertTiff({ tmpPath: '/path/to/tmp' }); | ||
converter.tiffs = ['/test/foo.tif', '/test/foo.tif']; | ||
converter.total = 2; | ||
converter.location = './public'; | ||
converter.complete = function(errors, total){ | ||
createDirStub.restore(); | ||
execStub.restore(); | ||
convertSpy.callCount.should.equal(2); | ||
convertSpy.restore(); | ||
done(); | ||
} | ||
converter.convert(); | ||
}); | ||
it('Should attempt to clear all files named magick-* from the tmpPath', function(done){ | ||
var createDirStub = sinon.stub(ConvertTiff.prototype, 'createDir', function(target, filename, cb){ | ||
cb(); | ||
}); | ||
var execStub = sinon.stub(childProcess, 'exec', function(command, cb){ | ||
cb(); | ||
}); | ||
var readDirStub = sinon.stub(fs, 'readdir', function(path, cb) { | ||
cb(null, ['./magick-bla.ext']); | ||
}); | ||
var unlinkStub = sinon.stub(fs, 'unlink', function(path) { | ||
path.should.contain('magick-bla.ext'); | ||
}); | ||
var convertSpy = sinon.spy(ConvertTiff.prototype, 'convert'); | ||
var converter = new ConvertTiff({ tmpPath: '/path/to/tmp', autoRemoveTmp: true }); | ||
converter.tiffs = ['/test/foo.tif', '/test/foo.tif']; | ||
converter.total = 2; | ||
converter.location = './public'; | ||
converter.complete = function(errors, total){ | ||
createDirStub.restore(); | ||
execStub.restore(); | ||
readDirStub.restore(); | ||
unlinkStub.restore(); | ||
convertSpy.callCount.should.equal(2); | ||
convertSpy.restore(); | ||
done(); | ||
} | ||
converter.convert(); | ||
}); | ||
it('Should log an error when an error occurs checking the tmp directory', function(done){ | ||
var createDirStub = sinon.stub(ConvertTiff.prototype, 'createDir', function(target, filename, cb){ | ||
cb(); | ||
}); | ||
var execStub = sinon.stub(childProcess, 'exec', function(command, cb){ | ||
cb(); | ||
}); | ||
var error = new Error('Test Error'); | ||
var readDirStub = sinon.stub(fs, 'readdir', function(path, cb) { | ||
cb(error, []); | ||
}); | ||
var loggerStub = sinon.stub(logger, 'error', function(message){ | ||
createDirStub.restore(); | ||
execStub.restore(); | ||
loggerStub.restore(); | ||
readDirStub.restore(); | ||
message.should.equal(error); | ||
done(); | ||
}); | ||
var convertSpy = sinon.spy(ConvertTiff.prototype, 'convert'); | ||
var converter = new ConvertTiff({ tmpPath: '/path/to/tmp', autoRemoveTmp: true }); | ||
converter.tiffs = ['/test/foo.tif', '/test/foo.tif']; | ||
converter.total = 2; | ||
converter.location = './public'; | ||
converter.complete = function(errors, total){ | ||
convertSpy.callCount.should.equal(2); | ||
convertSpy.restore(); | ||
} | ||
converter.convert(); | ||
}); | ||
}); | ||
describe('Convert Array', function(){ | ||
@@ -422,0 +530,0 @@ it('Should log an error when the array of tiffs is null', function(done){ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38600
764
163