downloadGenericPng() handle missing file PNG extension, log generic import url, add error message for when generic import site is not whitelisted

1a1464800f35f23e94f7907f31d23ab9090e8b40

Cyberes <64224601+Cyberes@users.noreply.github.com>

1 files changed, +16 -2Showing whitespace changes
src/endpoints/content-manager.js+16 -2
@@ -540,9 +540,21 @@ async function downloadGenericPng(url) {
540540
541541 if (result.ok) {
542542 const buffer = Buffer.from(await result.arrayBuffer());
543543 constlet fileName = sanitize(result.url.split('?')[0].split('/').reverse()[0]);
544544 const contentType = result.headers.get('content-type') || 'image/png'; //yoink it from AICC function lol
545545
546+ // The `importCharacter()` function detects the MIME (content-type) of the file
547+ // using its file extension. The problem is that not all third-party APIs serve
548+ // their cards with a `.png` extension. To support more third-party sites,
549+ // dynamically append the `.png` extension to the filename if it doesn't
550+ // already have a file extension.
551+ if (contentType === 'image/png') {
552+ const ext = fileName.match(/\.(\w+)$/); // Same regex used by `importCharacter()`
553+ if (!ext) {
554+ fileName += '.png';
555+ }
556+ }
557+
546558 return {
547559 buffer: buffer,
548560 fileName: fileName,
@@ -694,10 +706,12 @@ router.post('/importURL', async (request, response) => {
694706 type = 'character';
695707 result = await downloadRisuCharacter(uuid);
696708 } else if (isGeneric) {
697709 console.info('Downloading from generic url.:', url);
698710 type = 'character';
699711 result = await downloadGenericPng(url);
700712 } else {
713+ const receivedURL = new URL(url);
714+ 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.`);
701715 return response.sendStatus(404);
702716 }
703717