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 Dec 23, 2017
1 parent 513c53b commit da66ec5
Show file tree
Hide file tree
Showing 7 changed files with 96 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). |
| | move | Boolean | If `true`, an original uploaded file will delete from Bucket after completion. |
| reduce | - | Object | Reduce setting following fields. |
Expand All @@ -98,6 +100,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 @@ -112,6 +115,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). |
| optimizers | - | Object | Definitions for override the each Optimizers command arguments. |
| | pngquant | Array | `Pngquant` command arguments. Default is `["--speed=1", "256"]`. |
Expand Down
9 changes: 5 additions & 4 deletions bin/configtest
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,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 @@ -79,7 +79,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 @@ -99,7 +99,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 @@ -171,7 +171,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 @@ -192,6 +192,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
78 changes: 77 additions & 1 deletion test/e2e-jpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,87 @@ 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);

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 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);
});

0 comments on commit da66ec5

Please sign in to comment.