Refactor base64 validation and decoding in minio_t to handle data URI prefixes more robustly.

This commit is contained in:
Jeppe Bundgaard
2026-01-09 11:01:01 +01:00
parent 5b364e2141
commit 10c1631f51
+16 -5
View File
@@ -198,11 +198,17 @@ trait minio_t
public function storeTempFileFromBase64(string $base64, string $fileExtension = 'txt'): string|bool
{
// Check if the base64 string is valid
if (empty($base64) || !preg_match('/^data:.*;base64,/', $base64)) {
if (empty($base64)) {
return false; // Invalid base64 string
}
// Check if the base64 string has a data URI prefix
if (preg_match('/^data:.*;base64,/', $base64)) {
$base64 = preg_replace('/^data:.*;base64,/', '', $base64);
}
// Decode the base64 data
$fileData = base64_decode(preg_replace('/^data:.*;base64,/', '', $base64));
$fileData = base64_decode($base64);
if ($fileData === false) {
return false; // Failed to decode base64 data
}
@@ -236,11 +242,16 @@ trait minio_t
public function storeTempImageFromBase64(string $base64): string|bool
{
// Check if the base64 string is valid
if (empty($base64) || !preg_match('/^data:image\/(\w+);base64,/', $base64)) {
if (empty($base64)) {
return false; // Invalid base64 string
}
// Extract the base64 data from the string
$imageData = preg_replace('/^data:image\/\w+;base64,/', '', $base64);
// Extract the base64 data from the string if it has a data URI prefix
$imageData = $base64;
if (preg_match('/^data:image\/(\w+);base64,/', $base64)) {
$imageData = preg_replace('/^data:image\/\w+;base64,/', '', $base64);
}
// Decode the base64 data
$imageData = base64_decode($imageData);
if ($imageData === false) {