Handles images without extensions correctly
Ensures images without file extensions are properly handled by appending ".png" to the filename, preventing potential issues with image processing or display. Preserves existing extensions for other image types (e.g., svg, ico, avif) without forcing a ".png" extension.
This commit is contained in:
1 file changed
+8
-5
@@ -91,14 +91,17 @@ export class EmbedProcessor {
|
|||||||
// Parse URL to get filename
|
// Parse URL to get filename
|
||||||
const urlObj = new URL(imageUrl);
|
const urlObj = new URL(imageUrl);
|
||||||
const urlPath = urlObj.pathname;
|
const urlPath = urlObj.pathname;
|
||||||
const fileName = path.basename(urlPath) || `remote-image-${Date.now()}.png`;
|
const fileName = path.basename(urlPath) || `remote-image-${Date.now()}`;
|
||||||
|
|
||||||
// Ensure we have a valid file extension
|
// Ensure we have a valid file extension
|
||||||
|
// Extract extension and normalize it (lowercase)
|
||||||
const ext = path.extname(fileName).toLowerCase();
|
const ext = path.extname(fileName).toLowerCase();
|
||||||
const validExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.webp', '.bmp', '.tiff'];
|
|
||||||
const finalFileName = validExtensions.includes(ext)
|
// Preserve any existing extension, or append .png if no extension present
|
||||||
? fileName
|
// This handles all image types (svg, ico, avif, etc.) without forcing .png
|
||||||
: `${fileName}.png`;
|
const finalFileName = ext
|
||||||
|
? fileName // Has an extension, preserve it
|
||||||
|
: `${fileName}.png`; // No extension, append .png
|
||||||
|
|
||||||
const outputPath = path.join(tempDir, finalFileName);
|
const outputPath = path.join(tempDir, finalFileName);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user