Browse Source

fix: upload report image

pull/8/head
wahyun 4 months ago
parent
commit
f86800f3d1
  1. 69
      app/Http/Controllers/ImageController.php

69
app/Http/Controllers/ImageController.php

@ -25,62 +25,21 @@ class ImageController extends Controller
$extension = $dokumen->extension(); $extension = $dokumen->extension();
$category = $request->category; $category = $request->category;
$name = $ref_id.date("YmdHis").".".$extension; $name = $ref_id.date("YmdHis").".".$extension;
// Limited Storage $destinationPath = $this->setCustomeDirectoryUpload();
$company = Company::where('company_name', $request->company_name)->first(); $data = [
if($company) { 'ref_id' => (int)$ref_id,
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); 'image' => $name,
$getLimitStorage = $this->setLimitsStorage($company, $dokumen, $destinationPath['pathImage'], $destinationPath); 'category' => $category
} ];
if(isset($getLimitStorage)) {
if($getLimitStorage === false) { $result = Image::create($data);
DB::rollBack(); if($dokumen->move($destinationPath['pathImage'], $name) && $result) {
return response()->json(['status' => 'failed', 'message' => 'Limited storage maximum!', 'code' => 500], 500); DB::commit();
} return response()->json(['status' => 'success', "name_image" => $name,'message' => 'image upload is successfully!','code' => 200], 200);
$data = [
'ref_id' => (int)$ref_id,
'image' => $name,
'category' => $category
];
$result = Image::create($data);
$company = Company::find((int)$ref_id);
if($company && $category === 'company_logo_login') {
$dataArray = json_decode($company['logo_login'], true);
if(isset($dataArray['slider'])) {
$jsonImageSlider['login'] = $name;
$jsonImageSlider['slider'] = $dataArray['slider'];
} else {
$jsonImageSlider['login'] = $name;
}
$company->update([
'logo_login' => $jsonImageSlider
]);
}
if($company && $category === 'company_logo_header') {
$company->update([
'logo_header' => array('content' => $name)
]);
}
if($company && $category === 'company_favicon') {
$company->update([
'favicon_image' => array('content' => $name)
]);
}
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);
}
} else { } else {
DB::rollBack(); DB::rollBack();
return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); unlink($destinationPath['pathImage'].$name);
return response()->json(['status' => 'failed','message' => 'image upload is failed!' ,'code' => 400], 400);
} }
} else { } else {
DB::rollBack(); DB::rollBack();
@ -123,7 +82,7 @@ class ImageController extends Controller
$totalSize += filesize($folderPath . '/' . $file); $totalSize += filesize($folderPath . '/' . $file);
} }
} }
$totalSize += $sizeFile; $totalSize += $sizeFile;
} }
foreach ($dokumen as $file) { foreach ($dokumen as $file) {
$extension = $file->extension(); $extension = $file->extension();

Loading…
Cancel
Save