87 lines
2.7 KiB
PHP
87 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use App\Models\FileMarhun;
|
|
|
|
class MarhunPhotoService
|
|
{
|
|
|
|
public function store(Request $request)
|
|
{
|
|
|
|
$file_type = $request->gambarMarhun->extension();
|
|
|
|
$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();
|
|
}
|
|
|
|
$directory = "/{$request->lelongbid_id}/{$request->lelong_id}/{$request->photo_type}/";
|
|
$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();
|
|
|
|
$lelongbid_id = $deleteFile->lelongbid_id;
|
|
$lelongbidmarhun_id = $deleteFile->lelongbidmarhun_id;
|
|
$lelong_id = $request->lelong_id;
|
|
$photo_type = $deleteFile->photo_type;
|
|
|
|
$directory = "/{$lelongbid_id}/{$lelong_id}/{$photo_type}/";
|
|
$imageExtension = $deleteFile->file_type;
|
|
|
|
$marhunImageFileName = "overall.{$imageExtension}";
|
|
|
|
if (!empty($request->lelongbidmarhund_id)) {
|
|
$marhunImageFileName = "$lelongbidmarhun_id.$imageExtension";
|
|
}
|
|
|
|
Storage::disk('marhun')->delete("{$directory}${marhunImageFileName}");
|
|
$deleteFile->delete();
|
|
}
|
|
|
|
public function show($lelong_id, $marhun)
|
|
{
|
|
$lelongbid_id = $marhun->lelongbid_id;
|
|
$lelongbidmarhun_id = $marhun->lelongbidmarhun_id;
|
|
$lelong_id = $lelong_id;
|
|
$photo_type = $marhun->photo_type;
|
|
|
|
$directory = "{$lelongbid_id}/{$lelong_id}/{$photo_type}/";
|
|
$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:' . $imageExtension);
|
|
header('Content-Length: ' . filesize($filePath));
|
|
readfile($filePath);
|
|
}
|
|
}
|