mirror of
				https://github.com/SillyTavern/SillyTavern.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	Merge branch 'staging' into novelai-router
This commit is contained in:
		| @@ -58,6 +58,7 @@ module.exports = { | |||||||
|         'comma-dangle': ['error', 'always-multiline'], |         'comma-dangle': ['error', 'always-multiline'], | ||||||
|         'eol-last': ['error', 'always'], |         'eol-last': ['error', 'always'], | ||||||
|         'no-trailing-spaces': 'error', |         'no-trailing-spaces': 'error', | ||||||
|  |         'object-curly-spacing': ['error', 'always'], | ||||||
|  |  | ||||||
|         // These rules should eventually be enabled. |         // These rules should eventually be enabled. | ||||||
|         'no-async-promise-executor': 'off', |         'no-async-promise-executor': 'off', | ||||||
|   | |||||||
| @@ -887,7 +887,7 @@ async function getStatus() { | |||||||
|                 api_type: textgen_settings.type, |                 api_type: textgen_settings.type, | ||||||
|                 legacy_api: main_api == 'textgenerationwebui' ? |                 legacy_api: main_api == 'textgenerationwebui' ? | ||||||
|                     textgen_settings.legacy_api && |                     textgen_settings.legacy_api && | ||||||
|                         textgen_settings.type !== MANCER : |                     textgen_settings.type !== MANCER : | ||||||
|                     false, |                     false, | ||||||
|             }), |             }), | ||||||
|             signal: abortStatusCheck.signal, |             signal: abortStatusCheck.signal, | ||||||
| @@ -2914,7 +2914,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, | |||||||
|         let textareaText; |         let textareaText; | ||||||
|         if (type !== 'regenerate' && type !== 'swipe' && type !== 'quiet' && !isImpersonate && !dryRun) { |         if (type !== 'regenerate' && type !== 'swipe' && type !== 'quiet' && !isImpersonate && !dryRun) { | ||||||
|             is_send_press = true; |             is_send_press = true; | ||||||
|             textareaText = $('#send_textarea').val(); |             textareaText = String($('#send_textarea').val()); | ||||||
|             $('#send_textarea').val('').trigger('input'); |             $('#send_textarea').val('').trigger('input'); | ||||||
|         } else { |         } else { | ||||||
|             textareaText = ''; |             textareaText = ''; | ||||||
| @@ -2960,7 +2960,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, | |||||||
|         //********************************* |         //********************************* | ||||||
|  |  | ||||||
|         //for normal messages sent from user.. |         //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 user message contains no text other than bias - send as a system message | ||||||
|             if (messageBias && replaceBiasMarkup(textareaText).trim().length === 0) { |             if (messageBias && replaceBiasMarkup(textareaText).trim().length === 0) { | ||||||
|                 sendSystemMessage(system_message_types.GENERIC, ' ', { bias: messageBias }); |                 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); |                 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 |             // 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); |             await sendMessageAsUser(oai_settings.send_if_empty.trim(), messageBias); | ||||||
|         } |         } | ||||||
| @@ -4154,11 +4154,11 @@ async function DupeChar() { | |||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const confirm = await callPopup(` |     const confirmMessage = ` | ||||||
|         <h3>Are you sure you want to duplicate this character?</h3> |     <h3>Are you sure you want to duplicate this character?</h3> | ||||||
|         <span>If you just want to start a new chat with the same character, use "Start new chat" option in the bottom-left options menu.</span><br><br>`, |     <span>If you just want to start a new chat with the same character, use "Start new chat" option in the bottom-left options menu.</span><br><br>`; | ||||||
|         'confirm', |  | ||||||
|     ); |     const confirm = await callPopup(confirmMessage, 'confirm'); | ||||||
|  |  | ||||||
|     if (!confirm) { |     if (!confirm) { | ||||||
|         console.log('User cancelled duplication'); |         console.log('User cancelled duplication'); | ||||||
| @@ -7631,10 +7631,7 @@ function doTogglePanels() { | |||||||
| } | } | ||||||
|  |  | ||||||
| function addDebugFunctions() { | function addDebugFunctions() { | ||||||
|     registerDebugFunction('backfillTokenCounts', 'Backfill token counters', |     const doBackfill = async () => { | ||||||
|         `Recalculates token counts of all messages in the current chat to refresh the counters. |  | ||||||
|         Useful when you switch between models that have different tokenizers. |  | ||||||
|         This is a visual change only. Your chat will be reloaded.`, async () => { |  | ||||||
|         for (const message of chat) { |         for (const message of chat) { | ||||||
|             // System messages are not counted |             // System messages are not counted | ||||||
|             if (message.is_system) { |             if (message.is_system) { | ||||||
| @@ -7650,7 +7647,12 @@ function addDebugFunctions() { | |||||||
|  |  | ||||||
|         await saveChatConditional(); |         await saveChatConditional(); | ||||||
|         await reloadCurrentChat(); |         await reloadCurrentChat(); | ||||||
|     }); |     }; | ||||||
|  |  | ||||||
|  |     registerDebugFunction('backfillTokenCounts', 'Backfill token counters', | ||||||
|  |         `Recalculates token counts of all messages in the current chat to refresh the counters. | ||||||
|  |         Useful when you switch between models that have different tokenizers. | ||||||
|  |         This is a visual change only. Your chat will be reloaded.`, doBackfill); | ||||||
|  |  | ||||||
|     registerDebugFunction('generationTest', 'Send a generation request', 'Generates text using the currently selected API.', async () => { |     registerDebugFunction('generationTest', 'Send a generation request', 'Generates text using the currently selected API.', async () => { | ||||||
|         const text = prompt('Input text:', 'Hello'); |         const text = prompt('Input text:', 'Hello'); | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| import { characters, getCharacters, handleDeleteCharacter, callPopup } from '../script.js'; | import { characters, getCharacters, handleDeleteCharacter, callPopup } from '../script.js'; | ||||||
| import {BulkEditOverlay, BulkEditOverlayState} from './BulkEditOverlay.js'; | import { BulkEditOverlay, BulkEditOverlayState } from './BulkEditOverlay.js'; | ||||||
|  |  | ||||||
|  |  | ||||||
| let is_bulk_edit = false; | let is_bulk_edit = false; | ||||||
|   | |||||||
| @@ -152,7 +152,7 @@ export async function populateFileAttachment(message, inputId = 'file_form_input | |||||||
|  */ |  */ | ||||||
| export async function uploadFileAttachment(fileName, base64Data) { | export async function uploadFileAttachment(fileName, base64Data) { | ||||||
|     try { |     try { | ||||||
|         const result = await fetch('/api/file/upload', { |         const result = await fetch('/api/files/upload', { | ||||||
|             method: 'POST', |             method: 'POST', | ||||||
|             headers: getRequestHeaders(), |             headers: getRequestHeaders(), | ||||||
|             body: JSON.stringify({ |             body: JSON.stringify({ | ||||||
| @@ -302,7 +302,7 @@ async function viewMessageFile(messageId) { | |||||||
|     modalTemplate.addClass('file_modal'); |     modalTemplate.addClass('file_modal'); | ||||||
|     addCopyToCodeBlocks(modalTemplate); |     addCopyToCodeBlocks(modalTemplate); | ||||||
|  |  | ||||||
|     callPopup(modalTemplate, 'text'); |     callPopup(modalTemplate, 'text', '', { wide: true, large: true }); | ||||||
| } | } | ||||||
|  |  | ||||||
| /** | /** | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| export {translate}; | export { translate }; | ||||||
|  |  | ||||||
| import { | import { | ||||||
|     callPopup, |     callPopup, | ||||||
|   | |||||||
| @@ -10,7 +10,7 @@ import { NovelTtsProvider } from './novel.js'; | |||||||
| import { power_user } from '../../power-user.js'; | import { power_user } from '../../power-user.js'; | ||||||
| import { registerSlashCommand } from '../../slash-commands.js'; | import { registerSlashCommand } from '../../slash-commands.js'; | ||||||
| import { OpenAITtsProvider } from './openai.js'; | import { OpenAITtsProvider } from './openai.js'; | ||||||
| import {XTTSTtsProvider} from './xtts.js'; | import { XTTSTtsProvider } from './xtts.js'; | ||||||
| export { talkingAnimation }; | export { talkingAnimation }; | ||||||
|  |  | ||||||
| const UPDATE_INTERVAL = 1000; | const UPDATE_INTERVAL = 1000; | ||||||
|   | |||||||
| @@ -125,7 +125,7 @@ class NovelTtsProvider { | |||||||
|             throw 'TTS Voice name not provided'; |             throw 'TTS Voice name not provided'; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         return { name: voiceName, voice_id: voiceName, lang: 'en-US', preview_url: false}; |         return { name: voiceName, voice_id: voiceName, lang: 'en-US', preview_url: false }; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async generateTts(text, voiceId) { |     async generateTts(text, voiceId) { | ||||||
|   | |||||||
| @@ -666,7 +666,7 @@ async function CreateZenSliders(elmnt) { | |||||||
|         min: sliderMin, |         min: sliderMin, | ||||||
|         max: sliderMax, |         max: sliderMax, | ||||||
|         create: async function () { |         create: async function () { | ||||||
|             await delay(100) |             await delay(100); | ||||||
|             var handle = $(this).find('.ui-slider-handle'); |             var handle = $(this).find('.ui-slider-handle'); | ||||||
|             var handleText, stepNumber, leftMargin; |             var handleText, stepNumber, leftMargin; | ||||||
|  |  | ||||||
| @@ -711,7 +711,7 @@ async function CreateZenSliders(elmnt) { | |||||||
|                 stepNumber = ((sliderValue - sliderMin) / stepScale); |                 stepNumber = ((sliderValue - sliderMin) / stepScale); | ||||||
|                 leftMargin = (stepNumber / numSteps) * 50 * -1; |                 leftMargin = (stepNumber / numSteps) * 50 * -1; | ||||||
|                 originalSlider.val(numVal) |                 originalSlider.val(numVal) | ||||||
|                     .data('newSlider', newSlider) |                     .data('newSlider', newSlider); | ||||||
|                 //console.log(`${newSlider.attr('id')} sliderValue = ${sliderValue}, handleText:${handleText, numVal}, stepNum:${stepNumber}, numSteps:${numSteps}, left-margin:${leftMargin}`) |                 //console.log(`${newSlider.attr('id')} sliderValue = ${sliderValue}, handleText:${handleText, numVal}, stepNum:${stepNumber}, numSteps:${numSteps}, left-margin:${leftMargin}`) | ||||||
|                 var isManualInput = false; |                 var isManualInput = false; | ||||||
|                 var valueBeforeManualInput; |                 var valueBeforeManualInput; | ||||||
| @@ -737,8 +737,8 @@ async function CreateZenSliders(elmnt) { | |||||||
|                         isManualInput = true; |                         isManualInput = true; | ||||||
|                         //allow enter to trigger slider update |                         //allow enter to trigger slider update | ||||||
|                         if (e.key === 'Enter') { |                         if (e.key === 'Enter') { | ||||||
|                             e.preventDefault |                             e.preventDefault; | ||||||
|                             handle.trigger('blur') |                             handle.trigger('blur'); | ||||||
|                         } |                         } | ||||||
|                     }) |                     }) | ||||||
|                     //trigger slider changes when user clicks away |                     //trigger slider changes when user clicks away | ||||||
|   | |||||||
							
								
								
									
										15
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								server.js
									
									
									
									
									
								
							| @@ -3594,22 +3594,25 @@ require('./src/endpoints/thumbnails').registerEndpoints(app, jsonParser); | |||||||
| app.use('/api/novelai', require('./src/endpoints/novelai').router); | app.use('/api/novelai', require('./src/endpoints/novelai').router); | ||||||
|  |  | ||||||
| // Third-party extensions | // Third-party extensions | ||||||
| require('./src/endpoints/extensions').registerEndpoints(app, jsonParser); | app.use('/api/extensions', require('./src/endpoints/extensions').router); | ||||||
|  |  | ||||||
| // Asset management | // 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 | // Character sprite management | ||||||
| require('./src/endpoints/sprites').registerEndpoints(app, jsonParser, urlencodedParser); | require('./src/endpoints/sprites').registerEndpoints(app, jsonParser, urlencodedParser); | ||||||
|  |  | ||||||
| // Custom content management | // Custom content management | ||||||
| require('./src/endpoints/content-manager').registerEndpoints(app, jsonParser); | app.use('/api/content', require('./src/endpoints/content-manager').router); | ||||||
|  |  | ||||||
| // Stable Diffusion generation | // Stable Diffusion generation | ||||||
| require('./src/endpoints/stable-diffusion').registerEndpoints(app, jsonParser); | require('./src/endpoints/stable-diffusion').registerEndpoints(app, jsonParser); | ||||||
|  |  | ||||||
| // LLM and SD Horde generation | // LLM and SD Horde generation | ||||||
| require('./src/endpoints/horde').registerEndpoints(app, jsonParser); | app.use('/api/horde', require('./src/endpoints/horde').router); | ||||||
|  |  | ||||||
| // Vector storage DB | // Vector storage DB | ||||||
| require('./src/endpoints/vectors').registerEndpoints(app, jsonParser); | require('./src/endpoints/vectors').registerEndpoints(app, jsonParser); | ||||||
| @@ -3618,10 +3621,10 @@ require('./src/endpoints/vectors').registerEndpoints(app, jsonParser); | |||||||
| require('./src/endpoints/translate').registerEndpoints(app, jsonParser); | require('./src/endpoints/translate').registerEndpoints(app, jsonParser); | ||||||
|  |  | ||||||
| // Emotion classification | // Emotion classification | ||||||
| require('./src/endpoints/classify').registerEndpoints(app, jsonParser); | app.use('/api/extra/classify', require('./src/endpoints/classify').router); | ||||||
|  |  | ||||||
| // Image captioning | // Image captioning | ||||||
| require('./src/endpoints/caption').registerEndpoints(app, jsonParser); | app.use('/api/extra/caption', require('./src/endpoints/caption').router); | ||||||
|  |  | ||||||
| // Web search extension | // Web search extension | ||||||
| require('./src/endpoints/serpapi').registerEndpoints(app, jsonParser); | require('./src/endpoints/serpapi').registerEndpoints(app, jsonParser); | ||||||
|   | |||||||
| @@ -1,10 +1,12 @@ | |||||||
| const path = require('path'); | const path = require('path'); | ||||||
| const fs = require('fs'); | const fs = require('fs'); | ||||||
|  | const express = require('express'); | ||||||
| const sanitize = require('sanitize-filename'); | const sanitize = require('sanitize-filename'); | ||||||
| const fetch = require('node-fetch').default; | const fetch = require('node-fetch').default; | ||||||
| const { finished } = require('stream/promises'); | const { finished } = require('stream/promises'); | ||||||
| const writeFileSyncAtomic = require('write-file-atomic').sync; | const writeFileSyncAtomic = require('write-file-atomic').sync; | ||||||
| const { DIRECTORIES, UNSAFE_EXTENSIONS } = require('../constants'); | const { DIRECTORIES, UNSAFE_EXTENSIONS } = require('../constants'); | ||||||
|  | const { jsonParser } = require('../express-common'); | ||||||
|  |  | ||||||
| const VALID_CATEGORIES = ['bgm', 'ambient', 'blip', 'live2d']; | const VALID_CATEGORIES = ['bgm', 'ambient', 'blip', 'live2d']; | ||||||
|  |  | ||||||
| @@ -57,273 +59,266 @@ function getFiles(dir, files = []) { | |||||||
|     return files; |     return files; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | const router = express.Router(); | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Registers the endpoints for the asset management. |  * HTTP POST handler function to retrieve name of all files of a given folder path. | ||||||
|  * @param {import('express').Express} app Express app |  * | ||||||
|  * @param {any} jsonParser JSON parser middleware |  * @param {Object} request - HTTP Request object. Require folder path in query | ||||||
|  |  * @param {Object} response - HTTP Response object will contain a list of file path. | ||||||
|  |  * | ||||||
|  |  * @returns {void} | ||||||
|  */ |  */ | ||||||
| function registerEndpoints(app, jsonParser) { | router.post('/get', jsonParser, async (_, response) => { | ||||||
|     /** |     const folderPath = path.join(DIRECTORIES.assets); | ||||||
|      * HTTP POST handler function to retrieve name of all files of a given folder path. |     let output = {}; | ||||||
|      * |     //console.info("Checking files into",folderPath); | ||||||
|      * @param {Object} request - HTTP Request object. Require folder path in query |  | ||||||
|      * @param {Object} response - HTTP Response object will contain a list of file path. |  | ||||||
|      * |  | ||||||
|      * @returns {void} |  | ||||||
|      */ |  | ||||||
|     app.post('/api/assets/get', jsonParser, async (_, response) => { |  | ||||||
|         const folderPath = path.join(DIRECTORIES.assets); |  | ||||||
|         let output = {}; |  | ||||||
|         //console.info("Checking files into",folderPath); |  | ||||||
|  |  | ||||||
|         try { |     try { | ||||||
|             if (fs.existsSync(folderPath) && fs.statSync(folderPath).isDirectory()) { |         if (fs.existsSync(folderPath) && fs.statSync(folderPath).isDirectory()) { | ||||||
|                 const folders = fs.readdirSync(folderPath) |             const folders = fs.readdirSync(folderPath) | ||||||
|                     .filter(filename => { |                 .filter(filename => { | ||||||
|                         return fs.statSync(path.join(folderPath, filename)).isDirectory(); |                     return fs.statSync(path.join(folderPath, filename)).isDirectory(); | ||||||
|                     }); |  | ||||||
|  |  | ||||||
|                 for (const folder of folders) { |  | ||||||
|                     if (folder == 'temp') |  | ||||||
|                         continue; |  | ||||||
|  |  | ||||||
|                     // Live2d assets |  | ||||||
|                     if (folder == 'live2d') { |  | ||||||
|                         output[folder] = []; |  | ||||||
|                         const live2d_folder = path.normalize(path.join(folderPath, folder)); |  | ||||||
|                         const files = getFiles(live2d_folder); |  | ||||||
|                         //console.debug("FILE FOUND:",files) |  | ||||||
|                         for (let file of files) { |  | ||||||
|                             file = path.normalize(file.replace('public' + path.sep, '')); |  | ||||||
|                             if (file.includes('model') && file.endsWith('.json')) { |  | ||||||
|                                 //console.debug("Asset live2d model found:",file) |  | ||||||
|                                 output[folder].push(path.normalize(path.join(file))); |  | ||||||
|                             } |  | ||||||
|                         } |  | ||||||
|                         continue; |  | ||||||
|                     } |  | ||||||
|  |  | ||||||
|                     // Other assets (bgm/ambient/blip) |  | ||||||
|                     const files = fs.readdirSync(path.join(folderPath, folder)) |  | ||||||
|                         .filter(filename => { |  | ||||||
|                             return filename != '.placeholder'; |  | ||||||
|                         }); |  | ||||||
|                     output[folder] = []; |  | ||||||
|                     for (const file of files) { |  | ||||||
|                         output[folder].push(path.join('assets', folder, file)); |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|         catch (err) { |  | ||||||
|             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. |  | ||||||
|      * @param {Object} response - HTTP Response only gives status. |  | ||||||
|      * |  | ||||||
|      * @returns {void} |  | ||||||
|      */ |  | ||||||
|     app.post('/api/assets/download', jsonParser, async (request, response) => { |  | ||||||
|         const url = request.body.url; |  | ||||||
|         const inputCategory = request.body.category; |  | ||||||
|         const inputFilename = sanitize(request.body.filename); |  | ||||||
|  |  | ||||||
|         // Check category |  | ||||||
|         let category = null; |  | ||||||
|         for (let i of VALID_CATEGORIES) |  | ||||||
|             if (i == inputCategory) |  | ||||||
|                 category = i; |  | ||||||
|  |  | ||||||
|         if (category === null) { |  | ||||||
|             console.debug('Bad request: unsuported asset category.'); |  | ||||||
|             return response.sendStatus(400); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         // Sanitize filename |  | ||||||
|         const safe_input = checkAssetFileName(inputFilename); |  | ||||||
|         if (safe_input == '') |  | ||||||
|             return response.sendStatus(400); |  | ||||||
|  |  | ||||||
|         const temp_path = path.join(DIRECTORIES.assets, 'temp', safe_input); |  | ||||||
|         const file_path = path.join(DIRECTORIES.assets, category, safe_input); |  | ||||||
|         console.debug('Request received to download', url, 'to', file_path); |  | ||||||
|  |  | ||||||
|         try { |  | ||||||
|             // Download to temp |  | ||||||
|             const res = await fetch(url); |  | ||||||
|             if (!res.ok || res.body === null) { |  | ||||||
|                 throw new Error(`Unexpected response ${res.statusText}`); |  | ||||||
|             } |  | ||||||
|             const destination = path.resolve(temp_path); |  | ||||||
|             // Delete if previous download failed |  | ||||||
|             if (fs.existsSync(temp_path)) { |  | ||||||
|                 fs.unlink(temp_path, (err) => { |  | ||||||
|                     if (err) throw err; |  | ||||||
|                 }); |                 }); | ||||||
|             } |  | ||||||
|             const fileStream = fs.createWriteStream(destination, { flags: 'wx' }); |  | ||||||
|             await finished(res.body.pipe(fileStream)); |  | ||||||
|  |  | ||||||
|             // Move into asset place |             for (const folder of folders) { | ||||||
|             console.debug('Download finished, moving file from', temp_path, 'to', file_path); |                 if (folder == 'temp') | ||||||
|             fs.renameSync(temp_path, file_path); |                     continue; | ||||||
|             response.sendStatus(200); |  | ||||||
|         } |  | ||||||
|         catch (error) { |  | ||||||
|             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 |  | ||||||
|      * @param {Object} response - HTTP Response only gives stats. |  | ||||||
|      * |  | ||||||
|      * @returns {void} |  | ||||||
|      */ |  | ||||||
|     app.post('/api/assets/delete', jsonParser, async (request, response) => { |  | ||||||
|         const inputCategory = request.body.category; |  | ||||||
|         const inputFilename = sanitize(request.body.filename); |  | ||||||
|  |  | ||||||
|         // Check category |  | ||||||
|         let category = null; |  | ||||||
|         for (let i of VALID_CATEGORIES) |  | ||||||
|             if (i == inputCategory) |  | ||||||
|                 category = i; |  | ||||||
|  |  | ||||||
|         if (category === null) { |  | ||||||
|             console.debug('Bad request: unsuported asset category.'); |  | ||||||
|             return response.sendStatus(400); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         // Sanitize filename |  | ||||||
|         const safe_input = checkAssetFileName(inputFilename); |  | ||||||
|         if (safe_input == '') |  | ||||||
|             return response.sendStatus(400); |  | ||||||
|  |  | ||||||
|         const file_path = path.join(DIRECTORIES.assets, category, safe_input); |  | ||||||
|         console.debug('Request received to delete', category, file_path); |  | ||||||
|  |  | ||||||
|         try { |  | ||||||
|             // Delete if previous download failed |  | ||||||
|             if (fs.existsSync(file_path)) { |  | ||||||
|                 fs.unlink(file_path, (err) => { |  | ||||||
|                     if (err) throw err; |  | ||||||
|                 }); |  | ||||||
|                 console.debug('Asset deleted.'); |  | ||||||
|             } |  | ||||||
|             else { |  | ||||||
|                 console.debug('Asset not found.'); |  | ||||||
|                 response.sendStatus(400); |  | ||||||
|             } |  | ||||||
|             // Move into asset place |  | ||||||
|             response.sendStatus(200); |  | ||||||
|         } |  | ||||||
|         catch (error) { |  | ||||||
|             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. |  | ||||||
|      * @param {Object} response - HTTP Response object will contain a list of audio file path. |  | ||||||
|      * |  | ||||||
|      * @returns {void} |  | ||||||
|      */ |  | ||||||
|     app.post('/api/assets/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; |  | ||||||
|  |  | ||||||
|         // Check category |  | ||||||
|         let category = null; |  | ||||||
|         for (let i of VALID_CATEGORIES) |  | ||||||
|             if (i == inputCategory) |  | ||||||
|                 category = i; |  | ||||||
|  |  | ||||||
|         if (category === null) { |  | ||||||
|             console.debug('Bad request: unsuported asset category.'); |  | ||||||
|             return response.sendStatus(400); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         const folderPath = path.join(DIRECTORIES.characters, name, category); |  | ||||||
|  |  | ||||||
|         let output = []; |  | ||||||
|         try { |  | ||||||
|             if (fs.existsSync(folderPath) && fs.statSync(folderPath).isDirectory()) { |  | ||||||
|  |  | ||||||
|                 // Live2d assets |                 // Live2d assets | ||||||
|                 if (category == 'live2d') { |                 if (folder == 'live2d') { | ||||||
|                     const folders = fs.readdirSync(folderPath); |                     output[folder] = []; | ||||||
|                     for (let modelFolder of folders) { |                     const live2d_folder = path.normalize(path.join(folderPath, folder)); | ||||||
|                         const live2dModelPath = path.join(folderPath, modelFolder); |                     const files = getFiles(live2d_folder); | ||||||
|                         if (fs.statSync(live2dModelPath).isDirectory()) { |                     //console.debug("FILE FOUND:",files) | ||||||
|                             for (let file of fs.readdirSync(live2dModelPath)) { |                     for (let file of files) { | ||||||
|                                 //console.debug("Character live2d model found:", file) |                         file = path.normalize(file.replace('public' + path.sep, '')); | ||||||
|                                 if (file.includes('model') && file.endsWith('.json')) |                         if (file.includes('model') && file.endsWith('.json')) { | ||||||
|                                     output.push(path.join('characters', name, category, modelFolder, file)); |                             //console.debug("Asset live2d model found:",file) | ||||||
|                             } |                             output[folder].push(path.normalize(path.join(file))); | ||||||
|                         } |                         } | ||||||
|                     } |                     } | ||||||
|                     return response.send(output); |                     continue; | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 // Other assets |                 // Other assets (bgm/ambient/blip) | ||||||
|                 const files = fs.readdirSync(folderPath) |                 const files = fs.readdirSync(path.join(folderPath, folder)) | ||||||
|                     .filter(filename => { |                     .filter(filename => { | ||||||
|                         return filename != '.placeholder'; |                         return filename != '.placeholder'; | ||||||
|                     }); |                     }); | ||||||
|  |                 output[folder] = []; | ||||||
|                 for (let i of files) |                 for (const file of files) { | ||||||
|                     output.push(`/characters/${name}/${category}/${i}`); |                     output[folder].push(path.join('assets', folder, file)); | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|             return response.send(output); |  | ||||||
|         } |         } | ||||||
|         catch (err) { |     } | ||||||
|             console.log(err); |     catch (err) { | ||||||
|             return response.sendStatus(500); |         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. | ||||||
|  |  * @param {Object} response - HTTP Response only gives status. | ||||||
|  |  * | ||||||
|  |  * @returns {void} | ||||||
|  |  */ | ||||||
|  | router.post('/download', jsonParser, async (request, response) => { | ||||||
|  |     const url = request.body.url; | ||||||
|  |     const inputCategory = request.body.category; | ||||||
|  |     const inputFilename = sanitize(request.body.filename); | ||||||
|  |  | ||||||
|  |     // Check category | ||||||
|  |     let category = null; | ||||||
|  |     for (let i of VALID_CATEGORIES) | ||||||
|  |         if (i == inputCategory) | ||||||
|  |             category = i; | ||||||
|  |  | ||||||
|  |     if (category === null) { | ||||||
|  |         console.debug('Bad request: unsuported asset category.'); | ||||||
|  |         return response.sendStatus(400); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Sanitize filename | ||||||
|  |     const safe_input = checkAssetFileName(inputFilename); | ||||||
|  |     if (safe_input == '') | ||||||
|  |         return response.sendStatus(400); | ||||||
|  |  | ||||||
|  |     const temp_path = path.join(DIRECTORIES.assets, 'temp', safe_input); | ||||||
|  |     const file_path = path.join(DIRECTORIES.assets, category, safe_input); | ||||||
|  |     console.debug('Request received to download', url, 'to', file_path); | ||||||
|  |  | ||||||
|  |     try { | ||||||
|  |         // Download to temp | ||||||
|  |         const res = await fetch(url); | ||||||
|  |         if (!res.ok || res.body === null) { | ||||||
|  |             throw new Error(`Unexpected response ${res.statusText}`); | ||||||
|         } |         } | ||||||
|     }); |         const destination = path.resolve(temp_path); | ||||||
|  |         // Delete if previous download failed | ||||||
|     app.post('/api/file/upload', jsonParser, async (request, response) => { |         if (fs.existsSync(temp_path)) { | ||||||
|         try { |             fs.unlink(temp_path, (err) => { | ||||||
|             if (!request.body.name) { |                 if (err) throw err; | ||||||
|                 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); |  | ||||||
|         } |         } | ||||||
|     }); |         const fileStream = fs.createWriteStream(destination, { flags: 'wx' }); | ||||||
| } |         await finished(res.body.pipe(fileStream)); | ||||||
|  |  | ||||||
| module.exports = { |         // Move into asset place | ||||||
|     registerEndpoints, |         console.debug('Download finished, moving file from', temp_path, 'to', file_path); | ||||||
| }; |         fs.renameSync(temp_path, file_path); | ||||||
|  |         response.sendStatus(200); | ||||||
|  |     } | ||||||
|  |     catch (error) { | ||||||
|  |         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 | ||||||
|  |  * @param {Object} response - HTTP Response only gives stats. | ||||||
|  |  * | ||||||
|  |  * @returns {void} | ||||||
|  |  */ | ||||||
|  | router.post('/delete', jsonParser, async (request, response) => { | ||||||
|  |     const inputCategory = request.body.category; | ||||||
|  |     const inputFilename = sanitize(request.body.filename); | ||||||
|  |  | ||||||
|  |     // Check category | ||||||
|  |     let category = null; | ||||||
|  |     for (let i of VALID_CATEGORIES) | ||||||
|  |         if (i == inputCategory) | ||||||
|  |             category = i; | ||||||
|  |  | ||||||
|  |     if (category === null) { | ||||||
|  |         console.debug('Bad request: unsuported asset category.'); | ||||||
|  |         return response.sendStatus(400); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Sanitize filename | ||||||
|  |     const safe_input = checkAssetFileName(inputFilename); | ||||||
|  |     if (safe_input == '') | ||||||
|  |         return response.sendStatus(400); | ||||||
|  |  | ||||||
|  |     const file_path = path.join(DIRECTORIES.assets, category, safe_input); | ||||||
|  |     console.debug('Request received to delete', category, file_path); | ||||||
|  |  | ||||||
|  |     try { | ||||||
|  |         // Delete if previous download failed | ||||||
|  |         if (fs.existsSync(file_path)) { | ||||||
|  |             fs.unlink(file_path, (err) => { | ||||||
|  |                 if (err) throw err; | ||||||
|  |             }); | ||||||
|  |             console.debug('Asset deleted.'); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             console.debug('Asset not found.'); | ||||||
|  |             response.sendStatus(400); | ||||||
|  |         } | ||||||
|  |         // Move into asset place | ||||||
|  |         response.sendStatus(200); | ||||||
|  |     } | ||||||
|  |     catch (error) { | ||||||
|  |         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. | ||||||
|  |  * @param {Object} response - HTTP Response object will contain a list of audio file path. | ||||||
|  |  * | ||||||
|  |  * @returns {void} | ||||||
|  |  */ | ||||||
|  | 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; | ||||||
|  |  | ||||||
|  |     // Check category | ||||||
|  |     let category = null; | ||||||
|  |     for (let i of VALID_CATEGORIES) | ||||||
|  |         if (i == inputCategory) | ||||||
|  |             category = i; | ||||||
|  |  | ||||||
|  |     if (category === null) { | ||||||
|  |         console.debug('Bad request: unsuported asset category.'); | ||||||
|  |         return response.sendStatus(400); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     const folderPath = path.join(DIRECTORIES.characters, name, category); | ||||||
|  |  | ||||||
|  |     let output = []; | ||||||
|  |     try { | ||||||
|  |         if (fs.existsSync(folderPath) && fs.statSync(folderPath).isDirectory()) { | ||||||
|  |  | ||||||
|  |             // Live2d assets | ||||||
|  |             if (category == 'live2d') { | ||||||
|  |                 const folders = fs.readdirSync(folderPath); | ||||||
|  |                 for (let modelFolder of folders) { | ||||||
|  |                     const live2dModelPath = path.join(folderPath, modelFolder); | ||||||
|  |                     if (fs.statSync(live2dModelPath).isDirectory()) { | ||||||
|  |                         for (let file of fs.readdirSync(live2dModelPath)) { | ||||||
|  |                             //console.debug("Character live2d model found:", file) | ||||||
|  |                             if (file.includes('model') && file.endsWith('.json')) | ||||||
|  |                                 output.push(path.join('characters', name, category, modelFolder, file)); | ||||||
|  |                         } | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |                 return response.send(output); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             // Other assets | ||||||
|  |             const files = fs.readdirSync(folderPath) | ||||||
|  |                 .filter(filename => { | ||||||
|  |                     return filename != '.placeholder'; | ||||||
|  |                 }); | ||||||
|  |  | ||||||
|  |             for (let i of files) | ||||||
|  |                 output.push(`/characters/${name}/${category}/${i}`); | ||||||
|  |         } | ||||||
|  |         return response.send(output); | ||||||
|  |     } | ||||||
|  |     catch (err) { | ||||||
|  |         console.log(err); | ||||||
|  |         return response.sendStatus(500); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | 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, checkAssetFileName }; | ||||||
|   | |||||||
| @@ -1,35 +1,32 @@ | |||||||
|  | const express = require('express'); | ||||||
|  | const { jsonParser } = require('../express-common'); | ||||||
|  |  | ||||||
| const TASK = 'image-to-text'; | const TASK = 'image-to-text'; | ||||||
|  |  | ||||||
| /** | const router = express.Router(); | ||||||
|  * @param {import("express").Express} app |  | ||||||
|  * @param {any} jsonParser |  | ||||||
|  */ |  | ||||||
| function registerEndpoints(app, jsonParser) { |  | ||||||
|     app.post('/api/extra/caption', jsonParser, async (req, res) => { |  | ||||||
|         try { |  | ||||||
|             const { image } = req.body; |  | ||||||
|  |  | ||||||
|             const module = await import('../transformers.mjs'); | router.post('/', jsonParser, async (req, res) => { | ||||||
|             const rawImage = await module.default.getRawImage(image); |     try { | ||||||
|  |         const { image } = req.body; | ||||||
|  |  | ||||||
|             if (!rawImage) { |         const module = await import('../transformers.mjs'); | ||||||
|                 console.log('Failed to parse captioned image'); |         const rawImage = await module.default.getRawImage(image); | ||||||
|                 return res.sendStatus(400); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             const pipe = await module.default.getPipeline(TASK); |         if (!rawImage) { | ||||||
|             const result = await pipe(rawImage); |             console.log('Failed to parse captioned image'); | ||||||
|             const text = result[0].generated_text; |             return res.sendStatus(400); | ||||||
|             console.log('Image caption:', text); |  | ||||||
|  |  | ||||||
|             return res.json({ caption: text }); |  | ||||||
|         } catch (error) { |  | ||||||
|             console.error(error); |  | ||||||
|             return res.sendStatus(500); |  | ||||||
|         } |         } | ||||||
|     }); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| module.exports = { |         const pipe = await module.default.getPipeline(TASK); | ||||||
|     registerEndpoints, |         const result = await pipe(rawImage); | ||||||
| }; |         const text = result[0].generated_text; | ||||||
|  |         console.log('Image caption:', text); | ||||||
|  |  | ||||||
|  |         return res.json({ caption: text }); | ||||||
|  |     } catch (error) { | ||||||
|  |         console.error(error); | ||||||
|  |         return res.sendStatus(500); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | module.exports = { router }; | ||||||
|   | |||||||
| @@ -1,53 +1,50 @@ | |||||||
|  | const express = require('express'); | ||||||
|  | const { jsonParser } = require('../express-common'); | ||||||
|  |  | ||||||
| const TASK = 'text-classification'; | const TASK = 'text-classification'; | ||||||
|  |  | ||||||
| /** | const router = express.Router(); | ||||||
|  * @param {import("express").Express} app |  | ||||||
|  * @param {any} jsonParser |  | ||||||
|  */ |  | ||||||
| function registerEndpoints(app, jsonParser) { |  | ||||||
|     const cacheObject = {}; |  | ||||||
|  |  | ||||||
|     app.post('/api/extra/classify/labels', jsonParser, async (req, res) => { | const cacheObject = {}; | ||||||
|         try { |  | ||||||
|             const module = await import('../transformers.mjs'); |  | ||||||
|             const pipe = await module.default.getPipeline(TASK); |  | ||||||
|             const result = Object.keys(pipe.model.config.label2id); |  | ||||||
|             return res.json({ labels: result }); |  | ||||||
|         } catch (error) { |  | ||||||
|             console.error(error); |  | ||||||
|             return res.sendStatus(500); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     app.post('/api/extra/classify', jsonParser, async (req, res) => { | router.post('/labels', jsonParser, async (req, res) => { | ||||||
|         try { |     try { | ||||||
|             const { text } = req.body; |         const module = await import('../transformers.mjs'); | ||||||
|  |         const pipe = await module.default.getPipeline(TASK); | ||||||
|  |         const result = Object.keys(pipe.model.config.label2id); | ||||||
|  |         return res.json({ labels: result }); | ||||||
|  |     } catch (error) { | ||||||
|  |         console.error(error); | ||||||
|  |         return res.sendStatus(500); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|             async function getResult(text) { | router.post('/', jsonParser, async (req, res) => { | ||||||
|                 if (Object.hasOwn(cacheObject, text)) { |     try { | ||||||
|                     return cacheObject[text]; |         const { text } = req.body; | ||||||
|                 } else { |  | ||||||
|                     const module = await import('../transformers.mjs'); |         async function getResult(text) { | ||||||
|                     const pipe = await module.default.getPipeline(TASK); |             if (Object.hasOwn(cacheObject, text)) { | ||||||
|                     const result = await pipe(text, { topk: 5 }); |                 return cacheObject[text]; | ||||||
|                     result.sort((a, b) => b.score - a.score); |             } else { | ||||||
|                     cacheObject[text] = result; |                 const module = await import('../transformers.mjs'); | ||||||
|                     return result; |                 const pipe = await module.default.getPipeline(TASK); | ||||||
|                 } |                 const result = await pipe(text, { topk: 5 }); | ||||||
|  |                 result.sort((a, b) => b.score - a.score); | ||||||
|  |                 cacheObject[text] = result; | ||||||
|  |                 return result; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             console.log('Classify input:', text); |  | ||||||
|             const result = await getResult(text); |  | ||||||
|             console.log('Classify output:', result); |  | ||||||
|  |  | ||||||
|             return res.json({ classification: result }); |  | ||||||
|         } catch (error) { |  | ||||||
|             console.error(error); |  | ||||||
|             return res.sendStatus(500); |  | ||||||
|         } |         } | ||||||
|     }); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| module.exports = { |         console.log('Classify input:', text); | ||||||
|     registerEndpoints, |         const result = await getResult(text); | ||||||
| }; |         console.log('Classify output:', result); | ||||||
|  |  | ||||||
|  |         return res.json({ classification: result }); | ||||||
|  |     } catch (error) { | ||||||
|  |         console.error(error); | ||||||
|  |         return res.sendStatus(500); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | module.exports = { router }; | ||||||
|   | |||||||
| @@ -1,8 +1,10 @@ | |||||||
| const fs = require('fs'); | const fs = require('fs'); | ||||||
| const path = require('path'); | const path = require('path'); | ||||||
|  | const express = require('express'); | ||||||
| const fetch = require('node-fetch').default; | const fetch = require('node-fetch').default; | ||||||
| const sanitize = require('sanitize-filename'); | const sanitize = require('sanitize-filename'); | ||||||
| const { getConfigValue } = require('../util'); | const { getConfigValue } = require('../util'); | ||||||
|  | const { jsonParser } = require('../express-common'); | ||||||
| const contentDirectory = path.join(process.cwd(), 'default/content'); | const contentDirectory = path.join(process.cwd(), 'default/content'); | ||||||
| const contentLogPath = path.join(contentDirectory, 'content.log'); | const contentLogPath = path.join(contentDirectory, 'content.log'); | ||||||
| const contentIndexPath = path.join(contentDirectory, 'index.json'); | const contentIndexPath = path.join(contentDirectory, 'index.json'); | ||||||
| @@ -302,62 +304,57 @@ function parseJannyUrl(url) { | |||||||
|     return uuid; |     return uuid; | ||||||
| } | } | ||||||
|  |  | ||||||
| /** | const router = express.Router(); | ||||||
|  * Registers endpoints for custom content management |  | ||||||
|  * @param {import('express').Express} app Express app |  | ||||||
|  * @param {any} jsonParser JSON parser middleware |  | ||||||
|  */ |  | ||||||
| function registerEndpoints(app, jsonParser) { |  | ||||||
|     app.post('/api/content/import', jsonParser, async (request, response) => { |  | ||||||
|         if (!request.body.url) { |  | ||||||
|             return response.sendStatus(400); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         try { | router.post('/import', jsonParser, async (request, response) => { | ||||||
|             const url = request.body.url; |     if (!request.body.url) { | ||||||
|             let result; |         return response.sendStatus(400); | ||||||
|             let type; |     } | ||||||
|  |  | ||||||
|             const isJannnyContent = url.includes('janitorai'); |     try { | ||||||
|             if (isJannnyContent) { |         const url = request.body.url; | ||||||
|                 const uuid = parseJannyUrl(url); |         let result; | ||||||
|                 if (!uuid) { |         let type; | ||||||
|                     return response.sendStatus(404); |  | ||||||
|                 } |  | ||||||
|  |  | ||||||
|                 type = 'character'; |         const isJannnyContent = url.includes('janitorai'); | ||||||
|                 result = await downloadJannyCharacter(uuid); |         if (isJannnyContent) { | ||||||
|             } else { |             const uuid = parseJannyUrl(url); | ||||||
|                 const chubParsed = parseChubUrl(url); |             if (!uuid) { | ||||||
|                 type = chubParsed?.type; |                 return response.sendStatus(404); | ||||||
|  |  | ||||||
|                 if (chubParsed?.type === 'character') { |  | ||||||
|                     console.log('Downloading chub character:', chubParsed.id); |  | ||||||
|                     result = await downloadChubCharacter(chubParsed.id); |  | ||||||
|                 } |  | ||||||
|                 else if (chubParsed?.type === 'lorebook') { |  | ||||||
|                     console.log('Downloading chub lorebook:', chubParsed.id); |  | ||||||
|                     result = await downloadChubLorebook(chubParsed.id); |  | ||||||
|                 } |  | ||||||
|                 else { |  | ||||||
|                     return response.sendStatus(404); |  | ||||||
|                 } |  | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             if (result.fileType) response.set('Content-Type', result.fileType); |             type = 'character'; | ||||||
|             response.set('Content-Disposition', `attachment; filename="${result.fileName}"`); |             result = await downloadJannyCharacter(uuid); | ||||||
|             response.set('X-Custom-Content-Type', type); |         } else { | ||||||
|             return response.send(result.buffer); |             const chubParsed = parseChubUrl(url); | ||||||
|         } catch (error) { |             type = chubParsed?.type; | ||||||
|             console.log('Importing custom content failed', error); |  | ||||||
|             return response.sendStatus(500); |             if (chubParsed?.type === 'character') { | ||||||
|  |                 console.log('Downloading chub character:', chubParsed.id); | ||||||
|  |                 result = await downloadChubCharacter(chubParsed.id); | ||||||
|  |             } | ||||||
|  |             else if (chubParsed?.type === 'lorebook') { | ||||||
|  |                 console.log('Downloading chub lorebook:', chubParsed.id); | ||||||
|  |                 result = await downloadChubLorebook(chubParsed.id); | ||||||
|  |             } | ||||||
|  |             else { | ||||||
|  |                 return response.sendStatus(404); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     }); |  | ||||||
| } |         if (result.fileType) response.set('Content-Type', result.fileType); | ||||||
|  |         response.set('Content-Disposition', `attachment; filename="${result.fileName}"`); | ||||||
|  |         response.set('X-Custom-Content-Type', type); | ||||||
|  |         return response.send(result.buffer); | ||||||
|  |     } catch (error) { | ||||||
|  |         console.log('Importing custom content failed', error); | ||||||
|  |         return response.sendStatus(500); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     checkForNewContent, |     checkForNewContent, | ||||||
|     registerEndpoints, |  | ||||||
|     getDefaultPresets, |     getDefaultPresets, | ||||||
|     getDefaultPresetFile, |     getDefaultPresetFile, | ||||||
|  |     router, | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -1,8 +1,10 @@ | |||||||
| const path = require('path'); | const path = require('path'); | ||||||
| const fs = require('fs'); | const fs = require('fs'); | ||||||
|  | const express = require('express'); | ||||||
| const { default: simpleGit } = require('simple-git'); | const { default: simpleGit } = require('simple-git'); | ||||||
| const sanitize = require('sanitize-filename'); | const sanitize = require('sanitize-filename'); | ||||||
| const { DIRECTORIES } = require('../constants'); | const { DIRECTORIES } = require('../constants'); | ||||||
|  | const { jsonParser } = require('../express-common'); | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * This function extracts the extension information from the manifest file. |  * This function extracts the extension information from the manifest file. | ||||||
| @@ -45,206 +47,198 @@ async function checkIfRepoIsUpToDate(extensionPath) { | |||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | const router = express.Router(); | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Registers the endpoints for the third-party extensions API. |  * HTTP POST handler function to clone a git repository from a provided URL, read the extension manifest, | ||||||
|  * @param {import('express').Express} app - Express app |  * and return extension information and path. | ||||||
|  * @param {any} jsonParser - JSON parser middleware |  * | ||||||
|  |  * @param {Object} request - HTTP Request object, expects a JSON body with a 'url' property. | ||||||
|  |  * @param {Object} response - HTTP Response object used to respond to the HTTP request. | ||||||
|  |  * | ||||||
|  |  * @returns {void} | ||||||
|  */ |  */ | ||||||
| function registerEndpoints(app, jsonParser) { | router.post('/install', jsonParser, async (request, response) => { | ||||||
|     /** |     if (!request.body.url) { | ||||||
|      * HTTP POST handler function to clone a git repository from a provided URL, read the extension manifest, |         return response.status(400).send('Bad Request: URL is required in the request body.'); | ||||||
|      * and return extension information and path. |     } | ||||||
|      * |  | ||||||
|      * @param {Object} request - HTTP Request object, expects a JSON body with a 'url' property. |  | ||||||
|      * @param {Object} response - HTTP Response object used to respond to the HTTP request. |  | ||||||
|      * |  | ||||||
|      * @returns {void} |  | ||||||
|      */ |  | ||||||
|     app.post('/api/extensions/install', jsonParser, async (request, response) => { |  | ||||||
|         if (!request.body.url) { |  | ||||||
|             return response.status(400).send('Bad Request: URL is required in the request body.'); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         try { |     try { | ||||||
|             const git = simpleGit(); |  | ||||||
|  |  | ||||||
|             // make sure the third-party directory exists |  | ||||||
|             if (!fs.existsSync(path.join(DIRECTORIES.extensions, 'third-party'))) { |  | ||||||
|                 fs.mkdirSync(path.join(DIRECTORIES.extensions, 'third-party')); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             const url = request.body.url; |  | ||||||
|             const extensionPath = path.join(DIRECTORIES.extensions, 'third-party', path.basename(url, '.git')); |  | ||||||
|  |  | ||||||
|             if (fs.existsSync(extensionPath)) { |  | ||||||
|                 return response.status(409).send(`Directory already exists at ${extensionPath}`); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             await git.clone(url, extensionPath, { '--depth': 1 }); |  | ||||||
|             console.log(`Extension has been cloned at ${extensionPath}`); |  | ||||||
|  |  | ||||||
|  |  | ||||||
|             const { version, author, display_name } = await getManifest(extensionPath); |  | ||||||
|  |  | ||||||
|  |  | ||||||
|             return response.send({ version, author, display_name, extensionPath }); |  | ||||||
|         } catch (error) { |  | ||||||
|             console.log('Importing custom content failed', error); |  | ||||||
|             return response.status(500).send(`Server Error: ${error.message}`); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * HTTP POST handler function to pull the latest updates from a git repository |  | ||||||
|      * based on the extension name provided in the request body. It returns the latest commit hash, |  | ||||||
|      * the path of the extension, the status of the repository (whether it's up-to-date or not), |  | ||||||
|      * and the remote URL of the repository. |  | ||||||
|      * |  | ||||||
|      * @param {Object} request - HTTP Request object, expects a JSON body with an 'extensionName' property. |  | ||||||
|      * @param {Object} response - HTTP Response object used to respond to the HTTP request. |  | ||||||
|      * |  | ||||||
|      * @returns {void} |  | ||||||
|      */ |  | ||||||
|     app.post('/api/extensions/update', jsonParser, async (request, response) => { |  | ||||||
|         const git = simpleGit(); |         const git = simpleGit(); | ||||||
|         if (!request.body.extensionName) { |  | ||||||
|             return response.status(400).send('Bad Request: extensionName is required in the request body.'); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         try { |  | ||||||
|             const extensionName = request.body.extensionName; |  | ||||||
|             const extensionPath = path.join(DIRECTORIES.extensions, 'third-party', extensionName); |  | ||||||
|  |  | ||||||
|             if (!fs.existsSync(extensionPath)) { |  | ||||||
|                 return response.status(404).send(`Directory does not exist at ${extensionPath}`); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); |  | ||||||
|             const currentBranch = await git.cwd(extensionPath).branch(); |  | ||||||
|             if (!isUpToDate) { |  | ||||||
|  |  | ||||||
|                 await git.cwd(extensionPath).pull('origin', currentBranch.current); |  | ||||||
|                 console.log(`Extension has been updated at ${extensionPath}`); |  | ||||||
|             } else { |  | ||||||
|                 console.log(`Extension is up to date at ${extensionPath}`); |  | ||||||
|             } |  | ||||||
|             await git.cwd(extensionPath).fetch('origin'); |  | ||||||
|             const fullCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); |  | ||||||
|             const shortCommitHash = fullCommitHash.slice(0, 7); |  | ||||||
|  |  | ||||||
|             return response.send({ shortCommitHash, extensionPath, isUpToDate, remoteUrl }); |  | ||||||
|  |  | ||||||
|         } catch (error) { |  | ||||||
|             console.log('Updating custom content failed', error); |  | ||||||
|             return response.status(500).send(`Server Error: ${error.message}`); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * HTTP POST handler function to get the current git commit hash and branch name for a given extension. |  | ||||||
|      * It checks whether the repository is up-to-date with the remote, and returns the status along with |  | ||||||
|      * the remote URL of the repository. |  | ||||||
|      * |  | ||||||
|      * @param {Object} request - HTTP Request object, expects a JSON body with an 'extensionName' property. |  | ||||||
|      * @param {Object} response - HTTP Response object used to respond to the HTTP request. |  | ||||||
|      * |  | ||||||
|      * @returns {void} |  | ||||||
|      */ |  | ||||||
|     app.post('/api/extensions/version', jsonParser, async (request, response) => { |  | ||||||
|         const git = simpleGit(); |  | ||||||
|         if (!request.body.extensionName) { |  | ||||||
|             return response.status(400).send('Bad Request: extensionName is required in the request body.'); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         try { |  | ||||||
|             const extensionName = request.body.extensionName; |  | ||||||
|             const extensionPath = path.join(DIRECTORIES.extensions, 'third-party', extensionName); |  | ||||||
|  |  | ||||||
|             if (!fs.existsSync(extensionPath)) { |  | ||||||
|                 return response.status(404).send(`Directory does not exist at ${extensionPath}`); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             const currentBranch = await git.cwd(extensionPath).branch(); |  | ||||||
|             // get only the working branch |  | ||||||
|             const currentBranchName = currentBranch.current; |  | ||||||
|             await git.cwd(extensionPath).fetch('origin'); |  | ||||||
|             const currentCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); |  | ||||||
|             console.log(currentBranch, currentCommitHash); |  | ||||||
|             const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); |  | ||||||
|  |  | ||||||
|             return response.send({ currentBranchName, currentCommitHash, isUpToDate, remoteUrl }); |  | ||||||
|  |  | ||||||
|         } catch (error) { |  | ||||||
|             console.log('Getting extension version failed', error); |  | ||||||
|             return response.status(500).send(`Server Error: ${error.message}`); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * HTTP POST handler function to delete a git repository based on the extension name provided in the request body. |  | ||||||
|      * |  | ||||||
|      * @param {Object} request - HTTP Request object, expects a JSON body with a 'url' property. |  | ||||||
|      * @param {Object} response - HTTP Response object used to respond to the HTTP request. |  | ||||||
|      * |  | ||||||
|      * @returns {void} |  | ||||||
|      */ |  | ||||||
|     app.post('/api/extensions/delete', jsonParser, async (request, response) => { |  | ||||||
|         if (!request.body.extensionName) { |  | ||||||
|             return response.status(400).send('Bad Request: extensionName is required in the request body.'); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         // Sanatize the extension name to prevent directory traversal |  | ||||||
|         const extensionName = sanitize(request.body.extensionName); |  | ||||||
|  |  | ||||||
|         try { |  | ||||||
|             const extensionPath = path.join(DIRECTORIES.extensions, 'third-party', extensionName); |  | ||||||
|  |  | ||||||
|             if (!fs.existsSync(extensionPath)) { |  | ||||||
|                 return response.status(404).send(`Directory does not exist at ${extensionPath}`); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             await fs.promises.rmdir(extensionPath, { recursive: true }); |  | ||||||
|             console.log(`Extension has been deleted at ${extensionPath}`); |  | ||||||
|  |  | ||||||
|             return response.send(`Extension has been deleted at ${extensionPath}`); |  | ||||||
|  |  | ||||||
|         } catch (error) { |  | ||||||
|             console.log('Deleting custom content failed', error); |  | ||||||
|             return response.status(500).send(`Server Error: ${error.message}`); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * Discover the extension folders |  | ||||||
|      * If the folder is called third-party, search for subfolders instead |  | ||||||
|      */ |  | ||||||
|     app.get('/api/extensions/discover', jsonParser, function (_, response) { |  | ||||||
|  |  | ||||||
|         // get all folders in the extensions folder, except third-party |  | ||||||
|         const extensions = fs |  | ||||||
|             .readdirSync(DIRECTORIES.extensions) |  | ||||||
|             .filter(f => fs.statSync(path.join(DIRECTORIES.extensions, f)).isDirectory()) |  | ||||||
|             .filter(f => f !== 'third-party'); |  | ||||||
|  |  | ||||||
|         // get all folders in the third-party folder, if it exists |  | ||||||
|  |  | ||||||
|  |         // make sure the third-party directory exists | ||||||
|         if (!fs.existsSync(path.join(DIRECTORIES.extensions, 'third-party'))) { |         if (!fs.existsSync(path.join(DIRECTORIES.extensions, 'third-party'))) { | ||||||
|             return response.send(extensions); |             fs.mkdirSync(path.join(DIRECTORIES.extensions, 'third-party')); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         const thirdPartyExtensions = fs |         const url = request.body.url; | ||||||
|             .readdirSync(path.join(DIRECTORIES.extensions, 'third-party')) |         const extensionPath = path.join(DIRECTORIES.extensions, 'third-party', path.basename(url, '.git')); | ||||||
|             .filter(f => fs.statSync(path.join(DIRECTORIES.extensions, 'third-party', f)).isDirectory()); |  | ||||||
|  |  | ||||||
|         // add the third-party extensions to the extensions array |         if (fs.existsSync(extensionPath)) { | ||||||
|         extensions.push(...thirdPartyExtensions.map(f => `third-party/${f}`)); |             return response.status(409).send(`Directory already exists at ${extensionPath}`); | ||||||
|         console.log(extensions); |         } | ||||||
|  |  | ||||||
|  |         await git.clone(url, extensionPath, { '--depth': 1 }); | ||||||
|  |         console.log(`Extension has been cloned at ${extensionPath}`); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         const { version, author, display_name } = await getManifest(extensionPath); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         return response.send({ version, author, display_name, extensionPath }); | ||||||
|  |     } catch (error) { | ||||||
|  |         console.log('Importing custom content failed', error); | ||||||
|  |         return response.status(500).send(`Server Error: ${error.message}`); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * HTTP POST handler function to pull the latest updates from a git repository | ||||||
|  |  * based on the extension name provided in the request body. It returns the latest commit hash, | ||||||
|  |  * the path of the extension, the status of the repository (whether it's up-to-date or not), | ||||||
|  |  * and the remote URL of the repository. | ||||||
|  |  * | ||||||
|  |  * @param {Object} request - HTTP Request object, expects a JSON body with an 'extensionName' property. | ||||||
|  |  * @param {Object} response - HTTP Response object used to respond to the HTTP request. | ||||||
|  |  * | ||||||
|  |  * @returns {void} | ||||||
|  |  */ | ||||||
|  | router.post('/update', jsonParser, async (request, response) => { | ||||||
|  |     const git = simpleGit(); | ||||||
|  |     if (!request.body.extensionName) { | ||||||
|  |         return response.status(400).send('Bad Request: extensionName is required in the request body.'); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     try { | ||||||
|  |         const extensionName = request.body.extensionName; | ||||||
|  |         const extensionPath = path.join(DIRECTORIES.extensions, 'third-party', extensionName); | ||||||
|  |  | ||||||
|  |         if (!fs.existsSync(extensionPath)) { | ||||||
|  |             return response.status(404).send(`Directory does not exist at ${extensionPath}`); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); | ||||||
|  |         const currentBranch = await git.cwd(extensionPath).branch(); | ||||||
|  |         if (!isUpToDate) { | ||||||
|  |  | ||||||
|  |             await git.cwd(extensionPath).pull('origin', currentBranch.current); | ||||||
|  |             console.log(`Extension has been updated at ${extensionPath}`); | ||||||
|  |         } else { | ||||||
|  |             console.log(`Extension is up to date at ${extensionPath}`); | ||||||
|  |         } | ||||||
|  |         await git.cwd(extensionPath).fetch('origin'); | ||||||
|  |         const fullCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); | ||||||
|  |         const shortCommitHash = fullCommitHash.slice(0, 7); | ||||||
|  |  | ||||||
|  |         return response.send({ shortCommitHash, extensionPath, isUpToDate, remoteUrl }); | ||||||
|  |  | ||||||
|  |     } catch (error) { | ||||||
|  |         console.log('Updating custom content failed', error); | ||||||
|  |         return response.status(500).send(`Server Error: ${error.message}`); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * HTTP POST handler function to get the current git commit hash and branch name for a given extension. | ||||||
|  |  * It checks whether the repository is up-to-date with the remote, and returns the status along with | ||||||
|  |  * the remote URL of the repository. | ||||||
|  |  * | ||||||
|  |  * @param {Object} request - HTTP Request object, expects a JSON body with an 'extensionName' property. | ||||||
|  |  * @param {Object} response - HTTP Response object used to respond to the HTTP request. | ||||||
|  |  * | ||||||
|  |  * @returns {void} | ||||||
|  |  */ | ||||||
|  | router.post('/version', jsonParser, async (request, response) => { | ||||||
|  |     const git = simpleGit(); | ||||||
|  |     if (!request.body.extensionName) { | ||||||
|  |         return response.status(400).send('Bad Request: extensionName is required in the request body.'); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     try { | ||||||
|  |         const extensionName = request.body.extensionName; | ||||||
|  |         const extensionPath = path.join(DIRECTORIES.extensions, 'third-party', extensionName); | ||||||
|  |  | ||||||
|  |         if (!fs.existsSync(extensionPath)) { | ||||||
|  |             return response.status(404).send(`Directory does not exist at ${extensionPath}`); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         const currentBranch = await git.cwd(extensionPath).branch(); | ||||||
|  |         // get only the working branch | ||||||
|  |         const currentBranchName = currentBranch.current; | ||||||
|  |         await git.cwd(extensionPath).fetch('origin'); | ||||||
|  |         const currentCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); | ||||||
|  |         console.log(currentBranch, currentCommitHash); | ||||||
|  |         const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); | ||||||
|  |  | ||||||
|  |         return response.send({ currentBranchName, currentCommitHash, isUpToDate, remoteUrl }); | ||||||
|  |  | ||||||
|  |     } catch (error) { | ||||||
|  |         console.log('Getting extension version failed', error); | ||||||
|  |         return response.status(500).send(`Server Error: ${error.message}`); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * HTTP POST handler function to delete a git repository based on the extension name provided in the request body. | ||||||
|  |  * | ||||||
|  |  * @param {Object} request - HTTP Request object, expects a JSON body with a 'url' property. | ||||||
|  |  * @param {Object} response - HTTP Response object used to respond to the HTTP request. | ||||||
|  |  * | ||||||
|  |  * @returns {void} | ||||||
|  |  */ | ||||||
|  | router.post('/delete', jsonParser, async (request, response) => { | ||||||
|  |     if (!request.body.extensionName) { | ||||||
|  |         return response.status(400).send('Bad Request: extensionName is required in the request body.'); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Sanatize the extension name to prevent directory traversal | ||||||
|  |     const extensionName = sanitize(request.body.extensionName); | ||||||
|  |  | ||||||
|  |     try { | ||||||
|  |         const extensionPath = path.join(DIRECTORIES.extensions, 'third-party', extensionName); | ||||||
|  |  | ||||||
|  |         if (!fs.existsSync(extensionPath)) { | ||||||
|  |             return response.status(404).send(`Directory does not exist at ${extensionPath}`); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         await fs.promises.rmdir(extensionPath, { recursive: true }); | ||||||
|  |         console.log(`Extension has been deleted at ${extensionPath}`); | ||||||
|  |  | ||||||
|  |         return response.send(`Extension has been deleted at ${extensionPath}`); | ||||||
|  |  | ||||||
|  |     } catch (error) { | ||||||
|  |         console.log('Deleting custom content failed', error); | ||||||
|  |         return response.status(500).send(`Server Error: ${error.message}`); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Discover the extension folders | ||||||
|  |  * If the folder is called third-party, search for subfolders instead | ||||||
|  |  */ | ||||||
|  | router.get('/discover', jsonParser, function (_, response) { | ||||||
|  |     // get all folders in the extensions folder, except third-party | ||||||
|  |     const extensions = fs | ||||||
|  |         .readdirSync(DIRECTORIES.extensions) | ||||||
|  |         .filter(f => fs.statSync(path.join(DIRECTORIES.extensions, f)).isDirectory()) | ||||||
|  |         .filter(f => f !== 'third-party'); | ||||||
|  |  | ||||||
|  |     // get all folders in the third-party folder, if it exists | ||||||
|  |  | ||||||
|  |     if (!fs.existsSync(path.join(DIRECTORIES.extensions, 'third-party'))) { | ||||||
|         return response.send(extensions); |         return response.send(extensions); | ||||||
|     }); |     } | ||||||
| } |  | ||||||
|  |  | ||||||
| module.exports = { |     const thirdPartyExtensions = fs | ||||||
|     registerEndpoints, |         .readdirSync(path.join(DIRECTORIES.extensions, 'third-party')) | ||||||
| }; |         .filter(f => fs.statSync(path.join(DIRECTORIES.extensions, 'third-party', f)).isDirectory()); | ||||||
|  |  | ||||||
|  |     // add the third-party extensions to the extensions array | ||||||
|  |     extensions.push(...thirdPartyExtensions.map(f => `third-party/${f}`)); | ||||||
|  |     console.log(extensions); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     return response.send(extensions); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | module.exports = { router }; | ||||||
|   | |||||||
							
								
								
									
										35
									
								
								src/endpoints/files.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/endpoints/files.js
									
									
									
									
									
										Normal file
									
								
							| @@ -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 }; | ||||||
| @@ -1,7 +1,9 @@ | |||||||
| const fetch = require('node-fetch').default; | const fetch = require('node-fetch').default; | ||||||
|  | const express = require('express'); | ||||||
| const AIHorde = require('../ai_horde'); | const AIHorde = require('../ai_horde'); | ||||||
| const { getVersion, delay } = require('../util'); | const { getVersion, delay } = require('../util'); | ||||||
| const { readSecret, SECRET_KEYS } = require('./secrets'); | const { readSecret, SECRET_KEYS } = require('./secrets'); | ||||||
|  | const { jsonParser } = require('../express-common'); | ||||||
|  |  | ||||||
| const ANONYMOUS_KEY = '0000000000'; | const ANONYMOUS_KEY = '0000000000'; | ||||||
|  |  | ||||||
| @@ -52,221 +54,214 @@ function sanitizeHordeImagePrompt(prompt) { | |||||||
|     return prompt; |     return prompt; | ||||||
| } | } | ||||||
|  |  | ||||||
| /** | const router = express.Router(); | ||||||
|  * |  | ||||||
|  * @param {import("express").Express} app | router.post('/generate-text', jsonParser, async (request, response) => { | ||||||
|  * @param {any} jsonParser |     const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY; | ||||||
|  */ |     const url = 'https://horde.koboldai.net/api/v2/generate/text/async'; | ||||||
| function registerEndpoints(app, jsonParser) { |  | ||||||
|     app.post('/api/horde/generate-text', jsonParser, async (request, response) => { |     console.log(request.body); | ||||||
|  |     try { | ||||||
|  |         const result = await fetch(url, { | ||||||
|  |             method: 'POST', | ||||||
|  |             body: JSON.stringify(request.body), | ||||||
|  |             headers: { | ||||||
|  |                 'Content-Type': 'application/json', | ||||||
|  |                 'apikey': api_key_horde, | ||||||
|  |                 'Client-Agent': String(request.header('Client-Agent')), | ||||||
|  |             }, | ||||||
|  |         }); | ||||||
|  |  | ||||||
|  |         if (!result.ok) { | ||||||
|  |             const message = await result.text(); | ||||||
|  |             console.log('Horde returned an error:', message); | ||||||
|  |             return response.send({ error: { message } }); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         const data = await result.json(); | ||||||
|  |         return response.send(data); | ||||||
|  |     } catch (error) { | ||||||
|  |         console.log(error); | ||||||
|  |         return response.send({ error: true }); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | router.post('/sd-samplers', jsonParser, async (_, response) => { | ||||||
|  |     try { | ||||||
|  |         const ai_horde = await getHordeClient(); | ||||||
|  |         const samplers = Object.values(ai_horde.ModelGenerationInputStableSamplers); | ||||||
|  |         response.send(samplers); | ||||||
|  |     } catch (error) { | ||||||
|  |         console.error(error); | ||||||
|  |         response.sendStatus(500); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | router.post('/sd-models', jsonParser, async (_, response) => { | ||||||
|  |     try { | ||||||
|  |         const ai_horde = await getHordeClient(); | ||||||
|  |         const models = await ai_horde.getModels(); | ||||||
|  |         response.send(models); | ||||||
|  |     } catch (error) { | ||||||
|  |         console.error(error); | ||||||
|  |         response.sendStatus(500); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | router.post('/caption-image', jsonParser, async (request, response) => { | ||||||
|  |     try { | ||||||
|         const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY; |         const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY; | ||||||
|         const url = 'https://horde.koboldai.net/api/v2/generate/text/async'; |         const ai_horde = await getHordeClient(); | ||||||
|  |         const result = await ai_horde.postAsyncInterrogate({ | ||||||
|  |             source_image: request.body.image, | ||||||
|  |             forms: [{ name: AIHorde.ModelInterrogationFormTypes.caption }], | ||||||
|  |         }, { token: api_key_horde }); | ||||||
|  |  | ||||||
|         console.log(request.body); |         if (!result.id) { | ||||||
|         try { |             console.error('Image interrogation request is not satisfyable:', result.message || 'unknown error'); | ||||||
|             const result = await fetch(url, { |  | ||||||
|                 method: 'POST', |  | ||||||
|                 body: JSON.stringify(request.body), |  | ||||||
|                 headers: { |  | ||||||
|                     'Content-Type': 'application/json', |  | ||||||
|                     'apikey': api_key_horde, |  | ||||||
|                     'Client-Agent': String(request.header('Client-Agent')), |  | ||||||
|                 }, |  | ||||||
|             }); |  | ||||||
|  |  | ||||||
|             if (!result.ok) { |  | ||||||
|                 const message = await result.text(); |  | ||||||
|                 console.log('Horde returned an error:', message); |  | ||||||
|                 return response.send({ error: { message } }); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             const data = await result.json(); |  | ||||||
|             return response.send(data); |  | ||||||
|         } catch (error) { |  | ||||||
|             console.log(error); |  | ||||||
|             return response.send({ error: true }); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     app.post('/api/horde/sd-samplers', jsonParser, async (_, response) => { |  | ||||||
|         try { |  | ||||||
|             const ai_horde = await getHordeClient(); |  | ||||||
|             const samplers = Object.values(ai_horde.ModelGenerationInputStableSamplers); |  | ||||||
|             response.send(samplers); |  | ||||||
|         } catch (error) { |  | ||||||
|             console.error(error); |  | ||||||
|             response.sendStatus(500); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     app.post('/api/horde/sd-models', jsonParser, async (_, response) => { |  | ||||||
|         try { |  | ||||||
|             const ai_horde = await getHordeClient(); |  | ||||||
|             const models = await ai_horde.getModels(); |  | ||||||
|             response.send(models); |  | ||||||
|         } catch (error) { |  | ||||||
|             console.error(error); |  | ||||||
|             response.sendStatus(500); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     app.post('/api/horde/caption-image', jsonParser, async (request, response) => { |  | ||||||
|         try { |  | ||||||
|             const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY; |  | ||||||
|             const ai_horde = await getHordeClient(); |  | ||||||
|             const result = await ai_horde.postAsyncInterrogate({ |  | ||||||
|                 source_image: request.body.image, |  | ||||||
|                 forms: [{ name: AIHorde.ModelInterrogationFormTypes.caption }], |  | ||||||
|             }, { token: api_key_horde }); |  | ||||||
|  |  | ||||||
|             if (!result.id) { |  | ||||||
|                 console.error('Image interrogation request is not satisfyable:', result.message || 'unknown error'); |  | ||||||
|                 return response.sendStatus(400); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             const MAX_ATTEMPTS = 200; |  | ||||||
|             const CHECK_INTERVAL = 3000; |  | ||||||
|  |  | ||||||
|             for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { |  | ||||||
|                 await delay(CHECK_INTERVAL); |  | ||||||
|                 const status = await ai_horde.getInterrogationStatus(result.id); |  | ||||||
|                 console.log(status); |  | ||||||
|  |  | ||||||
|                 if (status.state === AIHorde.HordeAsyncRequestStates.done) { |  | ||||||
|  |  | ||||||
|                     if (status.forms === undefined) { |  | ||||||
|                         console.error('Image interrogation request failed: no forms found.'); |  | ||||||
|                         return response.sendStatus(500); |  | ||||||
|                     } |  | ||||||
|  |  | ||||||
|                     console.log('Image interrogation result:', status); |  | ||||||
|                     const caption = status?.forms[0]?.result?.caption || ''; |  | ||||||
|  |  | ||||||
|                     if (!caption) { |  | ||||||
|                         console.error('Image interrogation request failed: no caption found.'); |  | ||||||
|                         return response.sendStatus(500); |  | ||||||
|                     } |  | ||||||
|  |  | ||||||
|                     return response.send({ caption }); |  | ||||||
|                 } |  | ||||||
|  |  | ||||||
|                 if (status.state === AIHorde.HordeAsyncRequestStates.faulted || status.state === AIHorde.HordeAsyncRequestStates.cancelled) { |  | ||||||
|                     console.log('Image interrogation request is not successful.'); |  | ||||||
|                     return response.sendStatus(503); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|         } catch (error) { |  | ||||||
|             console.error(error); |  | ||||||
|             response.sendStatus(500); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     app.post('/api/horde/user-info', jsonParser, async (_, response) => { |  | ||||||
|         const api_key_horde = readSecret(SECRET_KEYS.HORDE); |  | ||||||
|  |  | ||||||
|         if (!api_key_horde) { |  | ||||||
|             return response.send({ anonymous: true }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         try { |  | ||||||
|             const ai_horde = await getHordeClient(); |  | ||||||
|             const user = await ai_horde.findUser({ token: api_key_horde }); |  | ||||||
|             return response.send(user); |  | ||||||
|         } catch (error) { |  | ||||||
|             console.error(error); |  | ||||||
|             return response.sendStatus(500); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     app.post('/api/horde/generate-image', jsonParser, async (request, response) => { |  | ||||||
|         if (!request.body.prompt) { |  | ||||||
|             return response.sendStatus(400); |             return response.sendStatus(400); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         const MAX_ATTEMPTS = 200; |         const MAX_ATTEMPTS = 200; | ||||||
|         const CHECK_INTERVAL = 3000; |         const CHECK_INTERVAL = 3000; | ||||||
|         const PROMPT_THRESHOLD = 5000; |  | ||||||
|  |  | ||||||
|         try { |         for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { | ||||||
|             const maxLength = PROMPT_THRESHOLD - String(request.body.negative_prompt).length - 5; |             await delay(CHECK_INTERVAL); | ||||||
|             if (String(request.body.prompt).length > maxLength) { |             const status = await ai_horde.getInterrogationStatus(result.id); | ||||||
|                 console.log('Stable Horde prompt is too long, truncating...'); |             console.log(status); | ||||||
|                 request.body.prompt = String(request.body.prompt).substring(0, maxLength); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // Sanitize prompt if requested |             if (status.state === AIHorde.HordeAsyncRequestStates.done) { | ||||||
|             if (request.body.sanitize) { |  | ||||||
|                 const sanitized = sanitizeHordeImagePrompt(request.body.prompt); |  | ||||||
|  |  | ||||||
|                 if (request.body.prompt !== sanitized) { |                 if (status.forms === undefined) { | ||||||
|                     console.log('Stable Horde prompt was sanitized.'); |                     console.error('Image interrogation request failed: no forms found.'); | ||||||
|                 } |  | ||||||
|  |  | ||||||
|                 request.body.prompt = sanitized; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY; |  | ||||||
|             console.log('Stable Horde request:', request.body); |  | ||||||
|  |  | ||||||
|             const ai_horde = await getHordeClient(); |  | ||||||
|             const generation = await ai_horde.postAsyncImageGenerate( |  | ||||||
|                 { |  | ||||||
|                     prompt: `${request.body.prompt} ### ${request.body.negative_prompt}`, |  | ||||||
|                     params: |  | ||||||
|                     { |  | ||||||
|                         sampler_name: request.body.sampler, |  | ||||||
|                         hires_fix: request.body.enable_hr, |  | ||||||
|                         // @ts-ignore - use_gfpgan param is not in the type definition, need to update to new ai_horde @ https://github.com/ZeldaFan0225/ai_horde/blob/main/index.ts |  | ||||||
|                         use_gfpgan: request.body.restore_faces, |  | ||||||
|                         cfg_scale: request.body.scale, |  | ||||||
|                         steps: request.body.steps, |  | ||||||
|                         width: request.body.width, |  | ||||||
|                         height: request.body.height, |  | ||||||
|                         karras: Boolean(request.body.karras), |  | ||||||
|                         n: 1, |  | ||||||
|                     }, |  | ||||||
|                     r2: false, |  | ||||||
|                     nsfw: request.body.nfsw, |  | ||||||
|                     models: [request.body.model], |  | ||||||
|                 }, |  | ||||||
|                 { token: api_key_horde }); |  | ||||||
|  |  | ||||||
|             if (!generation.id) { |  | ||||||
|                 console.error('Image generation request is not satisfyable:', generation.message || 'unknown error'); |  | ||||||
|                 return response.sendStatus(400); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { |  | ||||||
|                 await delay(CHECK_INTERVAL); |  | ||||||
|                 const check = await ai_horde.getImageGenerationCheck(generation.id); |  | ||||||
|                 console.log(check); |  | ||||||
|  |  | ||||||
|                 if (check.done) { |  | ||||||
|                     const result = await ai_horde.getImageGenerationStatus(generation.id); |  | ||||||
|                     if (result.generations === undefined) return response.sendStatus(500); |  | ||||||
|                     return response.send(result.generations[0].img); |  | ||||||
|                 } |  | ||||||
|  |  | ||||||
|                 /* |  | ||||||
|                 if (!check.is_possible) { |  | ||||||
|                     return response.sendStatus(503); |  | ||||||
|                 } |  | ||||||
|                 */ |  | ||||||
|  |  | ||||||
|                 if (check.faulted) { |  | ||||||
|                     return response.sendStatus(500); |                     return response.sendStatus(500); | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|  |                 console.log('Image interrogation result:', status); | ||||||
|  |                 const caption = status?.forms[0]?.result?.caption || ''; | ||||||
|  |  | ||||||
|  |                 if (!caption) { | ||||||
|  |                     console.error('Image interrogation request failed: no caption found.'); | ||||||
|  |                     return response.sendStatus(500); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 return response.send({ caption }); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             return response.sendStatus(504); |             if (status.state === AIHorde.HordeAsyncRequestStates.faulted || status.state === AIHorde.HordeAsyncRequestStates.cancelled) { | ||||||
|         } catch (error) { |                 console.log('Image interrogation request is not successful.'); | ||||||
|             console.error(error); |                 return response.sendStatus(503); | ||||||
|             return response.sendStatus(500); |             } | ||||||
|         } |         } | ||||||
|     }); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| module.exports = { |     } catch (error) { | ||||||
|     registerEndpoints, |         console.error(error); | ||||||
| }; |         response.sendStatus(500); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | router.post('/user-info', jsonParser, async (_, response) => { | ||||||
|  |     const api_key_horde = readSecret(SECRET_KEYS.HORDE); | ||||||
|  |  | ||||||
|  |     if (!api_key_horde) { | ||||||
|  |         return response.send({ anonymous: true }); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     try { | ||||||
|  |         const ai_horde = await getHordeClient(); | ||||||
|  |         const user = await ai_horde.findUser({ token: api_key_horde }); | ||||||
|  |         return response.send(user); | ||||||
|  |     } catch (error) { | ||||||
|  |         console.error(error); | ||||||
|  |         return response.sendStatus(500); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | router.post('/generate-image', jsonParser, async (request, response) => { | ||||||
|  |     if (!request.body.prompt) { | ||||||
|  |         return response.sendStatus(400); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     const MAX_ATTEMPTS = 200; | ||||||
|  |     const CHECK_INTERVAL = 3000; | ||||||
|  |     const PROMPT_THRESHOLD = 5000; | ||||||
|  |  | ||||||
|  |     try { | ||||||
|  |         const maxLength = PROMPT_THRESHOLD - String(request.body.negative_prompt).length - 5; | ||||||
|  |         if (String(request.body.prompt).length > maxLength) { | ||||||
|  |             console.log('Stable Horde prompt is too long, truncating...'); | ||||||
|  |             request.body.prompt = String(request.body.prompt).substring(0, maxLength); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // Sanitize prompt if requested | ||||||
|  |         if (request.body.sanitize) { | ||||||
|  |             const sanitized = sanitizeHordeImagePrompt(request.body.prompt); | ||||||
|  |  | ||||||
|  |             if (request.body.prompt !== sanitized) { | ||||||
|  |                 console.log('Stable Horde prompt was sanitized.'); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             request.body.prompt = sanitized; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY; | ||||||
|  |         console.log('Stable Horde request:', request.body); | ||||||
|  |  | ||||||
|  |         const ai_horde = await getHordeClient(); | ||||||
|  |         const generation = await ai_horde.postAsyncImageGenerate( | ||||||
|  |             { | ||||||
|  |                 prompt: `${request.body.prompt} ### ${request.body.negative_prompt}`, | ||||||
|  |                 params: | ||||||
|  |                 { | ||||||
|  |                     sampler_name: request.body.sampler, | ||||||
|  |                     hires_fix: request.body.enable_hr, | ||||||
|  |                     // @ts-ignore - use_gfpgan param is not in the type definition, need to update to new ai_horde @ https://github.com/ZeldaFan0225/ai_horde/blob/main/index.ts | ||||||
|  |                     use_gfpgan: request.body.restore_faces, | ||||||
|  |                     cfg_scale: request.body.scale, | ||||||
|  |                     steps: request.body.steps, | ||||||
|  |                     width: request.body.width, | ||||||
|  |                     height: request.body.height, | ||||||
|  |                     karras: Boolean(request.body.karras), | ||||||
|  |                     n: 1, | ||||||
|  |                 }, | ||||||
|  |                 r2: false, | ||||||
|  |                 nsfw: request.body.nfsw, | ||||||
|  |                 models: [request.body.model], | ||||||
|  |             }, | ||||||
|  |             { token: api_key_horde }); | ||||||
|  |  | ||||||
|  |         if (!generation.id) { | ||||||
|  |             console.error('Image generation request is not satisfyable:', generation.message || 'unknown error'); | ||||||
|  |             return response.sendStatus(400); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { | ||||||
|  |             await delay(CHECK_INTERVAL); | ||||||
|  |             const check = await ai_horde.getImageGenerationCheck(generation.id); | ||||||
|  |             console.log(check); | ||||||
|  |  | ||||||
|  |             if (check.done) { | ||||||
|  |                 const result = await ai_horde.getImageGenerationStatus(generation.id); | ||||||
|  |                 if (result.generations === undefined) return response.sendStatus(500); | ||||||
|  |                 return response.send(result.generations[0].img); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             /* | ||||||
|  |             if (!check.is_possible) { | ||||||
|  |                 return response.sendStatus(503); | ||||||
|  |             } | ||||||
|  |             */ | ||||||
|  |  | ||||||
|  |             if (check.faulted) { | ||||||
|  |                 return response.sendStatus(500); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return response.sendStatus(504); | ||||||
|  |     } catch (error) { | ||||||
|  |         console.error(error); | ||||||
|  |         return response.sendStatus(500); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | module.exports = { router }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user