diff --git a/public/scripts/extensions/stable-diffusion/index.js b/public/scripts/extensions/stable-diffusion/index.js index 5c903e2f0..247dea357 100644 --- a/public/scripts/extensions/stable-diffusion/index.js +++ b/public/scripts/extensions/stable-diffusion/index.js @@ -663,6 +663,19 @@ async function sendGenerationRequest(generationType, prompt, characterName=null, } } +/** + * Generates an "extras" image using a provided prompt and other settings, + * then saves the generated image and either invokes a callback or sends a message with the image. + * + * @param {string} prompt - The main instruction or question used to guide the image generation. + * @param {string} prefix - Additional context or prefix to guide the image generation. + * @param {string} characterName - The name used to determine the sub-directory for saving. + * @param {function} [callback] - Optional callback function invoked with the prompt and saved image. + * If not provided, `sendMessage` is called instead. + * + * @returns {Promise} - A promise that resolves when the image generation and processing are complete. + * The function doesn't explicitly return a value, but based on the results, either a callback is called, a message is sent, or a popup is shown. + */ async function generateExtrasImage(prompt, prefix, characterName, callback) { console.debug(extension_settings.sd); const url = new URL(getApiUrl()); @@ -694,6 +707,19 @@ async function generateExtrasImage(prompt, prefix, characterName, callback) { } } +/** + * Generates a "horde" image using the provided prompt and configuration settings, + * then saves the generated image and either invokes a callback or sends a message with the image. + * + * @param {string} prompt - The main instruction or question used to guide the image generation. + * @param {string} prefix - Additional context or prefix to guide the image generation. + * @param {string} characterName - The name used to determine the sub-directory for saving. + * @param {function} [callback] - Optional callback function invoked with the prompt and saved image. + * If not provided, `sendMessage` is called instead. + * + * @returns {Promise} - A promise that resolves when the image generation and processing are complete. + * The function doesn't explicitly return a value, but based on the results, either a callback is called, a message is sent, or a toastr error is shown. + */ async function generateHordeImage(prompt, prefix, characterName, callback) { const result = await fetch('/horde_generateimage', { method: 'POST', diff --git a/public/scripts/group-chats.js b/public/scripts/group-chats.js index 19033f2c6..f9767c81a 100644 --- a/public/scripts/group-chats.js +++ b/public/scripts/group-chats.js @@ -1135,6 +1135,16 @@ function select_group_chats(groupId, skipAnimation) { eventSource.emit('groupSelected', {detail: {id: openGroupId, group: group}}); } +/** + * Handles the upload and processing of a group avatar. + * The selected image is read, cropped using a popup, processed into a thumbnail, + * and then uploaded to the server. + * + * @param {Event} event - The event triggered by selecting a file input, containing the image file to upload. + * + * @returns {Promise} - A promise that resolves when the processing and upload is complete. + * No return value, but internal state might be updated based on the processing result. + */ async function uploadGroupAvatar(event) { const file = event.target.files[0]; diff --git a/public/scripts/utils.js b/public/scripts/utils.js index da628ad09..b5c6f4595 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -555,6 +555,16 @@ export function extractDataFromPng(data, identifier = 'chara') { } } +/** + * Sends a base64 encoded image to the backend to be saved as a file. + * + * @param {string} base64Data - The base64 encoded image data. + * @param {string} characterName - The character name to determine the sub-directory for saving. + * @param {string} ext - The file extension for the image (e.g., 'jpg', 'png', 'webp'). + * + * @returns {Promise} - Resolves to the saved image's path on the server. + * Rejects with an error if the upload fails. + */ export async function saveBase64AsFile(base64Data, characterName, ext) { // Construct the full data URL const format = ext; // Extract the file extension (jpg, png, webp) diff --git a/server.js b/server.js index 04ec8ee54..4af7ff0e3 100644 --- a/server.js +++ b/server.js @@ -2614,7 +2614,12 @@ app.post('/uploaduseravatar', urlencodedParser, async (request, response) => { }); - +/** + * Ensure the directory for the provided file path exists. + * If not, it will recursively create the directory. + * + * @param {string} filePath - The full path of the file for which the directory should be ensured. + */ function ensureDirectoryExistence(filePath) { const dirname = path.dirname(filePath); if (fs.existsSync(dirname)) { @@ -2624,6 +2629,17 @@ function ensureDirectoryExistence(filePath) { fs.mkdirSync(dirname); } +/** + * Endpoint to handle image uploads. + * The image should be provided in the request body in base64 format. + * Optionally, a character name can be provided to save the image in a sub-folder. + * + * @route POST /uploadimage + * @param {Object} request.body - The request payload. + * @param {string} request.body.image - The base64 encoded image data. + * @param {string} [request.body.ch_name] - Optional character name to determine the sub-directory. + * @returns {Object} response - The response object containing the path where the image was saved. + */ app.post('/uploadimage', jsonParser, async (request, response) => { // Check for image data if (!request.body || !request.body.image) {