Use Express router for sprites endpoint

This commit is contained in:
valadaptive 2023-12-04 12:57:13 -05:00
parent 414c9bd5fb
commit 173bc5975f
2 changed files with 136 additions and 140 deletions

View File

@ -3600,7 +3600,7 @@ require('./src/endpoints/extensions').registerEndpoints(app, jsonParser);
require('./src/endpoints/assets').registerEndpoints(app, jsonParser); require('./src/endpoints/assets').registerEndpoints(app, jsonParser);
// Character sprite management // Character sprite management
require('./src/endpoints/sprites').registerEndpoints(app, jsonParser, urlencodedParser); app.use('/api/sprites', require('./src/endpoints/sprites').router);
// Custom content management // Custom content management
require('./src/endpoints/content-manager').registerEndpoints(app, jsonParser); require('./src/endpoints/content-manager').registerEndpoints(app, jsonParser);

View File

@ -1,11 +1,13 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const express = require('express');
const mime = require('mime-types'); const mime = require('mime-types');
const sanitize = require('sanitize-filename'); const sanitize = require('sanitize-filename');
const writeFileAtomicSync = require('write-file-atomic').sync; const writeFileAtomicSync = require('write-file-atomic').sync;
const { DIRECTORIES, UPLOADS_PATH } = require('../constants'); const { DIRECTORIES, UPLOADS_PATH } = require('../constants');
const { getImageBuffers } = require('../util'); const { getImageBuffers } = require('../util');
const { jsonParser, urlencodedParser } = require('../express-common');
/** /**
* Gets the path to the sprites folder for the provided character name * Gets the path to the sprites folder for the provided character name
@ -101,14 +103,9 @@ function importRisuSprites(data) {
} }
} }
/** const router = express.Router();
* Registers the endpoints for the sprite management.
* @param {import('express').Express} app Express app router.get('/get', jsonParser, function (request, response) {
* @param {any} jsonParser JSON parser middleware
* @param {any} urlencodedParser URL encoded parser middleware
*/
function registerEndpoints(app, jsonParser, urlencodedParser) {
app.get('/api/sprites/get', jsonParser, function (request, response) {
const name = String(request.query.name); const name = String(request.query.name);
const isSubfolder = name.includes('/'); const isSubfolder = name.includes('/');
const spritesPath = getSpritesPath(name, isSubfolder); const spritesPath = getSpritesPath(name, isSubfolder);
@ -136,7 +133,7 @@ function registerEndpoints(app, jsonParser, urlencodedParser) {
return response.send(sprites); return response.send(sprites);
}); });
app.post('/api/sprites/delete', jsonParser, async (request, response) => { router.post('/delete', jsonParser, async (request, response) => {
const label = request.body.label; const label = request.body.label;
const name = request.body.name; const name = request.body.name;
@ -168,7 +165,7 @@ function registerEndpoints(app, jsonParser, urlencodedParser) {
} }
}); });
app.post('/api/sprites/upload-zip', urlencodedParser, async (request, response) => { router.post('/upload-zip', urlencodedParser, async (request, response) => {
const file = request.file; const file = request.file;
const name = request.body.name; const name = request.body.name;
@ -215,7 +212,7 @@ function registerEndpoints(app, jsonParser, urlencodedParser) {
} }
}); });
app.post('/api/sprites/upload', urlencodedParser, async (request, response) => { router.post('/upload', urlencodedParser, async (request, response) => {
const file = request.file; const file = request.file;
const label = request.body.label; const label = request.body.label;
const name = request.body.name; const name = request.body.name;
@ -259,9 +256,8 @@ function registerEndpoints(app, jsonParser, urlencodedParser) {
return response.sendStatus(500); return response.sendStatus(500);
} }
}); });
}
module.exports = { module.exports = {
registerEndpoints, router,
importRisuSprites, importRisuSprites,
}; };