Merge pull request #1452 from valadaptive/assets-router
Use Express router for assets + "files" endpoints
This commit is contained in:
commit
ddd16c1469
|
@ -2914,7 +2914,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||
let textareaText;
|
||||
if (type !== 'regenerate' && type !== 'swipe' && type !== 'quiet' && !isImpersonate && !dryRun) {
|
||||
is_send_press = true;
|
||||
textareaText = $('#send_textarea').val();
|
||||
textareaText = String($('#send_textarea').val());
|
||||
$('#send_textarea').val('').trigger('input');
|
||||
} else {
|
||||
textareaText = '';
|
||||
|
@ -2960,7 +2960,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||
//*********************************
|
||||
|
||||
//for normal messages sent from user..
|
||||
if ((textareaText != '' || hasPendingFileAttachment()) && !automatic_trigger && type !== 'quiet') {
|
||||
if ((textareaText != '' || hasPendingFileAttachment()) && !automatic_trigger && type !== 'quiet' && !dryRun) {
|
||||
// If user message contains no text other than bias - send as a system message
|
||||
if (messageBias && replaceBiasMarkup(textareaText).trim().length === 0) {
|
||||
sendSystemMessage(system_message_types.GENERIC, ' ', { bias: messageBias });
|
||||
|
@ -2969,7 +2969,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||
await sendMessageAsUser(textareaText, messageBias);
|
||||
}
|
||||
}
|
||||
else if (textareaText == '' && !automatic_trigger && type === undefined && main_api == 'openai' && oai_settings.send_if_empty.trim().length > 0) {
|
||||
else if (textareaText == '' && !automatic_trigger && !dryRun && type === undefined && main_api == 'openai' && oai_settings.send_if_empty.trim().length > 0) {
|
||||
// Use send_if_empty if set and the user message is empty. Only when sending messages normally
|
||||
await sendMessageAsUser(oai_settings.send_if_empty.trim(), messageBias);
|
||||
}
|
||||
|
|
|
@ -152,7 +152,7 @@ export async function populateFileAttachment(message, inputId = 'file_form_input
|
|||
*/
|
||||
export async function uploadFileAttachment(fileName, base64Data) {
|
||||
try {
|
||||
const result = await fetch('/api/file/upload', {
|
||||
const result = await fetch('/api/files/upload', {
|
||||
method: 'POST',
|
||||
headers: getRequestHeaders(),
|
||||
body: JSON.stringify({
|
||||
|
@ -302,7 +302,7 @@ async function viewMessageFile(messageId) {
|
|||
modalTemplate.addClass('file_modal');
|
||||
addCopyToCodeBlocks(modalTemplate);
|
||||
|
||||
callPopup(modalTemplate, 'text');
|
||||
callPopup(modalTemplate, 'text', '', { wide: true, large: true });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -3597,7 +3597,10 @@ require('./src/endpoints/novelai').registerEndpoints(app, jsonParser);
|
|||
require('./src/endpoints/extensions').registerEndpoints(app, jsonParser);
|
||||
|
||||
// Asset management
|
||||
require('./src/endpoints/assets').registerEndpoints(app, jsonParser);
|
||||
app.use('/api/assets', require('./src/endpoints/assets').router);
|
||||
|
||||
// File management
|
||||
app.use('/api/files', require('./src/endpoints/files').router);
|
||||
|
||||
// Character sprite management
|
||||
require('./src/endpoints/sprites').registerEndpoints(app, jsonParser, urlencodedParser);
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const express = require('express');
|
||||
const sanitize = require('sanitize-filename');
|
||||
const fetch = require('node-fetch').default;
|
||||
const { finished } = require('stream/promises');
|
||||
const writeFileSyncAtomic = require('write-file-atomic').sync;
|
||||
const { DIRECTORIES, UNSAFE_EXTENSIONS } = require('../constants');
|
||||
const { jsonParser } = require('../express-common');
|
||||
|
||||
const VALID_CATEGORIES = ['bgm', 'ambient', 'blip', 'live2d'];
|
||||
|
||||
|
@ -57,13 +59,9 @@ function getFiles(dir, files = []) {
|
|||
return files;
|
||||
}
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/**
|
||||
* Registers the endpoints for the asset management.
|
||||
* @param {import('express').Express} app Express app
|
||||
* @param {any} jsonParser JSON parser middleware
|
||||
*/
|
||||
function registerEndpoints(app, jsonParser) {
|
||||
/**
|
||||
* HTTP POST handler function to retrieve name of all files of a given folder path.
|
||||
*
|
||||
* @param {Object} request - HTTP Request object. Require folder path in query
|
||||
|
@ -71,7 +69,7 @@ function registerEndpoints(app, jsonParser) {
|
|||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
app.post('/api/assets/get', jsonParser, async (_, response) => {
|
||||
router.post('/get', jsonParser, async (_, response) => {
|
||||
const folderPath = path.join(DIRECTORIES.assets);
|
||||
let output = {};
|
||||
//console.info("Checking files into",folderPath);
|
||||
|
@ -119,9 +117,9 @@ function registerEndpoints(app, jsonParser) {
|
|||
console.log(err);
|
||||
}
|
||||
return response.send(output);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
/**
|
||||
* HTTP POST handler function to download the requested asset.
|
||||
*
|
||||
* @param {Object} request - HTTP Request object, expects a url, a category and a filename.
|
||||
|
@ -129,7 +127,7 @@ function registerEndpoints(app, jsonParser) {
|
|||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
app.post('/api/assets/download', jsonParser, async (request, response) => {
|
||||
router.post('/download', jsonParser, async (request, response) => {
|
||||
const url = request.body.url;
|
||||
const inputCategory = request.body.category;
|
||||
const inputFilename = sanitize(request.body.filename);
|
||||
|
@ -179,9 +177,9 @@ function registerEndpoints(app, jsonParser) {
|
|||
console.log(error);
|
||||
response.sendStatus(500);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
/**
|
||||
* HTTP POST handler function to delete the requested asset.
|
||||
*
|
||||
* @param {Object} request - HTTP Request object, expects a category and a filename
|
||||
|
@ -189,7 +187,7 @@ function registerEndpoints(app, jsonParser) {
|
|||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
app.post('/api/assets/delete', jsonParser, async (request, response) => {
|
||||
router.post('/delete', jsonParser, async (request, response) => {
|
||||
const inputCategory = request.body.category;
|
||||
const inputFilename = sanitize(request.body.filename);
|
||||
|
||||
|
@ -231,10 +229,10 @@ function registerEndpoints(app, jsonParser) {
|
|||
console.log(error);
|
||||
response.sendStatus(500);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
///////////////////////////////
|
||||
/**
|
||||
///////////////////////////////
|
||||
/**
|
||||
* HTTP POST handler function to retrieve a character background music list.
|
||||
*
|
||||
* @param {Object} request - HTTP Request object, expects a character name in the query.
|
||||
|
@ -242,7 +240,7 @@ function registerEndpoints(app, jsonParser) {
|
|||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
app.post('/api/assets/character', jsonParser, async (request, response) => {
|
||||
router.post('/character', jsonParser, async (request, response) => {
|
||||
if (request.query.name === undefined) return response.sendStatus(400);
|
||||
const name = sanitize(request.query.name.toString());
|
||||
const inputCategory = request.query.category;
|
||||
|
@ -295,9 +293,9 @@ function registerEndpoints(app, jsonParser) {
|
|||
console.log(err);
|
||||
return response.sendStatus(500);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/api/file/upload', jsonParser, async (request, response) => {
|
||||
router.post('/upload', jsonParser, async (request, response) => {
|
||||
try {
|
||||
if (!request.body.name) {
|
||||
return response.status(400).send('No upload name specified');
|
||||
|
@ -321,9 +319,6 @@ function registerEndpoints(app, jsonParser) {
|
|||
console.log(error);
|
||||
return response.sendStatus(500);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
registerEndpoints,
|
||||
};
|
||||
module.exports = { router, checkAssetFileName };
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
const path = require('path');
|
||||
const writeFileSyncAtomic = require('write-file-atomic').sync;
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { checkAssetFileName } = require('./assets');
|
||||
const { jsonParser } = require('../express-common');
|
||||
const { DIRECTORIES } = require('../constants');
|
||||
|
||||
router.post('/upload', jsonParser, async (request, response) => {
|
||||
try {
|
||||
if (!request.body.name) {
|
||||
return response.status(400).send('No upload name specified');
|
||||
}
|
||||
|
||||
if (!request.body.data) {
|
||||
return response.status(400).send('No upload data specified');
|
||||
}
|
||||
|
||||
const safeInput = checkAssetFileName(request.body.name);
|
||||
|
||||
if (!safeInput) {
|
||||
return response.status(400).send('Invalid upload name');
|
||||
}
|
||||
|
||||
const pathToUpload = path.join(DIRECTORIES.files, safeInput);
|
||||
writeFileSyncAtomic(pathToUpload, request.body.data, 'base64');
|
||||
const url = path.normalize(pathToUpload.replace('public' + path.sep, ''));
|
||||
return response.send({ path: url });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return response.sendStatus(500);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = { router };
|
Loading…
Reference in New Issue