Files
lelong/app/Services/MarhunPhotoService.php
T
ismailmasseran e29a5891c8 Bugfix/list clickup issue (#5)
Reviewed-on: #5
Co-authored-by: ISMAIL MASSERAN <ismail@koppkb.com>
Co-committed-by: ISMAIL MASSERAN <ismail@koppkb.com>
2026-10-19 13:30:36 +08:00

155 lines
5.4 KiB
PHP

<?php
namespace App\Services;
use App\Models\FileMarhun;
use App\Models\LelongBid;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class MarhunPhotoService
{
public function store(Request $request)
{
try {
$file_type = $request->gambarMarhun->getClientOriginalExtension();
} catch (Exception $exception) {
$file_type = 'PNG';
logger($exception);
}
$addfile = new FileMarhun;
$addfile->lelongbid_id = $request->lelongbid_id;
$addfile->file_type = $file_type;
if ($request->photo_type == 1) {
$addfile->photo_type = 1;
$addfile->save();
} elseif ($request->photo_type == 2) {
$addfile->photo_type = 2;
$addfile->lelongbidmarhun_id = $request->lelongbidmarhun_id;
$addfile->save();
}
$no_rujukan_sag = LelongBid::select('norujukan')->where('id', $request->lelongbid_id)->first()->norujukan;
$photo_type_name = $request->photo_type === '1' ? 'overall_photo' : 'by_marhun_photo';
$directory = "/{$no_rujukan_sag}/{$photo_type_name}/";
$imageExtension = $file_type;
$marhunImageFileName = 'overall';
if (! empty($request->lelongbidmarhun_id)) {
$marhunImageFileName = "$request->lelongbidmarhun_id";
}
Storage::disk('marhun')->putFileAs($directory, $request->gambarMarhun, "${marhunImageFileName}.${imageExtension}");
$stored_path = Storage::disk('marhun')->path("{$directory}{$marhunImageFileName}.${imageExtension}");
$this->optimizeImageToWebP($stored_path, $directory, $marhunImageFileName);
Storage::disk('marhun')->delete("{$directory}${marhunImageFileName}.${imageExtension}");
$addfile->update([
'file_type' => 'webp',
]);
}
public function delete(Request $request)
{
$deleteFile = FileMarhun::where('id', '=', $request->filemarhun_id)->first();
$no_rujukan_sag = LelongBid::select('norujukan')->where('id', $request->lelongbid_id)->first()->norujukan;
$photo_type_name = $request->photo_type === '1' ? 'overall_photo' : 'by_marhun_photo';
$directory = "/{$no_rujukan_sag}/{$photo_type_name}/";
$imageExtension = $deleteFile->file_type;
$marhunImageFileName = "overall.{$imageExtension}";
$lelongbidmarhun_id = $deleteFile->lelongbidmarhun_id;
if (! empty($request->lelongbidmarhun_id)) {
$marhunImageFileName = "$lelongbidmarhun_id.$imageExtension";
}
Storage::disk('marhun')->delete("{$directory}${marhunImageFileName}");
$deleteFile->delete();
}
public function show($norujukan_sag, $marhun)
{
$lelongbidmarhun_id = $marhun->lelongbidmarhun_id;
$photo_type_name = $marhun->photo_type === 1 ? 'overall_photo' : 'by_marhun_photo';
$directory = "/{$norujukan_sag}/{$photo_type_name}/";
$imageExtension = $marhun->file_type;
$marhunImageFileName = "overall.{$imageExtension}";
if (! empty($lelongbidmarhun_id)) {
$marhunImageFileName = "$lelongbidmarhun_id.$imageExtension";
}
$filePath = Storage::disk('marhun')->path("{$directory}${marhunImageFileName}");
header('Content-Type: '.mime_content_type($filePath));
header('Content-Disposition: inline; filename="'.basename($filePath).'"');
readfile($filePath);
}
public function get($norujukan_sag, $marhun)
{
$lelongbidmarhun_id = $marhun->lelongbidmarhun_id;
$photo_type_name = $marhun->photo_type === 1 ? 'overall_photo' : 'by_marhun_photo';
$directory = "/{$norujukan_sag}/{$photo_type_name}/";
$imageExtension = $marhun->file_type;
$marhunImageFileName = "overall.{$imageExtension}";
if (! empty($lelongbidmarhun_id)) {
$marhunImageFileName = "$lelongbidmarhun_id.$imageExtension";
}
return Storage::disk('marhun')->get("{$directory}${marhunImageFileName}");
}
private function getWebpCompilerByOS()
{
$DS = DIRECTORY_SEPARATOR;
$cwebp_compilers = [
'Windows' => 'binaries'.$DS.'libwebp-1.2.4-windows-x64'.$DS.'bin'.$DS.'cwebp',
'Linux' => 'binaries'.$DS.'libwebp-1.2.4-linux-x86-64'.$DS.'bin'.$DS.'cwebp',
'Darwin' => [
'X86-64' => 'binaries'.$DS.'libwebp-1.2.4-mac-X86-64'.$DS.'bin'.$DS.'cwebp',
'arm64' => 'binaries'.$DS.'libwebp-1.2.4-mac-arm64'.$DS.'bin'.$DS.'cwebp',
],
];
$server_operating_system = explode(' ', trim(php_uname()))[0];
$server_processor_architecture = trim(php_uname('m'));
if ($server_operating_system === 'Darwin') {
return resource_path($cwebp_compilers['Darwin'][$server_processor_architecture]);
}
if (in_array($server_operating_system, array_keys($cwebp_compilers))) {
return resource_path($cwebp_compilers[$server_operating_system]);
}
}
private function optimizeImageToWebP($originalFilePath, $directory, $lelongbidmarhun_id)
{
$convertedFilePath = config('filesystems.disks.marhun.root').'/'."{$directory}{$lelongbidmarhun_id}.webp";
$webpCompiler = $this->getWebpCompilerByOS();
exec($webpCompiler.' -preset photo '.' -resize 1280 720 '.' -mt '.$originalFilePath.' -o '.$convertedFilePath);
}
}