Move missed endpoints

This commit is contained in:
Cohee 2023-09-16 18:03:31 +03:00
parent 61995bb33f
commit ab9aa28fe4
5 changed files with 82 additions and 77 deletions

View File

@ -917,7 +917,7 @@ async function getSpritesList(name) {
console.debug('getting sprites list');
try {
const result = await fetch(`/get_sprites?name=${encodeURIComponent(name)}`);
const result = await fetch(`/api/sprites/get?name=${encodeURIComponent(name)}`);
let sprites = result.ok ? (await result.json()) : [];
return sprites;
}

View File

@ -2425,14 +2425,16 @@ async function onDeletePresetClick() {
$('#settings_perset_openai').trigger('change');
}
const response = await fetch('/deletepreset_openai', {
const response = await fetch('/api/presets/delete-openai', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ name: nameToDelete }),
});
if (!response.ok) {
console.warn('Preset was not deleted from server');
toastr.warning('Preset was not deleted from server');
} else {
toastr.success('Preset deleted');
}
saveSettingsDebounced();

View File

@ -2701,64 +2701,6 @@ app.post('/deletegroup', jsonParser, async (request, response) => {
return response.send({ ok: true });
});
/**
* Gets the path to the sprites folder for the provided character name
* @param {string} name - The name of the character
* @param {boolean} isSubfolder - Whether the name contains a subfolder
* @returns {string | null} The path to the sprites folder. Null if the name is invalid.
*/
function getSpritesPath(name, isSubfolder) {
if (isSubfolder) {
const nameParts = name.split('/');
const characterName = sanitize(nameParts[0]);
const subfolderName = sanitize(nameParts[1]);
if (!characterName || !subfolderName) {
return null;
}
return path.join(DIRECTORIES.characters, characterName, subfolderName);
}
name = sanitize(name);
if (!name) {
return null;
}
return path.join(DIRECTORIES.characters, name);
}
app.get('/get_sprites', jsonParser, function (request, response) {
const name = String(request.query.name);
const isSubfolder = name.includes('/');
const spritesPath = getSpritesPath(name, isSubfolder);
let sprites = [];
try {
if (spritesPath && fs.existsSync(spritesPath) && fs.statSync(spritesPath).isDirectory()) {
sprites = fs.readdirSync(spritesPath)
.filter(file => {
const mimeType = mime.lookup(file);
return mimeType && mimeType.startsWith('image/');
})
.map((file) => {
const pathToSprite = path.join(spritesPath, file);
return {
label: path.parse(pathToSprite).name.toLowerCase(),
path: `/characters/${name}/${file}`,
};
});
}
}
catch (err) {
console.log(err);
}
finally {
return response.send(sprites);
}
});
function cleanUploads() {
try {
if (fs.existsSync(UPLOADS_PATH)) {
@ -2880,22 +2822,6 @@ app.post("/openai_bias", jsonParser, async function (request, response) {
return response.send(result);
});
app.post("/deletepreset_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 });
});
function convertChatMLPrompt(messages) {
const messageStrings = [];
messages.forEach(m => {

View File

@ -87,6 +87,23 @@ function registerEndpoints(app, jsonParser) {
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 = {

View File

@ -1,10 +1,40 @@
const fs = require('fs');
const path = require('path');
const mime = require('mime-types');
const sanitize = require('sanitize-filename');
const writeFileAtomicSync = require('write-file-atomic').sync;
const { DIRECTORIES, UPLOADS_PATH } = require('./constants');
const { getImageBuffers } = require('./util');
/**
* Gets the path to the sprites folder for the provided character name
* @param {string} name - The name of the character
* @param {boolean} isSubfolder - Whether the name contains a subfolder
* @returns {string | null} The path to the sprites folder. Null if the name is invalid.
*/
function getSpritesPath(name, isSubfolder) {
if (isSubfolder) {
const nameParts = name.split('/');
const characterName = sanitize(nameParts[0]);
const subfolderName = sanitize(nameParts[1]);
if (!characterName || !subfolderName) {
return null;
}
return path.join(DIRECTORIES.characters, characterName, subfolderName);
}
name = sanitize(name);
if (!name) {
return null;
}
return path.join(DIRECTORIES.characters, name);
}
/**
* Imports base64 encoded sprites from RisuAI character data.
* @param {object} data RisuAI character data
@ -78,6 +108,36 @@ function importRisuSprites(data) {
* @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 isSubfolder = name.includes('/');
const spritesPath = getSpritesPath(name, isSubfolder);
let sprites = [];
try {
if (spritesPath && fs.existsSync(spritesPath) && fs.statSync(spritesPath).isDirectory()) {
sprites = fs.readdirSync(spritesPath)
.filter(file => {
const mimeType = mime.lookup(file);
return mimeType && mimeType.startsWith('image/');
})
.map((file) => {
const pathToSprite = path.join(spritesPath, file);
return {
label: path.parse(pathToSprite).name.toLowerCase(),
path: `/characters/${name}/${file}`,
};
});
}
}
catch (err) {
console.log(err);
}
finally {
return response.send(sprites);
}
});
app.post('/api/sprites/delete', jsonParser, async (request, response) => {
const label = request.body.label;
const name = request.body.name;