Refactor base64 validation and decoding in minio_t to handle data URI prefixes more robustly.
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user