Use Express router for presets endpoint
This commit is contained in:
parent
414c9bd5fb
commit
ba74288e4a
|
@ -3582,7 +3582,7 @@ require('./src/endpoints/openai').registerEndpoints(app, jsonParser, urlencodedP
|
|||
require('./src/endpoints/tokenizers').registerEndpoints(app, jsonParser);
|
||||
|
||||
// Preset management
|
||||
require('./src/endpoints/presets').registerEndpoints(app, jsonParser);
|
||||
app.use('/api/presets', require('./src/endpoints/presets').router);
|
||||
|
||||
// Secrets managemenet
|
||||
require('./src/endpoints/secrets').registerEndpoints(app, jsonParser);
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const express = require('express');
|
||||
const sanitize = require('sanitize-filename');
|
||||
const writeFileAtomicSync = require('write-file-atomic').sync;
|
||||
const { DIRECTORIES } = require('../constants');
|
||||
const { getDefaultPresetFile, getDefaultPresets } = require('./content-manager');
|
||||
const { jsonParser } = require('../express-common');
|
||||
|
||||
/**
|
||||
* Gets the folder and extension for the preset settings based on the API source ID.
|
||||
|
@ -30,105 +32,98 @@ function getPresetSettingsByAPI(apiId) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the preset management endpoints.
|
||||
* @param {import('express').Express} app Express app
|
||||
* @param {any} jsonParser JSON parser middleware
|
||||
*/
|
||||
function registerEndpoints(app, jsonParser) {
|
||||
app.post('/api/presets/save', jsonParser, function (request, response) {
|
||||
const name = sanitize(request.body.name);
|
||||
if (!request.body.preset || !name) {
|
||||
return response.sendStatus(400);
|
||||
}
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/save', jsonParser, function (request, response) {
|
||||
const name = sanitize(request.body.name);
|
||||
if (!request.body.preset || !name) {
|
||||
return response.sendStatus(400);
|
||||
}
|
||||
|
||||
const settings = getPresetSettingsByAPI(request.body.apiId);
|
||||
const filename = name + settings.extension;
|
||||
|
||||
if (!settings.folder) {
|
||||
return response.sendStatus(400);
|
||||
}
|
||||
|
||||
const fullpath = path.join(settings.folder, filename);
|
||||
writeFileAtomicSync(fullpath, JSON.stringify(request.body.preset, null, 4), 'utf-8');
|
||||
return response.send({ name });
|
||||
});
|
||||
|
||||
router.post('/delete', jsonParser, function (request, response) {
|
||||
const name = sanitize(request.body.name);
|
||||
if (!name) {
|
||||
return response.sendStatus(400);
|
||||
}
|
||||
|
||||
const settings = getPresetSettingsByAPI(request.body.apiId);
|
||||
const filename = name + settings.extension;
|
||||
|
||||
if (!settings.folder) {
|
||||
return response.sendStatus(400);
|
||||
}
|
||||
|
||||
const fullpath = path.join(settings.folder, filename);
|
||||
|
||||
if (fs.existsSync(fullpath)) {
|
||||
fs.unlinkSync(fullpath);
|
||||
return response.sendStatus(200);
|
||||
} else {
|
||||
return response.sendStatus(404);
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/restore', jsonParser, function (request, response) {
|
||||
try {
|
||||
const settings = getPresetSettingsByAPI(request.body.apiId);
|
||||
const filename = name + settings.extension;
|
||||
|
||||
if (!settings.folder) {
|
||||
return response.sendStatus(400);
|
||||
}
|
||||
|
||||
const fullpath = path.join(settings.folder, filename);
|
||||
writeFileAtomicSync(fullpath, JSON.stringify(request.body.preset, null, 4), 'utf-8');
|
||||
return response.send({ name });
|
||||
});
|
||||
|
||||
app.post('/api/presets/delete', jsonParser, function (request, response) {
|
||||
const name = sanitize(request.body.name);
|
||||
if (!name) {
|
||||
return response.sendStatus(400);
|
||||
const defaultPresets = getDefaultPresets();
|
||||
|
||||
const defaultPreset = defaultPresets.find(p => p.name === name && p.folder === settings.folder);
|
||||
|
||||
const result = { isDefault: false, preset: {} };
|
||||
|
||||
if (defaultPreset) {
|
||||
result.isDefault = true;
|
||||
result.preset = getDefaultPresetFile(defaultPreset.filename) || {};
|
||||
}
|
||||
|
||||
const settings = getPresetSettingsByAPI(request.body.apiId);
|
||||
const filename = name + settings.extension;
|
||||
return response.send(result);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return response.sendStatus(500);
|
||||
}
|
||||
});
|
||||
|
||||
if (!settings.folder) {
|
||||
return response.sendStatus(400);
|
||||
}
|
||||
// TODO: Merge with /api/presets/save
|
||||
router.post('/save-openai', jsonParser, function (request, response) {
|
||||
if (!request.body || typeof request.query.name !== 'string') return response.sendStatus(400);
|
||||
const name = sanitize(request.query.name);
|
||||
if (!name) return response.sendStatus(400);
|
||||
|
||||
const fullpath = path.join(settings.folder, filename);
|
||||
const filename = `${name}.settings`;
|
||||
const fullpath = path.join(DIRECTORIES.openAI_Settings, filename);
|
||||
writeFileAtomicSync(fullpath, JSON.stringify(request.body, null, 4), 'utf-8');
|
||||
return response.send({ name });
|
||||
});
|
||||
|
||||
if (fs.existsSync(fullpath)) {
|
||||
fs.unlinkSync(fullpath);
|
||||
return response.sendStatus(200);
|
||||
} else {
|
||||
return response.sendStatus(404);
|
||||
}
|
||||
});
|
||||
// TODO: Merge with /api/presets/delete
|
||||
router.post('/delete-openai', jsonParser, function (request, response) {
|
||||
if (!request.body || !request.body.name) {
|
||||
return response.sendStatus(400);
|
||||
}
|
||||
|
||||
app.post('/api/presets/restore', jsonParser, function (request, response) {
|
||||
try {
|
||||
const settings = getPresetSettingsByAPI(request.body.apiId);
|
||||
const name = sanitize(request.body.name);
|
||||
const defaultPresets = getDefaultPresets();
|
||||
const name = request.body.name;
|
||||
const pathToFile = path.join(DIRECTORIES.openAI_Settings, `${name}.settings`);
|
||||
|
||||
const defaultPreset = defaultPresets.find(p => p.name === name && p.folder === settings.folder);
|
||||
if (fs.existsSync(pathToFile)) {
|
||||
fs.rmSync(pathToFile);
|
||||
return response.send({ ok: true });
|
||||
}
|
||||
|
||||
const result = { isDefault: false, preset: {} };
|
||||
return response.send({ error: true });
|
||||
});
|
||||
|
||||
if (defaultPreset) {
|
||||
result.isDefault = true;
|
||||
result.preset = getDefaultPresetFile(defaultPreset.filename) || {};
|
||||
}
|
||||
|
||||
return response.send(result);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return response.sendStatus(500);
|
||||
}
|
||||
});
|
||||
|
||||
// TODO: Merge with /api/presets/save
|
||||
app.post('/api/presets/save-openai', jsonParser, function (request, response) {
|
||||
if (!request.body || typeof request.query.name !== 'string') return response.sendStatus(400);
|
||||
const name = sanitize(request.query.name);
|
||||
if (!name) return response.sendStatus(400);
|
||||
|
||||
const filename = `${name}.settings`;
|
||||
const fullpath = path.join(DIRECTORIES.openAI_Settings, filename);
|
||||
writeFileAtomicSync(fullpath, JSON.stringify(request.body, null, 4), 'utf-8');
|
||||
return response.send({ name });
|
||||
});
|
||||
|
||||
// TODO: Merge with /api/presets/delete
|
||||
app.post('/api/presets/delete-openai', jsonParser, function (request, response) {
|
||||
if (!request.body || !request.body.name) {
|
||||
return response.sendStatus(400);
|
||||
}
|
||||
|
||||
const name = request.body.name;
|
||||
const pathToFile = path.join(DIRECTORIES.openAI_Settings, `${name}.settings`);
|
||||
|
||||
if (fs.existsSync(pathToFile)) {
|
||||
fs.rmSync(pathToFile);
|
||||
return response.send({ ok: true });
|
||||
}
|
||||
|
||||
return response.send({ error: true });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
registerEndpoints,
|
||||
};
|
||||
module.exports = { router };
|
||||
|
|
Loading…
Reference in New Issue