Merge pull request #1455 from valadaptive/content-manager-router

Use Express router for content endpoint
This commit is contained in:
Cohee 2023-12-04 21:49:02 +02:00 committed by GitHub
commit 0b13033ab8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 48 deletions

View File

@ -3606,7 +3606,7 @@ app.use('/api/files', require('./src/endpoints/files').router);
require('./src/endpoints/sprites').registerEndpoints(app, jsonParser, urlencodedParser); require('./src/endpoints/sprites').registerEndpoints(app, jsonParser, urlencodedParser);
// Custom content management // Custom content management
require('./src/endpoints/content-manager').registerEndpoints(app, jsonParser); app.use('/api/content', require('./src/endpoints/content-manager').router);
// Stable Diffusion generation // Stable Diffusion generation
require('./src/endpoints/stable-diffusion').registerEndpoints(app, jsonParser); require('./src/endpoints/stable-diffusion').registerEndpoints(app, jsonParser);

View File

@ -1,8 +1,10 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const express = require('express');
const fetch = require('node-fetch').default; const fetch = require('node-fetch').default;
const sanitize = require('sanitize-filename'); const sanitize = require('sanitize-filename');
const { getConfigValue } = require('../util'); const { getConfigValue } = require('../util');
const { jsonParser } = require('../express-common');
const contentDirectory = path.join(process.cwd(), 'default/content'); const contentDirectory = path.join(process.cwd(), 'default/content');
const contentLogPath = path.join(contentDirectory, 'content.log'); const contentLogPath = path.join(contentDirectory, 'content.log');
const contentIndexPath = path.join(contentDirectory, 'index.json'); const contentIndexPath = path.join(contentDirectory, 'index.json');
@ -302,62 +304,57 @@ function parseJannyUrl(url) {
return uuid; return uuid;
} }
/** const router = express.Router();
* Registers endpoints for custom content management
* @param {import('express').Express} app Express app
* @param {any} jsonParser JSON parser middleware
*/
function registerEndpoints(app, jsonParser) {
app.post('/api/content/import', jsonParser, async (request, response) => {
if (!request.body.url) {
return response.sendStatus(400);
}
try { router.post('/import', jsonParser, async (request, response) => {
const url = request.body.url; if (!request.body.url) {
let result; return response.sendStatus(400);
let type; }
const isJannnyContent = url.includes('janitorai'); try {
if (isJannnyContent) { const url = request.body.url;
const uuid = parseJannyUrl(url); let result;
if (!uuid) { let type;
return response.sendStatus(404);
}
type = 'character'; const isJannnyContent = url.includes('janitorai');
result = await downloadJannyCharacter(uuid); if (isJannnyContent) {
} else { const uuid = parseJannyUrl(url);
const chubParsed = parseChubUrl(url); if (!uuid) {
type = chubParsed?.type; return response.sendStatus(404);
if (chubParsed?.type === 'character') {
console.log('Downloading chub character:', chubParsed.id);
result = await downloadChubCharacter(chubParsed.id);
}
else if (chubParsed?.type === 'lorebook') {
console.log('Downloading chub lorebook:', chubParsed.id);
result = await downloadChubLorebook(chubParsed.id);
}
else {
return response.sendStatus(404);
}
} }
if (result.fileType) response.set('Content-Type', result.fileType); type = 'character';
response.set('Content-Disposition', `attachment; filename="${result.fileName}"`); result = await downloadJannyCharacter(uuid);
response.set('X-Custom-Content-Type', type); } else {
return response.send(result.buffer); const chubParsed = parseChubUrl(url);
} catch (error) { type = chubParsed?.type;
console.log('Importing custom content failed', error);
return response.sendStatus(500); if (chubParsed?.type === 'character') {
console.log('Downloading chub character:', chubParsed.id);
result = await downloadChubCharacter(chubParsed.id);
}
else if (chubParsed?.type === 'lorebook') {
console.log('Downloading chub lorebook:', chubParsed.id);
result = await downloadChubLorebook(chubParsed.id);
}
else {
return response.sendStatus(404);
}
} }
});
} if (result.fileType) response.set('Content-Type', result.fileType);
response.set('Content-Disposition', `attachment; filename="${result.fileName}"`);
response.set('X-Custom-Content-Type', type);
return response.send(result.buffer);
} catch (error) {
console.log('Importing custom content failed', error);
return response.sendStatus(500);
}
});
module.exports = { module.exports = {
checkForNewContent, checkForNewContent,
registerEndpoints,
getDefaultPresets, getDefaultPresets,
getDefaultPresetFile, getDefaultPresetFile,
router,
}; };