mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
139 lines
4.0 KiB
PHP
139 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Documents\Services;
|
|
|
|
use App\Classes\Exceptions\MalformedRequestException;
|
|
use App\Classes\Modules\Documents\DataTransferObjects\FileObject;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ConvertsBase64ToFile
|
|
{
|
|
|
|
/** @var string|null */
|
|
private $path;
|
|
|
|
/** @var array */
|
|
private $filesInfo;
|
|
|
|
/**
|
|
* ConvertsBase64ToFile constructor.
|
|
* @param null|string $path
|
|
*/
|
|
public function __construct(?string $path = 'documents')
|
|
{
|
|
$this->path = $path;
|
|
$this->filesInfo = [];
|
|
}
|
|
|
|
|
|
/**
|
|
* @param array $files
|
|
* @return array
|
|
* @throws MalformedRequestException
|
|
*/
|
|
public function convert($files = [])
|
|
{
|
|
foreach ($files as $file) {
|
|
|
|
$object = new FileObject($file);
|
|
in_array($object->getExtension(), ['pdf', 'excel', 'text']) ? $this->generatePDF($object) : $this->generateImage($object);
|
|
|
|
}
|
|
|
|
return $this->filesInfo;
|
|
|
|
}
|
|
|
|
/**
|
|
* @param FileObject $file
|
|
* @throws MalformedRequestException
|
|
*/
|
|
private function generatePDF(FileObject $file)
|
|
{
|
|
|
|
$filePath = $this->generateFile($file);
|
|
$this->updateFiles($file, ['original' => ['file' => $filePath]]);
|
|
}
|
|
|
|
/**
|
|
* @param FileObject $file
|
|
* @throws MalformedRequestException
|
|
*/
|
|
private function generateImage(FileObject $file)
|
|
{
|
|
|
|
$fileInfo = [];
|
|
|
|
foreach (['original' => null, 'large' => 800, 'medium' => 480, 'small' => 320] as $size => $value) {
|
|
$suffix = $size !== 'original' ? '_' . $size : '';
|
|
|
|
if ($size !== 'original') {
|
|
// intervention/image v2: old
|
|
// $thumbnail = $file->getData()->widen($value, function ($constraint) {
|
|
// $constraint->upsize();
|
|
// })->heighten($value, function ($constraint) {
|
|
// $constraint->upsize();
|
|
// });
|
|
// $file->setData($thumbnail->encode('data-url')->encoded);
|
|
|
|
|
|
// intervention/image v3: Use scaleDown() instead of widen()/heighten()
|
|
// scaleDown() maintains aspect ratio and prevents upsizing by default
|
|
$thumbnail = $file->getData()->scaleDown(width: $value, height: $value);
|
|
|
|
// intervention/image v3: Use encode()->toDataUri() instead of encode('data-url')->encoded
|
|
$file->setData($thumbnail->encode()->toDataUri());
|
|
}
|
|
|
|
|
|
$filePath = $this->generateFile($file, $suffix);
|
|
|
|
$fileInfo[] = [$size => ['file' => $filePath, 'width' => $file->getData()->width(), 'height' => $file->getData()->height()]];
|
|
|
|
|
|
}
|
|
|
|
$this->updateFiles($file, $fileInfo);
|
|
|
|
}
|
|
|
|
/**
|
|
* @param FileObject $file
|
|
* @param string $suffix
|
|
* @return string
|
|
* @throws MalformedRequestException
|
|
*/
|
|
private function generateFile(FileObject $file, string $suffix = '')
|
|
{
|
|
|
|
$filesystemDriver = Storage::getDefaultDriver();
|
|
if ($filesystemDriver === 's3') {
|
|
$filePath = 'documents/' . $this->path . '/' . $file->getFileName() . $suffix . '.' . $file->getExtension();
|
|
Storage::put($filePath, $file->getDecodedData(), 's3');
|
|
return $filePath;
|
|
} else {
|
|
$filePath = $this->path . '/' . $file->getFileName() . $suffix . '.' . $file->getExtension();
|
|
Storage::disk('documents')->put($filePath, $file->getDecodedData());
|
|
return $filePath;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param FileObject $file
|
|
* @param array $fileInfo
|
|
* @throws MalformedRequestException
|
|
*/
|
|
private function updateFiles(FileObject $file, array $fileInfo)
|
|
{
|
|
$this->filesInfo[] = json_encode([
|
|
'path' => $this->path,
|
|
'filename' => $file->getFileName() . '.' . $file->getExtension(),
|
|
'mime_type' => $file->getMimeType(),
|
|
'extension' => $file->getExtension(),
|
|
'file_info' => $fileInfo
|
|
]);
|
|
}
|
|
|
|
}
|