mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/izyim.git
synced 2026-08-19 20:44:03 +00:00
8a28eb1790
This reverts merge request !5
55 lines
1.3 KiB
PHP
55 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Service\Files;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
use App\Classes\Constants;
|
|
|
|
class Base64Uploader
|
|
{
|
|
public function execute( string $path, ?string $base64) : ?string {
|
|
|
|
if($base64 !== null){
|
|
|
|
$type = $this->getFileType($base64);
|
|
|
|
$extension = $this->getExtension($base64, $type);
|
|
|
|
$uniqueName = uniqid().time();
|
|
|
|
$imagePath = $path.'/'.$uniqueName.'.'.$extension;
|
|
|
|
$encodedExtension = $extension;
|
|
|
|
if($extension === Constants::EXCEL_FILE_EXTENSION){
|
|
$encodedExtension = Constants::EXCEL_FILE_ENCODED_EXTENSION;
|
|
}
|
|
|
|
$data = base64_decode(str_replace(' ', '+', str_replace('data:'.$type.'/'.$encodedExtension.';base64,', '', $base64)));
|
|
|
|
|
|
Storage::disk('public')->put($imagePath, $data);
|
|
|
|
return $uniqueName.'.'.$extension;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
private function getFileType(string $base64){
|
|
return explode('/', explode(':', substr($base64, 0, strpos($base64, ';')))[1])[0];
|
|
}
|
|
|
|
private function getExtension(string $base64, string $type) {
|
|
$extension = explode('/', explode(':', substr($base64, 0, strpos($base64, ';')))[1])[1];
|
|
|
|
if($extension === Constants::EXCEL_FILE_ENCODED_EXTENSION){
|
|
return Constants::EXCEL_FILE_EXTENSION;
|
|
}
|
|
|
|
return $extension;
|
|
}
|
|
}
|