Merge pull request #1465 from valadaptive/thumbnails-router
Use Express router for thumbnails endpoint
This commit is contained in:
commit
5fa81ec37c
|
@ -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);
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue