Skip to content

Commit 5d5774c

Browse files
committed
fix: return file with extension when url is extension-less
1 parent e51495e commit 5d5774c

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<img src="http://52.209.207.148/assets/products/dadi-cdn-full.png" alt="DADI CDN" height="65"/>
22

33
[![npm (scoped)](https://img.shields.io/npm/v/@dadi/cdn.svg?maxAge=10800&style=flat-square)](https://www.npmjs.com/package/@dadi/cdn)
4-
[![coverage](https://img.shields.io/badge/coverage-68%25-yellow.svg?style=flat-square)](https://github.com/dadi/cdn)
4+
[![coverage](https://img.shields.io/badge/coverage-69%25-yellow.svg?style=flat-square)](https://github.com/dadi/cdn)
55
[![Build Status](https://travis-ci.org/dadi/cdn.svg?branch=master)](https://travis-ci.org/dadi/cdn)
66
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)
77

dadi/lib/handlers/image.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,16 @@ ImageHandler.prototype.contentType = function () {
863863
}
864864
}
865865

866+
/**
867+
* Returns the filename including extension of the requested image
868+
* @returns {string} the filename of the image
869+
*/
866870
ImageHandler.prototype.getFilename = function () {
867-
return this.fileName
871+
if (path.extname(this.fileName) === '') {
872+
return this.fileName + '.' + this.fileExt
873+
} else {
874+
return this.fileName
875+
}
868876
}
869877

870878
ImageHandler.prototype.getLastModified = function () {

test/unit/imagehandler.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,30 @@ describe('ImageHandler', function (done) {
265265
done()
266266
})
267267
})
268+
269+
it('should return filename with jpg extension when a URL has no extension', function (done) {
270+
var newTestConfig = JSON.parse(testConfigString)
271+
newTestConfig.caching.directory.enabled = false
272+
newTestConfig.caching.redis.enabled = false
273+
cache.reset()
274+
newTestConfig.images.directory.enabled = false
275+
newTestConfig.images.s3.enabled = false
276+
newTestConfig.images.remote.enabled = true
277+
fs.writeFileSync(config.configPath(), JSON.stringify(newTestConfig, null, 2))
278+
279+
config.loadFile(config.configPath())
280+
281+
var req = {
282+
headers: {},
283+
url: '/test'
284+
}
285+
286+
// set some expected values
287+
var expected = 'test.jpg'
288+
289+
var im = new imageHandler('jpg', req)
290+
im.getFilename().should.eql(expected)
291+
292+
done()
293+
})
268294
})

0 commit comments

Comments
 (0)