Skip to content

Commit 57b515e

Browse files
committed
config option to change the extension
1 parent 6a38142 commit 57b515e

File tree

7 files changed

+96
-11
lines changed

7 files changed

+96
-11
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ It's copy of our example file `config.json.sample`. More or less it looks like:
6464
"size": 600,
6565
"directory": "./resized/600-jpeg",
6666
"format": "jpg",
67-
"background": "white"
67+
"background": "white",
68+
"changeExtension": true
6869
},
6970
{
7071
"size": 900,
@@ -88,6 +89,7 @@ It's copy of our example file `config.json.sample`. More or less it looks like:
8889
| | template | Object | Map representing pattern substitution pair. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#template) |
8990
| | prefix | String | Prepend filename prefix if supplied. |
9091
| | suffix | String | Append filename suffix if supplied. |
92+
| | changeExtension | Boolean | Change the extension to match the actual file type. |
9193
| | acl | String | Permission of S3 object. [See AWS ACL documentation](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property). |
9294
| reduce | - | Object | Reduce setting following fields. |
9395
| | 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:
9799
| | template | Object | Map representing pattern substitution pair. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#template) |
98100
| | prefix | String | Prepend filename prefix if supplied. |
99101
| | suffix | String | Append filename suffix if supplied. |
102+
| | changeExtension | Boolean | Change the extension to match the actual file type. |
100103
| | acl | String | Permission of S3 object. [See AWS ACL documentation](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property). |
101104
| resize | - | Array | Resize setting list of following fields. |
102105
| | 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:
111114
| | template | Object | Map representing pattern substitution pair. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#template) |
112115
| | prefix | String | Prepend filename prefix if supplied. |
113116
| | suffix | String | Append filename suffix if supplied. |
117+
| | changeExtension | Boolean | Change the extension to match the actual file type. |
114118
| | acl | String | Permission of S3 object. [See AWS ACL documentation](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property). |
115119

116120
### Testing Configuration

bin/configtest

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var reset = '\u001b[0m';
5656
stdout.write("--------------------------------\r\n");
5757
if ( "backup" in config ) {
5858
var backup = config.backup || {};
59-
validateDestination(stdout, bucket, backup.bucket, backup.directory, backup.template);
59+
validateDestination(stdout, bucket, backup.bucket, backup.directory, backup.template, backup.changeExtension);
6060
validatePrefixAndSuffix(stdout, backup.prefix, backup.suffix);
6161
validateAcl(stdout, acl, backup.acl);
6262
} else {
@@ -70,7 +70,7 @@ var reset = '\u001b[0m';
7070
var reduce = config.reduce || {};
7171
validateQuality(stdout, reduce.quality);
7272
validateOptimizer(stdout, reduce.jpegOptimizer || jpegOptimizer);
73-
validateDestination(stdout, bucket, reduce.bucket, reduce.directory, reduce.template);
73+
validateDestination(stdout, bucket, reduce.bucket, reduce.directory, reduce.template, reduce.changeExtension);
7474
validatePrefixAndSuffix(stdout, reduce.prefix, reduce.suffix);
7575
validateAcl(stdout, acl, reduce.acl);
7676
} else {
@@ -90,7 +90,7 @@ var reset = '\u001b[0m';
9090
validateFormat(stdout, resize.format);
9191
validateQuality(stdout, resize.quality);
9292
validateOptimizer(stdout, resize.jpegOptimizer || jpegOptimizer);
93-
validateDestination(stdout, bucket, resize.bucket, resize.directory, resize.template);
93+
validateDestination(stdout, bucket, resize.bucket, resize.directory, resize.template, resize.changeExtension);
9494
validatePrefixAndSuffix(stdout, resize.prefix, resize.suffix);
9595
validateAcl(stdout, acl, resize.acl);
9696
stdout.write("\r\n");
@@ -158,7 +158,7 @@ var reset = '\u001b[0m';
158158
}
159159
}
160160

161-
function validateDestination(stdout, globalBucket, bucket, directory, template) {
161+
function validateDestination(stdout, globalBucket, bucket, directory, template, changeExtension) {
162162
var color = reset;
163163
if ( ! bucket && ! globalBucket && (! directory || /^\.\//.test(directory)) && (! template || ! template.pattern)) {
164164
warning.push(" Saving image to the same or relative directory may cause infinite Lambda process loop.");
@@ -179,6 +179,7 @@ var reset = '\u001b[0m';
179179
stdout.write("[Same directory]");
180180
}
181181
stdout.write(reset + "\r\n");
182+
stdout.write(magenta + " Change extension: " + reset + Boolean(changeExtension) + "\r\n");
182183
}
183184

184185
function validatePrefixAndSuffix(stdout, prefix, suffix) {

config.json.sample

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
{
1111
"size": 300,
1212
"directory": "./resized/small",
13-
"prefix": "resized-"
13+
"prefix": "resized-",
14+
"changeExtension": true
1415
},
1516
{
1617
"size": 450,

lib/ImageArchiver.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class ImageArchiver {
3434
directory: option.directory,
3535
template: option.template,
3636
prefix: option.prefix,
37-
suffix: option.suffix
37+
suffix: option.suffix,
38+
changeExtension: option.changeExtension
3839
}),
3940
option.bucket || image.bucketName,
4041
image.data,

lib/ImageData.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ class ImageData {
160160
combineWithDirectory(output) {
161161
const prefix = output.prefix || "";
162162
const suffix = output.suffix || "";
163-
const fileName = path.parse(this.baseName).name;
164-
const extension = "." + this.type.ext;
163+
const parsed = path.parse(this.baseName);
164+
const fileName = parsed.name;
165+
const extension = output.changeExtension ? ("." + this.type.ext) : parsed.ext;
165166

166167
const template = output.template;
167168
if ( typeof template === "object" && template.pattern ) {

lib/ImageReducer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class ImageReducer {
4242
directory: option.directory,
4343
template: option.template,
4444
prefix: option.prefix,
45-
suffix: option.suffix
45+
suffix: option.suffix,
46+
changeExtension: option.changeExtension
4647
}),
4748
option.bucket || image.bucketName,
4849
buffer,

test/e2e-jpeg.js

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,87 @@ test("Resize JPEG with format", async t => {
125125
}));
126126
t.is(images.length, 2);
127127

128+
const pngImage = images.shift();
129+
t.is(pngImage.fileName, "HappyFace.jpg");
130+
t.true(pngImage.data.length > 0);
131+
132+
const gifImage = images.shift();
133+
t.is(gifImage.fileName, "HappyFace.jpg");
134+
t.true(gifImage.data.length > 0);
135+
});
136+
137+
test("Resize JPEG with format and changeExtension", async t => {
138+
await processor.run(new Config({
139+
"resizes": [
140+
{
141+
"size": 100,
142+
"format": "png",
143+
"changeExtension": true
144+
},
145+
{
146+
"size": 100,
147+
"format": "gif",
148+
"changeExtension": true
149+
}
150+
]
151+
}));
152+
t.is(images.length, 2);
153+
128154
const pngImage = images.shift();
129155
t.is(pngImage.fileName, "HappyFace.png");
130156
t.true(pngImage.data.length > 0);
131157

132158
const gifImage = images.shift();
133159
t.is(gifImage.fileName, "HappyFace.gif");
134160
t.true(gifImage.data.length > 0);
135-
});
161+
});
162+
163+
test("Resize JPEG with format", async t => {
164+
await processor.run(new Config({
165+
"resizes": [
166+
{
167+
"size": 100,
168+
"format": "png"
169+
},
170+
{
171+
"size": 100,
172+
"format": "gif"
173+
}
174+
]
175+
}));
176+
t.is(images.length, 2);
177+
178+
const pngImage = images.shift();
179+
t.is(pngImage.fileName, "HappyFace.jpg");
180+
t.true(pngImage.data.length > 0);
181+
182+
const gifImage = images.shift();
183+
t.is(gifImage.fileName, "HappyFace.jpg");
184+
t.true(gifImage.data.length > 0);
185+
});
186+
187+
test("Resize JPEG with format and changeExtension", async t => {
188+
await processor.run(new Config({
189+
"resizes": [
190+
{
191+
"size": 100,
192+
"format": "png",
193+
"changeExtension": true
194+
},
195+
{
196+
"size": 100,
197+
"format": "gif",
198+
"changeExtension": true
199+
}
200+
]
201+
}));
202+
t.is(images.length, 2);
203+
204+
const pngImage = images.shift();
205+
t.is(pngImage.fileName, "HappyFace.png");
206+
t.true(pngImage.data.length > 0);
207+
208+
const gifImage = images.shift();
209+
t.is(gifImage.fileName, "HappyFace.gif");
210+
t.true(gifImage.data.length > 0);
211+
});

0 commit comments

Comments
 (0)