Use Express router for thumbnails endpoint

This commit is contained in:
valadaptive
2023-12-04 12:59:24 -05:00
parent 414c9bd5fb
commit 2d54a67a1f
2 changed files with 33 additions and 38 deletions

View File

@ -3587,8 +3587,8 @@ require('./src/endpoints/presets').registerEndpoints(app, jsonParser);
// Secrets managemenet // Secrets managemenet
require('./src/endpoints/secrets').registerEndpoints(app, jsonParser); require('./src/endpoints/secrets').registerEndpoints(app, jsonParser);
// Thumbnail generation // Thumbnail generation. These URLs are saved in chat, so this route cannot be renamed!
require('./src/endpoints/thumbnails').registerEndpoints(app, jsonParser); app.use('/thumbnail', require('./src/endpoints/thumbnails').router);
// NovelAI generation // NovelAI generation
require('./src/endpoints/novelai').registerEndpoints(app, jsonParser); require('./src/endpoints/novelai').registerEndpoints(app, jsonParser);

View File

@ -1,10 +1,12 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const express = require('express');
const sanitize = require('sanitize-filename'); const sanitize = require('sanitize-filename');
const jimp = require('jimp'); const jimp = require('jimp');
const writeFileAtomicSync = require('write-file-atomic').sync; const writeFileAtomicSync = require('write-file-atomic').sync;
const { DIRECTORIES } = require('../constants'); const { DIRECTORIES } = require('../constants');
const { getConfigValue } = require('../util'); const { getConfigValue } = require('../util');
const { jsonParser } = require('../express-common');
/** /**
* Gets a path to thumbnail folder based on the type. * Gets a path to thumbnail folder based on the type.
@ -150,15 +152,10 @@ async function ensureThumbnailCache() {
console.log(`Done! Generated: ${bgFiles.length} preview images`); console.log(`Done! Generated: ${bgFiles.length} preview images`);
} }
const router = express.Router();
/** // Important: This route must be mounted as '/thumbnail'. It is used in the client code and saved to chat files.
* Registers the endpoints for the thumbnail management. router.get('/', jsonParser, async function (request, response) {
* @param {import('express').Express} app Express app
* @param {any} jsonParser JSON parser middleware
*/
function registerEndpoints(app, jsonParser) {
// Important: Do not change a path to this endpoint. It is used in the client code and saved to chat files.
app.get('/thumbnail', jsonParser, async function (request, response) {
if (typeof request.query.file !== 'string' || typeof request.query.type !== 'string') return response.sendStatus(400); if (typeof request.query.file !== 'string' || typeof request.query.type !== 'string') return response.sendStatus(400);
const type = request.query.type; const type = request.query.type;
@ -193,10 +190,8 @@ function registerEndpoints(app, jsonParser) {
return response.sendFile(pathToCachedFile, { root: process.cwd() }); return response.sendFile(pathToCachedFile, { root: process.cwd() });
}); });
}
module.exports = { module.exports = {
invalidateThumbnail, invalidateThumbnail,
registerEndpoints,
ensureThumbnailCache, ensureThumbnailCache,
router,
}; };