This commit is contained in:
Jeffrey Sun 2017-06-13 18:37:52 -07:00
parent a8ca633a11
commit fabc7c87bc
3 changed files with 157 additions and 25 deletions

View file

@ -12,15 +12,37 @@ const sanitize = require('sanitize-filename')
const Canvas = require('canvas')
const Fox = require('./js/fox.js')
const FoxAmerica = require('./js/fox-america.js')
const renderFox = require('./js/render-fox.js')
function composeImage (width, height, seed) {
function composeImage (width, height, seed, version) {
seed = seed || uuid()
const fox = Fox(width, height, seed)
let fox
switch (version) {
case 2:
// America-color bg and fox
fox = FoxAmerica(width, height, seed)
break
default:
// original fox
fox = Fox(width, height, seed)
}
const canvas = new Canvas(width, height)
renderFox(canvas, fox)
return canvas
};
}
function getFox (req, res, version) {
let width = parseInt(req.params.width) || 400
if (width > 400) width = 400
const seed = sanitize(req.params.seed) || uuid()
const canvas = composeImage(width, width, seed, version)
const buffer = canvas.toBuffer()
res.set('Cache-Control', 'max-age=' + cacheTimeout)
res.set('Content-length', buffer.length)
res.type('png')
res.end(buffer, 'binary')
}
const cacheTimeout = 60 * 60 * 24 * 30
const app = express()
@ -30,15 +52,11 @@ app.get('/healthcheck', (req, res) => {
})
app.get('/:width/:seed', (req, res) => {
let width = parseInt(req.params.width) || 400
if (width > 400) width = 400
const seed = sanitize(req.params.seed) || uuid()
const canvas = composeImage(width, width, seed)
const buffer = canvas.toBuffer()
res.set('Cache-Control', 'max-age=' + cacheTimeout)
res.set('Content-length', buffer.length)
res.type('png')
res.end(buffer, 'binary')
getFox(req, res, 1)
})
app.get('/2/:width/:seed', (req, res) => {
getFox(req, res, 2)
})
module.exports = app