[optim] extract cache as component
This commit is contained in:
parent
70795a11fa
commit
2ee5d89e5f
|
@ -6,7 +6,8 @@ import avatar from '../avatar';
|
|||
import {buildPronoun, parsePronouns} from "../../src/buildPronoun";
|
||||
import {loadTsv} from "../../src/tsv";
|
||||
import {handleErrorAsync} from "../../src/helpers";
|
||||
import fs from 'fs';
|
||||
import cache from "../../src/cache";
|
||||
import fs from "fs";
|
||||
|
||||
const translations = loadSuml('translations');
|
||||
|
||||
|
@ -37,21 +38,9 @@ router.get('/banner/:pronounName*.png', handleErrorAsync(async (req, res) => {
|
|||
|
||||
const pronounName = req.params.pronounName + req.params[0];
|
||||
|
||||
const cacheDir = `${__dirname}/../../cache/banner`;
|
||||
fs.mkdirSync(cacheDir, { recursive: true });
|
||||
const cacheFilename = `${cacheDir}/${pronounName}.png`;
|
||||
if (fs.existsSync(cacheFilename) && fs.statSync(cacheFilename).mtimeMs >= (new Date() - 24*60*60*1000)) {
|
||||
return res.set('content-type', mime).send(fs.readFileSync(cacheFilename));
|
||||
}
|
||||
|
||||
const saveToCacheAndReturn = (canvas) => {
|
||||
const buffer = canvas.toBuffer(mime);
|
||||
fs.writeFileSync(cacheFilename, buffer);
|
||||
return res.set('content-type', mime).send(buffer);
|
||||
}
|
||||
|
||||
registerFont('static/fonts/quicksand-v21-latin-ext_latin-regular.ttf', { family: 'Quicksand', weight: 'regular'});
|
||||
registerFont('static/fonts/quicksand-v21-latin-ext_latin-700.ttf', { family: 'Quicksand', weight: 'bold'});
|
||||
const result = await cache('banner', `${pronounName}.png`, 24 * 60, async () => {
|
||||
registerFont('static/fonts/quicksand-v21-latin-ext_latin-regular.ttf', {family: 'Quicksand', weight: 'regular'});
|
||||
registerFont('static/fonts/quicksand-v21-latin-ext_latin-700.ttf', {family: 'Quicksand', weight: 'bold'});
|
||||
|
||||
const canvas = createCanvas(width, height)
|
||||
const context = canvas.getContext('2d')
|
||||
|
@ -71,7 +60,7 @@ router.get('/banner/:pronounName*.png', handleErrorAsync(async (req, res) => {
|
|||
const user = await req.db.get(SQL`SELECT id, username, email, avatarSource FROM users WHERE username=${pronounName.substring(1)}`);
|
||||
if (!user) {
|
||||
await fallback();
|
||||
return saveToCacheAndReturn(canvas);
|
||||
return canvas.toBuffer(mime);
|
||||
}
|
||||
|
||||
const avatarImage = await loadImage(await avatar(req.db, user));
|
||||
|
@ -89,7 +78,7 @@ router.get('/banner/:pronounName*.png', handleErrorAsync(async (req, res) => {
|
|||
context.drawImage(logo, width / leftRatio + imageSize, height / 2 + logoSize - 4, logoSize, logoSize / 1.25)
|
||||
context.fillText(translations.title, width / leftRatio + imageSize + 36, height / 2 + 48);
|
||||
|
||||
return saveToCacheAndReturn(canvas);
|
||||
return canvas.toBuffer(mime);
|
||||
}
|
||||
|
||||
const pronoun = buildPronoun(
|
||||
|
@ -101,7 +90,7 @@ router.get('/banner/:pronounName*.png', handleErrorAsync(async (req, res) => {
|
|||
|
||||
if (!pronoun && pronounName !== req.config.pronouns.any) {
|
||||
await fallback();
|
||||
return saveToCacheAndReturn(canvas);
|
||||
return canvas.toBuffer(mime);
|
||||
}
|
||||
|
||||
context.drawImage(logo, width / leftRatio - imageSize / 2, height / 2 - imageSize / 1.25 / 2, imageSize, imageSize / 1.25)
|
||||
|
@ -112,7 +101,10 @@ router.get('/banner/:pronounName*.png', handleErrorAsync(async (req, res) => {
|
|||
context.font = `bold ${pronounNameOptions.length <= 2 ? '70' : '36'}pt Quicksand`
|
||||
context.fillText(pronounNameOptions.join('\n'), width / leftRatio + imageSize / 1.5, height / 2 + (pronounNameOptions.length <= 2 ? 72 : 24));
|
||||
|
||||
return saveToCacheAndReturn(canvas);
|
||||
return canvas.toBuffer(mime);
|
||||
});
|
||||
|
||||
return res.set('content-type', mime).send(result);
|
||||
}));
|
||||
|
||||
export default router;
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
import fs from 'fs';
|
||||
|
||||
export default async (dir, filename, maxAgeMinutes, generator) => {
|
||||
const cacheDir = `${__dirname}/../cache/${dir}`
|
||||
fs.mkdirSync(cacheDir, { recursive: true });
|
||||
const cacheFilename = `${cacheDir}/${filename}`;
|
||||
|
||||
if (fs.existsSync(cacheFilename) && fs.statSync(cacheFilename).mtimeMs >= (new Date() - maxAgeMinutes*60*1000)) {
|
||||
return fs.readFileSync(cacheFilename);
|
||||
}
|
||||
|
||||
const result = await generator();
|
||||
|
||||
fs.writeFileSync(cacheFilename, result);
|
||||
|
||||
return result;
|
||||
}
|
Reference in New Issue