57 lines
1.0 KiB
PHP
57 lines
1.0 KiB
PHP
<?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;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|