Browse Source

update uploadProjectDokumen

pull/1/head
wahyun 9 months ago
parent
commit
5dadd294bf
  1. 49
      app/Http/Controllers/ProjectDokumenController.php

49
app/Http/Controllers/ProjectDokumenController.php

@ -2,10 +2,10 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Company;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\DokumenProject; use App\Models\{DokumenProject,Company};
use App\Models\ProductTransaction; use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class ProjectDokumenController extends Controller class ProjectDokumenController extends Controller
{ {
@ -35,8 +35,8 @@ class ProjectDokumenController extends Controller
$company = Company::find($company_id); $company = Company::find($company_id);
if($company) { if($company) {
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
if(file_exists($destinationPath['pathDocument'].$document->file)){ if(file_exists($destinationPath['pathDocument'].$document['file'])){
unlink($destinationPath['pathDocument'].$document->file); unlink($destinationPath['pathDocument'].$document['file']);
} }
} }
$document->delete(); $document->delete();
@ -45,33 +45,47 @@ class ProjectDokumenController extends Controller
public function uploadProjectDokumen(Request $request) public function uploadProjectDokumen(Request $request)
{ {
DB::beginTransaction();
$timeNow = Carbon::now();
if($request->hasFile('dokumen')){ if($request->hasFile('dokumen')){
$document = $request->file('dokumen'); $document = $request->file('dokumen');
$ref_id = $request->ref_id; $ref_id = $request->ref_id;
$name = $document->getClientOriginalName();
// Limited Storage $originalFilename = $document->getClientOriginalName();
$company = Company::where('company_name', $request->company_name)->first(); $extension = pathinfo($originalFilename, PATHINFO_EXTENSION);
$filename = pathinfo($originalFilename, PATHINFO_FILENAME);
$name = $filename . '_' . $timeNow->format('d-m-y-His') . '.' . $extension;
// Limited Storage
$company = Company::whereId($request->company_id)->first();
if($company) { if($company) {
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$getLimitStorage = $this->setLimitsStorage($company, $document, $name, $destinationPath['pathDocument'],$destinationPath); $getLimitStorage = $this->setLimitsStorage($company, $document, $destinationPath['pathDocument'],$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 = [ $data = [
'ref_id' => (int)$ref_id, 'ref_id' => (int)$ref_id,
'file' => $name, 'file' => $name,
'type_dokumen' => isset($request->type_dokumen) ? $request->type_dokumen : 'project-document' 'type_dokumen' => isset($request->type_dokumen) ? $request->type_dokumen : 'project-document'
]; ];
$getLimitStorage['resultMove'] = DokumenProject::create($data); $result = DokumenProject::create($data);
if(!$getLimitStorage['resultMove']){ if(!$document->move($destinationPath['pathDocument'], $name) && $result) {
unlink($destinationPath['pathDocument'].$name); unlink($destinationPath['pathDocument'].$name);
DB::rollBack();
return response()->json(['status'=>'failed','message'=>'Dokumen project gagal diupload!','code'=> 500], 500); return response()->json(['status'=>'failed','message'=>'Dokumen project gagal diupload!','code'=> 500], 500);
} }
DB::commit();
return response()->json(['status'=>'success','message'=>'Dokumen project berhasil diupload!','code'=>200], 200); return response()->json(['status'=>'success','message'=>'Dokumen project berhasil diupload!','code'=>200], 200);
} }
DB::rollBack();
return response()->json(['status'=>'failed','message'=>'Dokumen project gagal diupload!','code'=> 500], 500); return response()->json(['status'=>'failed','message'=>'Dokumen project gagal diupload!','code'=> 500], 500);
} }
DB::rollBack();
return response()->json(['status'=>'failed','message'=>'File is required!','code'=>400], 400); return response()->json(['status'=>'failed','message'=>'File is required!','code'=>400], 400);
} }
@ -94,15 +108,18 @@ class ProjectDokumenController extends Controller
} }
$document = DokumenProject::find($id); $document = DokumenProject::find($id);
$company = Company::find($company_id); $company = Company::find($company_id);
if(!$document || !$company) { if(!$document || !$company) {
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
} }
if(!file_exists($this->pathDocument.$document->file)) {
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$pathToFile = $destinationPath['pathDocument'].$document['file'];
if(!file_exists($pathToFile)) {
return response()->json(['status'=>'failed','message'=>'Directory not found!','code'=> 404], 404);
} }
if($company) { if($company) {
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$pathToFile = $destinationPath['pathDocument'].$document->file;
$name = pathinfo($pathToFile, PATHINFO_FILENAME) . "." . pathinfo($pathToFile, PATHINFO_EXTENSION); $name = pathinfo($pathToFile, PATHINFO_FILENAME) . "." . pathinfo($pathToFile, PATHINFO_EXTENSION);
} }
$headers = [ $headers = [

Loading…
Cancel
Save