mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Merge pull request #342 from 10sa/chara-card-src-refactor
Refactoring character card parse scripts
This commit is contained in:
11
.editorconfig
Normal file
11
.editorconfig
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[*.{js, conf, json}]
|
||||||
|
charset = utf-8
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
@@ -10,6 +10,11 @@ const enableExtensions = true; //Enables support for TavernAI-extras project
|
|||||||
const listen = true; // If true, Can be access from other device or PC. otherwise can be access only from hosting machine.
|
const listen = true; // If true, Can be access from other device or PC. otherwise can be access only from hosting machine.
|
||||||
const allowKeysExposure = false; // If true, private API keys could be fetched to the frontend.
|
const allowKeysExposure = false; // If true, private API keys could be fetched to the frontend.
|
||||||
|
|
||||||
|
|
||||||
|
// If true, Allows insecure settings for listen, whitelist, and authentication.
|
||||||
|
// Change this setting only on "trusted networks". Do not change this value unless you are aware of the issues that can arise from changing this setting and configuring a insecure setting.
|
||||||
|
const securityOverride = false;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
port,
|
port,
|
||||||
whitelist,
|
whitelist,
|
||||||
@@ -21,4 +26,5 @@ module.exports = {
|
|||||||
listen,
|
listen,
|
||||||
disableThumbnails,
|
disableThumbnails,
|
||||||
allowKeysExposure,
|
allowKeysExposure,
|
||||||
|
securityOverride,
|
||||||
};
|
};
|
||||||
|
66
server.js
66
server.js
@@ -63,7 +63,9 @@ const utf8Encode = new TextEncoder();
|
|||||||
const utf8Decode = new TextDecoder('utf-8', { ignoreBOM: true });
|
const utf8Decode = new TextDecoder('utf-8', { ignoreBOM: true });
|
||||||
const commandExistsSync = require('command-exists').sync;
|
const commandExistsSync = require('command-exists').sync;
|
||||||
|
|
||||||
|
const characterCardParser = require('./src/character-card-parser.js');
|
||||||
const config = require(path.join(process.cwd(), './config.conf'));
|
const config = require(path.join(process.cwd(), './config.conf'));
|
||||||
|
|
||||||
const server_port = process.env.SILLY_TAVERN_PORT || config.port;
|
const server_port = process.env.SILLY_TAVERN_PORT || config.port;
|
||||||
|
|
||||||
const whitelistPath = path.join(process.cwd(), "./whitelist.txt");
|
const whitelistPath = path.join(process.cwd(), "./whitelist.txt");
|
||||||
@@ -913,61 +915,7 @@ async function tryReadImage(img_url, crop) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function charaRead(img_url, input_format) {
|
async function charaRead(img_url, input_format) {
|
||||||
let format;
|
return characterCardParser.parse(img_url, input_format);
|
||||||
if (input_format === undefined) {
|
|
||||||
if (img_url.indexOf('.webp') !== -1) {
|
|
||||||
format = 'webp';
|
|
||||||
} else {
|
|
||||||
format = 'png';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
format = input_format;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (format) {
|
|
||||||
case 'webp':
|
|
||||||
try {
|
|
||||||
const exif_data = await ExifReader.load(fs.readFileSync(img_url));
|
|
||||||
let char_data;
|
|
||||||
|
|
||||||
if (exif_data['UserComment']['description']) {
|
|
||||||
let description = exif_data['UserComment']['description'];
|
|
||||||
if (description === 'Undefined' && exif_data['UserComment'].value && exif_data['UserComment'].value.length === 1) {
|
|
||||||
description = exif_data['UserComment'].value[0];
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
json5.parse(description);
|
|
||||||
char_data = description;
|
|
||||||
} catch {
|
|
||||||
const byteArr = description.split(",").map(Number);
|
|
||||||
const uint8Array = new Uint8Array(byteArr);
|
|
||||||
const char_data_string = utf8Decode.decode(uint8Array);
|
|
||||||
char_data = char_data_string;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log('No description found in EXIF data.');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return char_data;
|
|
||||||
}
|
|
||||||
catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
case 'png':
|
|
||||||
const buffer = fs.readFileSync(img_url);
|
|
||||||
const chunks = extract(buffer);
|
|
||||||
|
|
||||||
const textChunks = chunks.filter(function (chunk) {
|
|
||||||
return chunk.name === 'tEXt';
|
|
||||||
}).map(function (chunk) {
|
|
||||||
return PNGtext.decode(chunk.data);
|
|
||||||
});
|
|
||||||
var base64DecodedData = Buffer.from(textChunks[0].text, 'base64').toString('utf8');
|
|
||||||
return base64DecodedData;//textChunks[0].text;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.post("/getcharacters", jsonParser, function (request, response) {
|
app.post("/getcharacters", jsonParser, function (request, response) {
|
||||||
@@ -2752,8 +2700,12 @@ const setupTasks = async function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (listen && !config.whitelistMode && !config.basicAuthMode) {
|
if (listen && !config.whitelistMode && !config.basicAuthMode) {
|
||||||
console.error('Your SillyTavern is currently unsecurely open to the public. Enable whitelisting or basic authentication.');
|
if (config.securityOverride)
|
||||||
process.exit(1);
|
console.warn("Security has been override. If it's not a trusted network, change the settings.");
|
||||||
|
else {
|
||||||
|
console.error('Your SillyTavern is currently unsecurely open to the public. Enable whitelisting or basic authentication.');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (true === cliArguments.ssl)
|
if (true === cliArguments.ssl)
|
||||||
|
72
src/character-card-parser.js
Normal file
72
src/character-card-parser.js
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const json5 = require('json5');
|
||||||
|
const ExifReader = require('exifreader');
|
||||||
|
|
||||||
|
const extract = require('png-chunks-extract');
|
||||||
|
const PNGtext = require('png-chunk-text');
|
||||||
|
|
||||||
|
const utf8Decode = new TextDecoder('utf-8', { ignoreBOM: true });
|
||||||
|
|
||||||
|
const parse = async (cardUrl, format) => {
|
||||||
|
let fileFormat;
|
||||||
|
if (format === undefined) {
|
||||||
|
if (cardUrl.indexOf('.webp') !== -1)
|
||||||
|
fileFormat = 'webp';
|
||||||
|
else
|
||||||
|
fileFormat = 'png';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
fileFormat = format;
|
||||||
|
|
||||||
|
switch (fileFormat) {
|
||||||
|
case 'webp':
|
||||||
|
try {
|
||||||
|
const exif_data = await ExifReader.load(fs.readFileSync(cardUrl));
|
||||||
|
let char_data;
|
||||||
|
|
||||||
|
if (exif_data['UserComment']['description']) {
|
||||||
|
let description = exif_data['UserComment']['description'];
|
||||||
|
if (description === 'Undefined' && exif_data['UserComment'].value && exif_data['UserComment'].value.length === 1) {
|
||||||
|
description = exif_data['UserComment'].value[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
json5.parse(description);
|
||||||
|
char_data = description;
|
||||||
|
} catch {
|
||||||
|
const byteArr = description.split(",").map(Number);
|
||||||
|
const uint8Array = new Uint8Array(byteArr);
|
||||||
|
const char_data_string = utf8Decode.decode(uint8Array);
|
||||||
|
char_data = char_data_string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.log('No description found in EXIF data.');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return char_data;
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
case 'png':
|
||||||
|
const buffer = fs.readFileSync(cardUrl);
|
||||||
|
const chunks = extract(buffer);
|
||||||
|
|
||||||
|
const textChunks = chunks.filter(function (chunk) {
|
||||||
|
return chunk.name === 'tEXt';
|
||||||
|
}).map(function (chunk) {
|
||||||
|
return PNGtext.decode(chunk.data);
|
||||||
|
});
|
||||||
|
|
||||||
|
return Buffer.from(textChunks[0].text, 'base64').toString('utf8');
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
parse: parse
|
||||||
|
};
|
Reference in New Issue
Block a user