mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
downloadGenericPng() handle missing file PNG extension, log generic import url, add error message for when generic import site is not whitelisted
This commit is contained in:
@@ -540,9 +540,21 @@ async function downloadGenericPng(url) {
|
|||||||
|
|
||||||
if (result.ok) {
|
if (result.ok) {
|
||||||
const buffer = Buffer.from(await result.arrayBuffer());
|
const buffer = Buffer.from(await result.arrayBuffer());
|
||||||
const fileName = sanitize(result.url.split('?')[0].split('/').reverse()[0]);
|
let fileName = sanitize(result.url.split('?')[0].split('/').reverse()[0]);
|
||||||
const contentType = result.headers.get('content-type') || 'image/png'; //yoink it from AICC function lol
|
const contentType = result.headers.get('content-type') || 'image/png'; //yoink it from AICC function lol
|
||||||
|
|
||||||
|
// The `importCharacter()` function detects the MIME (content-type) of the file
|
||||||
|
// using its file extension. The problem is that not all third-party APIs serve
|
||||||
|
// their cards with a `.png` extension. To support more third-party sites,
|
||||||
|
// dynamically append the `.png` extension to the filename if it doesn't
|
||||||
|
// already have a file extension.
|
||||||
|
if (contentType === 'image/png') {
|
||||||
|
const ext = fileName.match(/\.(\w+)$/); // Same regex used by `importCharacter()`
|
||||||
|
if (!ext) {
|
||||||
|
fileName += '.png';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
buffer: buffer,
|
buffer: buffer,
|
||||||
fileName: fileName,
|
fileName: fileName,
|
||||||
@@ -694,10 +706,12 @@ router.post('/importURL', async (request, response) => {
|
|||||||
type = 'character';
|
type = 'character';
|
||||||
result = await downloadRisuCharacter(uuid);
|
result = await downloadRisuCharacter(uuid);
|
||||||
} else if (isGeneric) {
|
} else if (isGeneric) {
|
||||||
console.info('Downloading from generic url.');
|
console.info('Downloading from generic url:', url);
|
||||||
type = 'character';
|
type = 'character';
|
||||||
result = await downloadGenericPng(url);
|
result = await downloadGenericPng(url);
|
||||||
} else {
|
} else {
|
||||||
|
const receivedURL = new URL(url);
|
||||||
|
console.error(`Received an import for "${receivedURL.host}", but site is not whitelisted. This domain must be added to the config key "whitelistImportDomains" to allow import from this source.`);
|
||||||
return response.sendStatus(404);
|
return response.sendStatus(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user