Add comments and stuff

This commit is contained in:
city-unit 2023-08-20 00:15:57 -04:00
parent 24da113670
commit 3ce8343016
4 changed files with 63 additions and 1 deletions

View File

@ -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<void>} - 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<void>} - 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',

View File

@ -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<void>} - 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];

View File

@ -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<string>} - 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)

View File

@ -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) {