diff --git a/README.md b/README.md index e368cad..50ba208 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,8 @@ It's copy of our example file `config.json.sample`. More or less it looks like: "size": 600, "directory": "./resized/600-jpeg", "format": "jpg", - "background": "white" + "background": "white", + "changeExtension": true }, { "size": 900, @@ -88,6 +89,7 @@ It's copy of our example file `config.json.sample`. More or less it looks like: | | template | Object | Map representing pattern substitution pair. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#template) | | | prefix | String | Prepend filename prefix if supplied. | | | suffix | String | Append filename suffix if supplied. | +| | changeExtension | Boolean | Change the extension to match the actual file type. | | | acl | String | Permission of S3 object. [See AWS ACL documentation](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property). | | reduce | - | Object | Reduce setting following fields. | | | quality | Number | Determine reduced image quality ( only `JPG` ). | @@ -97,6 +99,7 @@ It's copy of our example file `config.json.sample`. More or less it looks like: | | template | Object | Map representing pattern substitution pair. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#template) | | | prefix | String | Prepend filename prefix if supplied. | | | suffix | String | Append filename suffix if supplied. | +| | changeExtension | Boolean | Change the extension to match the actual file type. | | | acl | String | Permission of S3 object. [See AWS ACL documentation](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property). | | resize | - | Array | Resize setting list of following fields. | | | size | String | Image dimensions. [See ImageMagick geometry documentation](http://imagemagick.org/script/command-line-processing.php#geometry). | @@ -111,6 +114,7 @@ It's copy of our example file `config.json.sample`. More or less it looks like: | | template | Object | Map representing pattern substitution pair. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#template) | | | prefix | String | Prepend filename prefix if supplied. | | | suffix | String | Append filename suffix if supplied. | +| | changeExtension | Boolean | Change the extension to match the actual file type. | | | acl | String | Permission of S3 object. [See AWS ACL documentation](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property). | ### Testing Configuration diff --git a/bin/configtest b/bin/configtest index d14f645..3af2092 100755 --- a/bin/configtest +++ b/bin/configtest @@ -56,7 +56,7 @@ var reset = '\u001b[0m'; stdout.write("--------------------------------\r\n"); if ( "backup" in config ) { var backup = config.backup || {}; - validateDestination(stdout, bucket, backup.bucket, backup.directory, backup.template); + validateDestination(stdout, bucket, backup.bucket, backup.directory, backup.template, backup.changeExtension); validatePrefixAndSuffix(stdout, backup.prefix, backup.suffix); validateAcl(stdout, acl, backup.acl); } else { @@ -70,7 +70,7 @@ var reset = '\u001b[0m'; var reduce = config.reduce || {}; validateQuality(stdout, reduce.quality); validateOptimizer(stdout, reduce.jpegOptimizer || jpegOptimizer); - validateDestination(stdout, bucket, reduce.bucket, reduce.directory, reduce.template); + validateDestination(stdout, bucket, reduce.bucket, reduce.directory, reduce.template, reduce.changeExtension); validatePrefixAndSuffix(stdout, reduce.prefix, reduce.suffix); validateAcl(stdout, acl, reduce.acl); } else { @@ -90,7 +90,7 @@ var reset = '\u001b[0m'; validateFormat(stdout, resize.format); validateQuality(stdout, resize.quality); validateOptimizer(stdout, resize.jpegOptimizer || jpegOptimizer); - validateDestination(stdout, bucket, resize.bucket, resize.directory, resize.template); + validateDestination(stdout, bucket, resize.bucket, resize.directory, resize.template, resize.changeExtension); validatePrefixAndSuffix(stdout, resize.prefix, resize.suffix); validateAcl(stdout, acl, resize.acl); stdout.write("\r\n"); @@ -158,7 +158,7 @@ var reset = '\u001b[0m'; } } - function validateDestination(stdout, globalBucket, bucket, directory, template) { + function validateDestination(stdout, globalBucket, bucket, directory, template, changeExtension) { var color = reset; if ( ! bucket && ! globalBucket && (! directory || /^\.\//.test(directory)) && (! template || ! template.pattern)) { warning.push(" Saving image to the same or relative directory may cause infinite Lambda process loop."); @@ -179,6 +179,7 @@ var reset = '\u001b[0m'; stdout.write("[Same directory]"); } stdout.write(reset + "\r\n"); + stdout.write(magenta + " Change extension: " + reset + Boolean(changeExtension) + "\r\n"); } function validatePrefixAndSuffix(stdout, prefix, suffix) { diff --git a/config.json.sample b/config.json.sample index 5bc2048..873b447 100644 --- a/config.json.sample +++ b/config.json.sample @@ -10,7 +10,8 @@ { "size": 300, "directory": "./resized/small", - "prefix": "resized-" + "prefix": "resized-", + "changeExtension": true }, { "size": 450, diff --git a/lib/ImageArchiver.js b/lib/ImageArchiver.js index baed4d5..cdaf4b4 100644 --- a/lib/ImageArchiver.js +++ b/lib/ImageArchiver.js @@ -34,7 +34,8 @@ class ImageArchiver { directory: option.directory, template: option.template, prefix: option.prefix, - suffix: option.suffix + suffix: option.suffix, + changeExtension: option.changeExtension }), option.bucket || image.bucketName, image.data, diff --git a/lib/ImageData.js b/lib/ImageData.js index d949f62..d024358 100644 --- a/lib/ImageData.js +++ b/lib/ImageData.js @@ -160,8 +160,9 @@ class ImageData { combineWithDirectory(output) { const prefix = output.prefix || ""; const suffix = output.suffix || ""; - const fileName = path.parse(this.baseName).name; - const extension = "." + this.type.ext; + const parsed = path.parse(this.baseName); + const fileName = parsed.name; + const extension = output.changeExtension ? ("." + this.type.ext) : parsed.ext; const template = output.template; if ( typeof template === "object" && template.pattern ) { diff --git a/lib/ImageReducer.js b/lib/ImageReducer.js index b33e6d2..4181163 100644 --- a/lib/ImageReducer.js +++ b/lib/ImageReducer.js @@ -42,7 +42,8 @@ class ImageReducer { directory: option.directory, template: option.template, prefix: option.prefix, - suffix: option.suffix + suffix: option.suffix, + changeExtension: option.changeExtension }), option.bucket || image.bucketName, buffer, diff --git a/test/e2e-jpeg.js b/test/e2e-jpeg.js index e104106..0b4eb1c 100644 --- a/test/e2e-jpeg.js +++ b/test/e2e-jpeg.js @@ -125,6 +125,32 @@ test("Resize JPEG with format", async t => { })); t.is(images.length, 2); + const pngImage = images.shift(); + t.is(pngImage.fileName, "HappyFace.jpg"); + t.true(pngImage.data.length > 0); + + const gifImage = images.shift(); + t.is(gifImage.fileName, "HappyFace.jpg"); + t.true(gifImage.data.length > 0); +}); + +test("Resize JPEG with format and changeExtension", async t => { + await processor.run(new Config({ + "resizes": [ + { + "size": 100, + "format": "png", + "changeExtension": true + }, + { + "size": 100, + "format": "gif", + "changeExtension": true + } + ] + })); + t.is(images.length, 2); + const pngImage = images.shift(); t.is(pngImage.fileName, "HappyFace.png"); t.true(pngImage.data.length > 0); @@ -132,4 +158,54 @@ test("Resize JPEG with format", async t => { const gifImage = images.shift(); t.is(gifImage.fileName, "HappyFace.gif"); t.true(gifImage.data.length > 0); -}); \ No newline at end of file +}); + +test("Resize JPEG with format", async t => { + await processor.run(new Config({ + "resizes": [ + { + "size": 100, + "format": "png" + }, + { + "size": 100, + "format": "gif" + } + ] + })); + t.is(images.length, 2); + + const pngImage = images.shift(); + t.is(pngImage.fileName, "HappyFace.jpg"); + t.true(pngImage.data.length > 0); + + const gifImage = images.shift(); + t.is(gifImage.fileName, "HappyFace.jpg"); + t.true(gifImage.data.length > 0); +}); + +test("Resize JPEG with format and changeExtension", async t => { + await processor.run(new Config({ + "resizes": [ + { + "size": 100, + "format": "png", + "changeExtension": true + }, + { + "size": 100, + "format": "gif", + "changeExtension": true + } + ] + })); + t.is(images.length, 2); + + const pngImage = images.shift(); + t.is(pngImage.fileName, "HappyFace.png"); + t.true(pngImage.data.length > 0); + + const gifImage = images.shift(); + t.is(gifImage.fileName, "HappyFace.gif"); + t.true(gifImage.data.length > 0); +});