Created
June 24, 2019 08:22
-
-
Save tenthree/4969fdc556dbc4c34fa732e0d449e809 to your computer and use it in GitHub Desktop.
fake-images middleware for local development
This file contains hidden or 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
module.exports = function () { | |
// | |
// [ express middleware ] | |
// Generate a /fake/images route for testing images source | |
// | |
// images service origin: | |
// - PICSUM(https://picsum.photos) | |
// Created by David Marby & Nijiko Yonskai | |
// Having trouble? Poke @DMarby on Twitter | |
// Images from unsplash | |
// -------------------------------------------------------------------------------- | |
// usage: | |
// /fake/images/{width}/{height}/{id:0~999}{?simple} | |
// e.g. | |
// /fake/images/320/480/ | |
// /fake/images/720-960/480/ | |
// /fake/images/720-960/480-540/13 | |
// /fake/images/720-960/480-540/13?simple | |
// | |
const axios = require('axios') | |
const PROTOCOL = 'http' | |
const VENDORS = { | |
picsum: 'picsum.photos' | |
} | |
const MIN_WIDTH = 100 | |
const MIN_HEIGHT = 100 | |
const MAX_WIDTH = 1000 | |
const MAX_HEIGHT = 1000 | |
const rand = function (minNum, MaxNum) { | |
let len = arguments.length | |
if (len === 0) { | |
return Math.random() | |
} else if (len > 1) { | |
return Math.round(Math.random() * (MaxNum - minNum)) + minNum | |
} | |
return Math.round(Math.random() * minNum) | |
} | |
return function (req, res, next) { | |
let params | |
let photoId | |
let simple | |
let width | |
let height | |
let widthInfo | |
let heightInfo | |
let minWidth | |
let minHeight | |
let maxWidth | |
let maxHeight | |
let vendorList | |
let vendorName | |
let source | |
if (!/^\/fake\/images(\/.*)?$/.test(req.path)) { | |
next() | |
return | |
} | |
params = req.path.replace(/^\/fake\/images\/?/, '').split('/') | |
widthInfo = params[0] || null | |
heightInfo = params[1] || null | |
photoId = params[2] || rand(0, 999) | |
simple = (typeof req.query.simple === 'string') | |
if (!widthInfo) { | |
width = rand(MIN_WIDTH, MAX_WIDTH) | |
} else { | |
widthInfo = widthInfo.split('-') | |
if (widthInfo.length < 2) { | |
width = Number(widthInfo[0]) || rand(MIN_WIDTH, MAX_WIDTH) | |
} else { | |
minWidth = Number(widthInfo[0]) || MIN_WIDTH | |
maxWidth = Number(widthInfo[1]) || MIN_WIDTH | |
width = rand(minWidth, maxWidth) | |
} | |
} | |
if (!heightInfo) { | |
height = rand(MIN_HEIGHT, MAX_HEIGHT) | |
} else { | |
heightInfo = heightInfo.split('-') | |
if (heightInfo.length < 2) { | |
height = Number(heightInfo[0]) || rand(MIN_HEIGHT, MAX_HEIGHT) | |
} else { | |
minHeight = Number(heightInfo[0]) || MIN_HEIGHT | |
maxHeight = Number(heightInfo[1]) || MIN_HEIGHT | |
height = rand(minHeight, maxHeight) | |
} | |
} | |
vendorList = Object.keys(VENDORS) | |
vendorName = (simple || !vendorList.length) ? null : vendorList[rand(0, vendorList.length - 1)] | |
switch (vendorName) { | |
case 'picsum': | |
source = `${PROTOCOL}://${VENDORS[vendorName]}/id/${photoId}/${width}/${height}?random=${Date.now()}` | |
break | |
default: | |
source = `${PROTOCOL}://placehold.jp/${width}x${height}.png` | |
} | |
if (!source) { | |
next() | |
return | |
} | |
axios.get(source, { responseType: 'stream' }) | |
.then(response => { | |
response.data.pipe(res) | |
}) | |
.catch(err => { | |
console.log(err) | |
next() | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment