diff --git a/app/Http/Controllers/ImageController.php b/app/Http/Controllers/ImageController.php index aa8adbe..af18b45 100644 --- a/app/Http/Controllers/ImageController.php +++ b/app/Http/Controllers/ImageController.php @@ -6,11 +6,13 @@ use App\Models\Image; use App\Models\Company; use Illuminate\Http\Request; use App\Models\ProductTransaction; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; class ImageController extends Controller { public function uploadImage(Request $request){ - + DB::beginTransaction(); if($request->hasFile('files')){ $this->validate($request, [ @@ -27,9 +29,13 @@ class ImageController extends Controller $company = Company::where('company_name', $request->company_name)->first(); if($company) { $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); - $getLimitStorage = $this->setLimitsStorage($company, $dokumen, $name, $destinationPath['pathImage'], $destinationPath); + $getLimitStorage = $this->setLimitsStorage($company, $dokumen, $destinationPath['pathImage'], $destinationPath); } - if(isset($getLimitStorage) && $getLimitStorage['resultMove']) { + if(isset($getLimitStorage)) { + if($getLimitStorage === false) { + DB::rollBack(); + return response()->json(['status' => 'failed', 'message' => 'Limited storage maximum!', 'code' => 500], 500); + } $data = [ 'ref_id' => (int)$ref_id, 'image' => $name, @@ -54,27 +60,30 @@ class ImageController extends Controller if($company && $category === 'company_logo_header') { $company->update([ - 'logo_header' => array('content'=>$name) + 'logo_header' => array('content' => $name) ]); } if($company && $category === 'company_favicon') { $company->update([ - 'favicon_image' => array('content'=>$name) + 'favicon_image' => array('content' => $name) ]); } - - if($result){ - return response()->json(['status'=>'success', "name_image"=>$name,'message'=>'image upload is successfully!','code'=>200], 200); - }else{ + if($dokumen->move($destinationPath['pathImage'], $name) && $result) { + DB::commit(); + return response()->json(['status' => 'success', "name_image" => $name,'message' => 'image upload is successfully!','code' => 200], 200); + } else { + DB::rollBack(); unlink($destinationPath['pathImage'].$name); - return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); + return response()->json(['status' => 'failed','message' => 'image upload is failed! pertama' ,'code' => 400], 400); } - }else{ - return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); + } else { + DB::rollBack(); + return response()->json(['status'=>'failed','message'=>'image upload is failed! kedua','code'=>400], 400); } - }else{ + } else { + DB::rollBack(); return response()->json(['status'=>'failed','message'=>'image is required!','code'=>400], 400); } } @@ -210,7 +219,7 @@ class ImageController extends Controller public function deleteByRef($id, $category, $company_id) { - $data = Image::where("ref_id", intval($id))->where("category", $category)->first(); + $data = Image::where("ref_id", (int)$id)->where("category", $category)->first(); $company = Company::find($company_id); if($data && $company){ $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);