Merge pull request #1465 from valadaptive/thumbnails-router

Use Express router for thumbnails endpoint
This commit is contained in:
Cohee 2023-12-05 00:10:12 +02:00 committed by GitHub
commit 5fa81ec37c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 38 deletions

View File

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

View File

@ -1,10 +1,12 @@
const fs = require('fs');
const path = require('path');
const express = require('express');
const sanitize = require('sanitize-filename');
const jimp = require('jimp');
const writeFileAtomicSync = require('write-file-atomic').sync;
const { DIRECTORIES } = require('../constants');
const { getConfigValue } = require('../util');
const { jsonParser } = require('../express-common');
/**
* Gets a path to thumbnail folder based on the type.
@ -150,53 +152,46 @@ async function ensureThumbnailCache() {
console.log(`Done! Generated: ${bgFiles.length} preview images`);
}
const router = express.Router();
/**
* Registers the endpoints for the thumbnail management.
* @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);
// Important: This route must be mounted as '/thumbnail'. It is used in the client code and saved to chat files.
router.get('/', jsonParser, async function (request, response) {
if (typeof request.query.file !== 'string' || typeof request.query.type !== 'string') return response.sendStatus(400);
const type = request.query.type;
const file = sanitize(request.query.file);
const type = request.query.type;
const file = sanitize(request.query.file);
if (!type || !file) {
return response.sendStatus(400);
}
if (!type || !file) {
return response.sendStatus(400);
}
if (!(type == 'bg' || type == 'avatar')) {
return response.sendStatus(400);
}
if (!(type == 'bg' || type == 'avatar')) {
return response.sendStatus(400);
}
if (sanitize(file) !== file) {
console.error('Malicious filename prevented');
return response.sendStatus(403);
}
if (sanitize(file) !== file) {
console.error('Malicious filename prevented');
return response.sendStatus(403);
}
if (getConfigValue('disableThumbnails', false) == true) {
let folder = getOriginalFolder(type);
if (folder === undefined) return response.sendStatus(400);
const pathToOriginalFile = path.join(folder, file);
return response.sendFile(pathToOriginalFile, { root: process.cwd() });
}
if (getConfigValue('disableThumbnails', false) == true) {
let folder = getOriginalFolder(type);
if (folder === undefined) return response.sendStatus(400);
const pathToOriginalFile = path.join(folder, file);
return response.sendFile(pathToOriginalFile, { root: process.cwd() });
}
const pathToCachedFile = await generateThumbnail(type, file);
const pathToCachedFile = await generateThumbnail(type, file);
if (!pathToCachedFile) {
return response.sendStatus(404);
}
if (!pathToCachedFile) {
return response.sendStatus(404);
}
return response.sendFile(pathToCachedFile, { root: process.cwd() });
});
}
return response.sendFile(pathToCachedFile, { root: process.cwd() });
});
module.exports = {
invalidateThumbnail,
registerEndpoints,
ensureThumbnailCache,
router,
};