Skip to content

Commit

Permalink
Merge 098548e into a057551
Browse files Browse the repository at this point in the history
  • Loading branch information
terales committed Jul 6, 2018
2 parents a057551 + 098548e commit 1823665
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
19 changes: 17 additions & 2 deletions src/middleware/imageNotFoundHandler.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
// Native Node.js modules
const fs = require('fs')
const path = require('path')

// Local modules
const fallback = path.join(__dirname, '..', 'public', 'fallback.svg')
const fallbackPath = path.join(__dirname, '..', 'public', 'fallback.svg')
const fallbackStr = fs.readFileSync(fallbackPath, 'utf8')

module.exports = function imageNotFoundHandler (err, req, res, next) {
if (res.headersSent || err.message !== '404') { return next(err) }
res.header('cache-control', 'public, max-age=1209600, no-transform')
res.status(404).sendFile(fallback)
res.header('content-type', 'image/svg+xml')
res.status(404)

if (res.locals.width === -1 && res.locals.height === -1) {
// 100% width, 100% height placeholder
res.sendFile(fallbackPath)
} else {
// placeholder with requested size
res.send(
fallbackStr
.replace('width="100"', `width="${res.locals.width}"`)
.replace('height="100"', `height="${res.locals.height}"`)
)
}
}
7 changes: 5 additions & 2 deletions src/middleware/normalizeDimentions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ module.exports = function normalizeDimentions (req, res, next) {
return res.status(400).send('Invalid image dimentions. Width and height should be integers from 1 to 599')
}

req.params = Object.assign({
const dimentions = {
width: getOriginal ? -1 : process.env.AVATAR_WIDTH,
height: getOriginal ? -1 : process.env.AVATAR_HEIGHT
}, req.params)
}

req.params = Object.assign(dimentions, req.params) // For middleware
res.locals = Object.assign(dimentions, res.locals) // For error handling
next()
}

Expand Down
7 changes: 2 additions & 5 deletions src/public/fallback.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions tests/macros/notFoundImages.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Native Node.js modules
const path = require('path')
const promisify = require('util').promisify
const fs = require('fs')

// Third party dependencies
const supertest = require('supertest')

// Local modules
const app = require('./../../src/app')()
const fallback = path.join(__dirname, '..', '..', 'src', 'public', 'fallback.svg')
const fallbackPath = path.join(__dirname, '..', '..', 'src', 'public', 'fallback.svg')
const fallbackImg = fs.readFileSync(fallbackPath)

module.exports = async function notFoundImages (t, source) {
const res = await supertest(app)
.get(`/${source}/definiteryWrongUserIdHere`)

t.is(res.statusCode, 404)
t.deepEqual(res.body, await promisify(fs.readFile)(fallback))
t.deepEqual(res.body, fallbackImg)
}
15 changes: 15 additions & 0 deletions tests/respectSizeIn404.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Third party dependencies
const test = require('ava')
const supertest = require('supertest')

// Local modules
const app = require('./../src/app')()

test('Should respect requested size on 404 placeholder', async t => {
const res = await supertest(app)
.get('/gravatar/no-exisitng-user/64-64')

t.is(res.statusCode, 404)
t.true(res.body.indexOf('width="64"') !== -1)
t.true(res.body.indexOf('height="64"') !== -1)
})
Binary file modified tests/sources/google/samples/socatar.com@gmail.com.png
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 1823665

Please sign in to comment.