Files
lelong/app/Services/MarhunPhotoService.php
T
2022-12-04 14:53:11 +08:00

168 lines
5.5 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Models\FileMarhun;
use App\Models\LelongBid;
use Exception;
use Buglinjo\LaravelWebp\Facades\Webp;
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}");
$convertedFilePath = $this->optimizeImageToWebP($stored_path, $directory, $marhunImageFileName);
$this->changeFileOwnerToServerUser($convertedFilePath);
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()
{
$cwebp_compilers = [
'Windows' => 'binaries\libwebp-1.2.4-windows-x64\bin\cwebp',
'Linux' => 'binaries\libwebp-1.2.4-linux-x86-64\bin\cwebp',
];
$cwebp_compilers_darwin = [
'X86-64' => 'binaries\libwebp-1.2.4-mac-X86-64\bin\cwebp',
'arm64' => 'binaries\libwebp-1.2.4-mac-arm64\bin\cwebp',
];
$server_operating_system = explode(' ', trim(php_uname()))[0];
$server_processor_architecture = trim(php_uname('m'));
if (in_array($server_operating_system, array_keys($cwebp_compilers))) {
return resource_path($cwebp_compilers[$server_operating_system]);
}
if ($server_operating_system === 'Darwin') {
return resource_path($cwebp_compilers_darwin[$server_processor_architecture]);
}
}
private function optimizeImageToWebP($originalFilePath, $directory, $lelongbidmarhun_id)
{
$convertedFilePath = config('filesystems.disks.marhun.root') . '/' . "{$directory}{$lelongbidmarhun_id}.webp";
$webpCompiler = $this->getWebpCompilerByOS();
exec($webpCompiler . ' -resize 1280 720 '.' -mt ' .$originalFilePath . ' -o ' . $convertedFilePath);
return $convertedFilePath;
}
private function changeFileOwnerToServerUser($filepath)
{
chown($filepath, get_current_user());
}
}