Merge pull request #3884 from Cyberes/improve-generic-import downloadGenericPng() handle missing file PNG extension

fc892b4514dff95fdcfde952b911f1432240d449

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +15 -2Showing whitespace changes
src/endpoints/content-manager.js+15 -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,11 @@ 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+ console.error(`Received an import for "${getHostFromUrl(url)}", but site is not whitelisted. This domain must be added to the config key "whitelistImportDomains" to allow import from this source.`);
701714 return response.sendStatus(404);
702715 }
703716