-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
42 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"`) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.