External extension support start

This commit is contained in:
BlipRanger
2023-07-11 16:10:42 -04:00
parent 040380e0fb
commit bb6ff352b3
3 changed files with 105 additions and 0 deletions

View File

@@ -2853,6 +2853,7 @@
<div id="rm_button_create" title="Create New Character" class="menu_button fa-solid fa-user-plus "></div> <div id="rm_button_create" title="Create New Character" class="menu_button fa-solid fa-user-plus "></div>
<div id="character_import_button" title="Import Character from File" class="menu_button fa-solid fa-file-arrow-up faSmallFontSquareFix"></div> <div id="character_import_button" title="Import Character from File" class="menu_button fa-solid fa-file-arrow-up faSmallFontSquareFix"></div>
<div id="external_import_button" title="Import content from external URL" class="menu_button fa-solid fa-cloud-arrow-down faSmallFontSquareFix"></div> <div id="external_import_button" title="Import content from external URL" class="menu_button fa-solid fa-cloud-arrow-down faSmallFontSquareFix"></div>
<div id="thrid_party_extension_button" title="Import extension from git repo" class="menu_button fa-solid fa-cloud-arrow-down faSmallFontSquareFix"></div>
<div id="rm_button_group_chats" title="Create New Chat Group" class="menu_button fa-solid fa-users-gear "></div> <div id="rm_button_group_chats" title="Create New Chat Group" class="menu_button fa-solid fa-users-gear "></div>
<input id="character_search_bar" class="text_pole width100p" type="search" placeholder="Search..." maxlength="50" /> <input id="character_search_bar" class="text_pole width100p" type="search" placeholder="Search..." maxlength="50" />
<select id="character_sort_order" title="Characters sorting order"> <select id="character_sort_order" title="Characters sorting order">

View File

@@ -8494,6 +8494,38 @@ $(document).ready(function () {
} }
}); });
$('#thrid_party_extension_button').on('click', async () => {
const html = `<h3>Enter the URL of the content to import</h3>
Supported sources:<br>
<ul class="justifyLeft">
<li>Chub characters (direct link or id)<br>Example: <tt>Anonymous/example-character</tt></li>
<li>Chub lorebooks (direct link or id)<br>Example: <tt>lorebooks/bartleby/example-lorebook</tt></li>
<li>More coming soon...</li>
<ul>`
const input = await callPopup(html, 'input');
if (!input) {
console.debug('Custom content import cancelled');
return;
}
const url = input.trim();
console.debug('Custom content import started', url);
const request = await fetch('/get_extension', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ url }),
});
if (!request.ok) {
toastr.info(request.statusText, 'Custom content import failed');
console.error('Custom content import failed', request.status, request.statusText);
return;
}
});
const $dropzone = $(document.body); const $dropzone = $(document.body);
$dropzone.on('dragover', (event) => { $dropzone.on('dragover', (event) => {

View File

@@ -4345,3 +4345,75 @@ async function getImageBuffers(zipFilePath) {
}); });
}); });
} }
/**
* This function is used ensure the list of extensions is up to date.
* @param {object} extensionList
* @returns
*/
function getExtensions(extensionList) {
const extensions = [];
// if the extension folder does not exist, git clone the extension, also check if the extension is up to date with the git repo, but don't pull if it is not up to date
for (const extension of extensionList) {
const extensionPath = path.join(directories.extensions, extension.name);
if (!fs.existsSync(extensionPath)) {
console.log(`Extension ${extension.name} does not exist. Cloning...`);
try {
git.clone(extension.url, extensionPath);
} catch (error) {
console.error(`Failed to clone extension ${extension.name}`);
console.error(error);
}
} else {
console.log(`Extension ${extension.name} exists
Checking if extension is up to date...`);
try {
git.fetch(extensionPath);
const status = git.status(extensionPath);
if (status.behind > 0) {
console.log(`Extension ${extension.name} is not up to date. Pulling...`);
git.pull(extensionPath);
}
} catch (error) {
console.error(`Failed to check if extension ${extension.name} is up to date`);
console.error(error);
}
}
extensions.push(extension.name);
}
}
/**
* This function is used to git clone a single extension into the thrid-party-extensions folder
*/
app.post('/get_extension', jsonParser, async (request, response) => {
if (!request.body.url) {
return response.sendStatus(400);
}
try {
const url = request.body.url;
let result;
// git clone and then get the resulting folder path of the extension
const extensionPath = git.clone(url, directories.extensions + '/third-party');
// load the info from the manifest.json in the extension folder
const manifest = JSON.parse(fs.readFileSync(extensionPath + '/manifest.json', 'utf8'));
// pull version, author, display_name
const version = manifest.version;
const author = manifest.author;
const display_name = manifest.display_name;
console.log(`Extension ${display_name} has been cloned`);
// return the version, author, display_name, and the path to the extension folder
return response.send({ version, author, display_name, extensionPath });
} catch (error) {
console.log('Importing custom content failed', error);
return response.sendStatus(500);
}
});