-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2636 from IntersectMBO/fix/2633-cip-119-drep-imag…
…es-base64-encoded-image-is-not-supported feat(#2633): add support for base64 encoded images
- Loading branch information
Showing
6 changed files
with
211 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Checks if a given string is a base64-encoded image or SVG and returns its details. | ||
* | ||
* @param str - The string to check. | ||
* @returns An object with the MIME type, base64 prefix, and validity flag. | ||
*/ | ||
export function getBase64ImageDetails(str: string): { | ||
type: string | null; | ||
base64Prefix: string | null; | ||
isValidBase64Image: boolean; | ||
} { | ||
if (!str || typeof str !== "string") { | ||
return { | ||
type: null, | ||
base64Prefix: null, | ||
isValidBase64Image: false, | ||
}; | ||
} | ||
|
||
try { | ||
const decoded = atob(str); | ||
|
||
if (decoded.trim().startsWith("<?xml") || decoded.includes("<svg")) { | ||
return { | ||
type: "svg+xml", | ||
base64Prefix: "data:image/svg+xml;base64,", | ||
isValidBase64Image: true, | ||
}; | ||
} | ||
|
||
const magicNumbers = decoded.slice(0, 4); | ||
let type: string | null = null; | ||
|
||
switch (magicNumbers) { | ||
case "\x89PNG": | ||
type = "png"; | ||
break; | ||
case "\xFF\xD8\xFF": | ||
type = "jpeg"; | ||
break; | ||
case "GIF8": | ||
type = "gif"; | ||
break; | ||
default: | ||
type = null; | ||
} | ||
|
||
if (type) { | ||
return { | ||
type, | ||
base64Prefix: `data:image/${type};base64,`, | ||
isValidBase64Image: true, | ||
}; | ||
} | ||
} catch { | ||
return { | ||
type: null, | ||
base64Prefix: null, | ||
isValidBase64Image: false, | ||
}; | ||
} | ||
|
||
return { | ||
type: null, | ||
base64Prefix: null, | ||
isValidBase64Image: false, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.