Files
lelong/app/Services/MarhunPhotoService.php
T
2022-12-01 09:49:50 +08:00

115 lines
3.7 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;
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 = $request->file('gambarMarhun')->getClientOriginalExtension();
$marhunImageFileName = "overall.{$imageExtension}";
if (!empty($request->lelongbidmarhun_id)) {
$marhunImageFileName = "$request->lelongbidmarhun_id.$imageExtension";
}
Storage::disk('marhun')->putFileAs($directory, $request->gambarMarhun, $marhunImageFileName);
}
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}");
}
}