Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/dadi/cdn into patch/exif…
Browse files Browse the repository at this point in the history
…-reader
  • Loading branch information
jimlambie committed Nov 9, 2018
2 parents e43b110 + cc3ae53 commit ceaf812
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 18 deletions.
10 changes: 1 addition & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@ notifications:
email: false
node_js:
- '8'
- '10'
before_script:
- npm prune
branches:
except:
- /^v\d+\.\d+\.\d+$/
env:
- CXX=g++-4.9
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.9
before_install:
- if [[ `npm -v` != 5* ]]; then npm i -g npm@latest; fi
- sudo apt-get install libjpeg-dev libgif-dev
4 changes: 2 additions & 2 deletions dadi/lib/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const Controller = function (router) {

pattern = pattern.concat([
parsedUrl.pathname,
parsedUrl.search.slice(1)
parsedUrl.search ? parsedUrl.search.slice(1) : null
])
}

Expand Down Expand Up @@ -250,7 +250,7 @@ Controller.prototype.addCacheControlHeader = function (res, handler, domain) {
if (!value || (value.length === 0)) return

// already set
if (res._headers['cache-control']) return
if (res.getHeader('cache-control')) return

// set the header
res.setHeader('Cache-Control', value)
Expand Down
31 changes: 27 additions & 4 deletions dadi/lib/handlers/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const sharp = require('sharp')
const smartcrop = require('smartcrop-sharp')
const urlParser = require('url')
const Vibrant = require('node-vibrant')
const imagemin = require('imagemin')
const imageminJpegtran = require('imagemin-jpegtran')

const StorageFactory = require(path.join(__dirname, '/../storage/factory'))
const Cache = require(path.join(__dirname, '/../cache'))
Expand Down Expand Up @@ -61,7 +63,8 @@ const IMAGE_PARAMETERS = [
{ name: 'blur', aliases: ['b'] },
{ name: 'strip', aliases: ['s'] },
{ name: 'rotate', aliases: ['r'] },
{ name: 'flip', aliases: ['fl'] }
{ name: 'flip', aliases: ['fl'] },
{ name: 'progressive', aliases: ['pg'], default: 'true' }
]

/**
Expand Down Expand Up @@ -792,9 +795,9 @@ ImageHandler.prototype.process = function (sharpImage, imageBuffer) {
will _not_ be preserved.
*/
case 'fill':
resizeOptions.fit = 'fill'
sharpImage = sharpImage
.resize(width, height, resizeOptions)
.ignoreAspectRatio()

break

Expand Down Expand Up @@ -822,7 +825,7 @@ ImageHandler.prototype.process = function (sharpImage, imageBuffer) {
// resize if options.width or options.height are explicitly set
if (options.width || options.height) {
if (options.width && options.height) {
sharpImage = sharpImage.ignoreAspectRatio()
resizeOptions.fit = 'fill'
}

if (options.devicePixelRatio && options.devicePixelRatio < 4) {
Expand Down Expand Up @@ -989,6 +992,10 @@ ImageHandler.prototype.process = function (sharpImage, imageBuffer) {
processBuffer = this.processGif(buffer)
}

if (options.progressive === 'true' && (format === 'jpeg' || format === 'jpg')) {
processBuffer = this.progressiveJpeg(buffer)
}

processBuffer.then(buffer => {
resolve(buffer)
})
Expand Down Expand Up @@ -1030,6 +1037,21 @@ ImageHandler.prototype.processGif = function (buffer) {
})
}

/**
* Transcodes an input buffer to a progressive JPEG
*
* @param {Buffer} buffer - a Buffer extracted from the main image
* processor after applying image manipulations
* @returns {Buffer} a progressive JPEG encoded buffer
*/
ImageHandler.prototype.progressiveJpeg = function (buffer) {
return imagemin.buffer(buffer, {
plugins: [
imageminJpegtran({progressive: true})
]
})
}

ImageHandler.prototype.sanitiseOptions = function (options) {
// check the options for aliases
// e.g. "dpr" === "devicePixelRatio"
Expand Down Expand Up @@ -1153,7 +1175,8 @@ function getImageOptionsFromLegacyURL (optionsArray) {
blur: optionsArray[9 + superLegacyFormatOffset],
strip: optionsArray[10 + superLegacyFormatOffset],
rotate: optionsArray[11 + superLegacyFormatOffset],
flip: optionsArray[12 + superLegacyFormatOffset]
flip: optionsArray[12 + superLegacyFormatOffset],
progressive: optionsArray[13 + superLegacyFormatOffset]
}

return options
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"he": "^1.1.0",
"husky": "^1.0.1",
"image-size-stream": "1.1.0",
"imagemin": "^6.0.0",
"imagemin-jpegtran": "^5.0.2",
"images": "^3.0.0",
"jimp": "^0.2.28",
"jsonwebtoken": "^8.2.1",
Expand Down
5 changes: 4 additions & 1 deletion test/acceptance/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ describe('Authentication', function () {
.send({pattern: 'test'})
.set('Authorization', 'Bearer ' + token)
.expect('content-type', 'application/json')
.expect(200, done)
.end((err, res) => {
res.statusCode.should.eql(200)
done()
})
})
})

Expand Down
12 changes: 10 additions & 2 deletions test/acceptance/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,20 @@ module.exports.clearCache = function () {
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath)
} else { // delete file
fs.unlinkSync(path.resolve(curPath))
try {
fs.unlinkSync(path.resolve(curPath))
} catch (err) {

}
}
})
fs.rmdirSync(filepath)
} else {
fs.unlinkSync(filepath)
try {
fs.unlinkSync(filepath)
} catch (err) {

}
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/acceptance/visual_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,13 @@
"quality": "100"
},
"baselineFilename": "images/visual/baseline/measure.png?quality=100.png"
},
{
"image": "measure2.jpg",
"params": {
"progressive": true
},
"baselineFilename": "images/visual/baseline/measure2.jpg?progressive=true.jpg"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ceaf812

Please sign in to comment.