initial: bootstrap from BukidBountyApp base
This commit is contained in:
56
app/Http/Controllers/Helpers/ObjectToBase64Helper.php
Normal file
56
app/Http/Controllers/Helpers/ObjectToBase64Helper.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Helpers;
|
||||
|
||||
|
||||
use App\Models\FileList;
|
||||
use Hypervel\Http\Request;
|
||||
use App\Http\Controllers\FilesMainController;
|
||||
|
||||
class ObjectToBase64Helper
|
||||
{
|
||||
|
||||
|
||||
public static function urlSafeBase64ToObject(string $urlSafeBase64, bool $compressed = true)
|
||||
{
|
||||
|
||||
if (!is_string($urlSafeBase64)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
$base64 = str_replace(['-', '_'], ['+', '/'], $urlSafeBase64);
|
||||
|
||||
|
||||
$padding = strlen($base64) % 4;
|
||||
if ($padding !== 0) {
|
||||
$base64 .= str_repeat('=', 4 - $padding);
|
||||
}
|
||||
|
||||
// Base64 decode
|
||||
$binary = base64_decode($base64, true);
|
||||
if ($binary === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
if ($compressed) {
|
||||
// Equivalent of pako.inflate()
|
||||
$json = gzuncompress($binary);
|
||||
if ($json === false) {
|
||||
return null;
|
||||
}
|
||||
return json_decode($json, true);
|
||||
}
|
||||
|
||||
return json_decode($binary, true);
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user