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 -2Ignore whitespace
src/endpoints/content-manager.js+16 -2
@@ -540,9 +540,21 @@ async function downloadGenericPng(url) {
540540
541 if (result.ok) {541 if (result.ok) {
542 const buffer = Buffer.from(await result.arrayBuffer());542 const buffer = Buffer.from(await result.arrayBuffer());
543 const fileName = sanitize(result.url.split('?')[0].split('/').reverse()[0]);543 let fileName = sanitize(result.url.split('?')[0].split('/').reverse()[0]);
544 const contentType = result.headers.get('content-type') || 'image/png'; //yoink it from AICC function lol544 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
546 return {558 return {
547 buffer: buffer,559 buffer: buffer,
548 fileName: fileName,560 fileName: fileName,
@@ -694,10 +706,12 @@ router.post('/importURL', async (request, response) => {
694 type = 'character';706 type = 'character';
695 result = await downloadRisuCharacter(uuid);707 result = await downloadRisuCharacter(uuid);
696 } else if (isGeneric) {708 } else if (isGeneric) {
697 console.info('Downloading from generic url.');709 console.info('Downloading from generic url:', url);
698 type = 'character';710 type = 'character';
699 result = await downloadGenericPng(url);711 result = await downloadGenericPng(url);
700 } else {712 } 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.`);
701 return response.sendStatus(404);715 return response.sendStatus(404);
702 }716 }
703717