| 540 | 540 | |
| 541 | 541 | if (result.ok) { |
| 542 | 542 | const buffer = Buffer.from(await result.arrayBuffer()); |
| 543 | 543 | constlet fileName = sanitize(result.url.split('?')[0].split('/').reverse()[0]); |
| 544 | 544 | const contentType = result.headers.get('content-type') || 'image/png'; //yoink it from AICC function lol |
| 545 | 545 | |
| 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 | 558 | return { |
| 547 | 559 | buffer: buffer, |
| 548 | 560 | fileName: fileName, |