Skip to content

Commit

Permalink
config option to change the extension
Browse files Browse the repository at this point in the history
  • Loading branch information
odedniv committed Mar 27, 2017
1 parent 9924bc1 commit d0c2794
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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` ). |
Expand All @@ -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). |
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions bin/configtest
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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");
Expand Down Expand Up @@ -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.");
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion config.json.sample
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
{
"size": 300,
"directory": "./resized/small",
"prefix": "resized-"
"prefix": "resized-",
"changeExtension": true
},
{
"size": 450,
Expand Down
3 changes: 2 additions & 1 deletion lib/ImageArchiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions lib/ImageData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
3 changes: 2 additions & 1 deletion lib/ImageReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
52 changes: 51 additions & 1 deletion test/e2e-jpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,61 @@ 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.gif");
t.true(gifImage.data.length > 0);
});

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 changes extension", 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);
});
});

0 comments on commit d0c2794

Please sign in to comment.