Extract API endpoint for moving UI

This commit is contained in:
Cohee 2024-03-20 01:07:28 +02:00
parent b261c8c4a9
commit abb8bdbc1e
4 changed files with 31 additions and 17 deletions

View File

@ -2175,7 +2175,7 @@ async function saveMovingUI() {
}; };
console.log(movingUIPreset); console.log(movingUIPreset);
const response = await fetch('/savemovingui', { const response = await fetch('/api/moving-ui/save', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify(movingUIPreset), body: JSON.stringify(movingUIPreset),

View File

@ -10,8 +10,6 @@ const util = require('util');
// cli/fs related library imports // cli/fs related library imports
const open = require('open'); const open = require('open');
const sanitize = require('sanitize-filename');
const writeFileAtomicSync = require('write-file-atomic').sync;
const yargs = require('yargs/yargs'); const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers'); const { hideBin } = require('yargs/helpers');
@ -36,7 +34,6 @@ util.inspect.defaultOptions.maxStringLength = null;
// local library imports // local library imports
const basicAuthMiddleware = require('./src/middleware/basicAuth'); const basicAuthMiddleware = require('./src/middleware/basicAuth');
const whitelistMiddleware = require('./src/middleware/whitelist'); const whitelistMiddleware = require('./src/middleware/whitelist');
const { jsonParser } = require('./src/express-common.js');
const contentManager = require('./src/endpoints/content-manager'); const contentManager = require('./src/endpoints/content-manager');
const { const {
getVersion, getVersion,
@ -200,7 +197,7 @@ if (getConfigValue('enableCorsProxy', false) || cliArguments.corsProxy) {
app.use(express.static(process.cwd() + '/public', {})); app.use(express.static(process.cwd() + '/public', {}));
app.use('/backgrounds', (req, res) => { app.use('/backgrounds', (req, res) => {
const filePath = decodeURIComponent(path.join(process.cwd(), 'public/backgrounds', req.url.replace(/%20/g, ' '))); const filePath = decodeURIComponent(path.join(process.cwd(), DIRECTORIES.backgrounds, req.url.replace(/%20/g, ' ')));
fs.readFile(filePath, (err, data) => { fs.readFile(filePath, (err, data) => {
if (err) { if (err) {
res.status(404).send('File not found'); res.status(404).send('File not found');
@ -230,17 +227,6 @@ app.get('/version', async function (_, response) {
response.send(data); response.send(data);
}); });
app.post('/savemovingui', jsonParser, (request, response) => {
if (!request.body || !request.body.name) {
return response.sendStatus(400);
}
const filename = path.join(DIRECTORIES.movingUI, sanitize(request.body.name) + '.json');
writeFileAtomicSync(filename, JSON.stringify(request.body, null, 4), 'utf8');
return response.sendStatus(200);
});
function cleanUploads() { function cleanUploads() {
try { try {
if (fs.existsSync(UPLOADS_PATH)) { if (fs.existsSync(UPLOADS_PATH)) {
@ -340,6 +326,12 @@ redirect('/savequickreply', '/api/quick-replies/save');
redirect('/uploadimage', '/api/images/upload'); redirect('/uploadimage', '/api/images/upload');
redirect('/listimgfiles/:folder', '/api/images/list/:folder'); redirect('/listimgfiles/:folder', '/api/images/list/:folder');
// Redirect deprecated moving UI endpoints
redirect('/savemovingui', '/api/moving-ui/save');
// Moving UI
app.use('/api/moving-ui', require('./src/endpoints/moving-ui').router);
// Image management // Image management
app.use('/api/images', require('./src/endpoints/images').router); app.use('/api/images', require('./src/endpoints/images').router);

View File

@ -8,7 +8,7 @@ const { DIRECTORIES, UPLOADS_PATH } = require('../constants');
const { invalidateThumbnail } = require('./thumbnails'); const { invalidateThumbnail } = require('./thumbnails');
const { getImages } = require('../util'); const { getImages } = require('../util');
const router = new express.Router(); const router = express.Router();
router.post('/all', jsonParser, function (request, response) { router.post('/all', jsonParser, function (request, response) {
var images = getImages('public/backgrounds'); var images = getImages('public/backgrounds');

View File

@ -0,0 +1,22 @@
const path = require('path');
const express = require('express');
const sanitize = require('sanitize-filename');
const writeFileAtomicSync = require('write-file-atomic').sync;
const { jsonParser } = require('../express-common');
const { DIRECTORIES } = require('../constants');
const router = express.Router();
router.post('/save', jsonParser, (request, response) => {
if (!request.body || !request.body.name) {
return response.sendStatus(400);
}
const filename = path.join(DIRECTORIES.movingUI, sanitize(request.body.name) + '.json');
writeFileAtomicSync(filename, JSON.stringify(request.body, null, 4), 'utf8');
return response.sendStatus(200);
});
module.exports = { router };