From b921c059ebe8395076b3b57a604634a3204b3ad3 Mon Sep 17 00:00:00 2001 From: Djery-Tom Date: Sat, 20 Jun 2020 16:09:37 +0100 Subject: [PATCH] + Fix bugs while upload files --- .../Controllers/WalletServiceController.php | 8 ++- app/Services/WalletService.php | 5 ++ app/Traits/ConsumesExternalService.php | 60 ++++++++++++------- routes/web.php | 2 +- 4 files changed, 51 insertions(+), 24 deletions(-) diff --git a/app/Http/Controllers/WalletServiceController.php b/app/Http/Controllers/WalletServiceController.php index f2f7d0b..1e87102 100755 --- a/app/Http/Controllers/WalletServiceController.php +++ b/app/Http/Controllers/WalletServiceController.php @@ -6,7 +6,6 @@ namespace App\Http\Controllers; use App\Services\WalletService; use App\Traits\ApiResponser; use Illuminate\Http\Request; -use Illuminate\Http\Response; class WalletServiceController extends Controller { @@ -40,6 +39,13 @@ class WalletServiceController extends Controller )); } + public function postWithFiles(Request $request) + { + return $this->successResponse($this->walletService->postFiles( + substr($request->getRequestUri(),strlen(env('WALLET_SERVICE_NAME'))+1), $request->all(),$request->header() + )); + } + public function put(Request $request) { return $this->successResponse($this->walletService->put( diff --git a/app/Services/WalletService.php b/app/Services/WalletService.php index 603822c..c985b64 100755 --- a/app/Services/WalletService.php +++ b/app/Services/WalletService.php @@ -29,6 +29,11 @@ class WalletService return $this->perfomRequest('POST',$uri,$data,$headers); } + public function postFiles($uri , $data, $headers) + { + return $this->perfomRequestWithFiles('POST',$uri,$data,$headers); + } + public function get($uri , $data, $headers) { return $this->perfomRequest('GET',$uri,$data,$headers); diff --git a/app/Traits/ConsumesExternalService.php b/app/Traits/ConsumesExternalService.php index 67c847d..cbd26a9 100755 --- a/app/Traits/ConsumesExternalService.php +++ b/app/Traits/ConsumesExternalService.php @@ -1,7 +1,10 @@ $this->baseUri, ]); - if(isset($this->key)){ + if (isset($this->key)) { unset($headers['authorization']); $headers['Authorization'] = $this->key; - } - $contentType = $headers['content-type'][0]; - - if(strpos($contentType, 'multipart/form-data') !== false){ -// $headers['content-type'] = 'multipart/form-data'; -// dd($body); - $response = $client->request($method ,$requestUrl , ['multipart' => $this->bodyToMultipart($body),'headers' => $headers]); - }else{ - $response = $client->request($method , $requestUrl , ['json'=> $body ,'form_params' => $formParams , 'headers' => $headers]); } + $response = $client->request($method, $requestUrl, ['json' => $body, 'form_params' => $formParams, 'headers' => $headers]); + return $response->getBody()->getContents(); } - private function bodyToMultipart(array $body){ + public function perfomRequestWithFiles($method, $requestUrl, $body = [], $headers = [], $formParams = []) + { + + if (isset($this->key)) { + unset($headers['authorization']); + $headers['Authorization'] = $this->key; + } + + $client = new Client([ + 'base_uri' => $this->baseUri, + 'headers' => $headers + ]); + + $request = new Request($method, $requestUrl, [], new MultipartStream($this->bodyToMultipart($body))); + $response = $client->send($request); + + return $response->getBody()->getContents(); + } + + private function bodyToMultipart(array $body) + { $multipart = []; - foreach ($body as $key => $value){ - if( $value instanceof UploadedFile){ - array_push($multipart , [ - 'name' => $key, + foreach ($body as $key => $value) { + if ($value instanceof UploadedFile) { + array_push($multipart, [ + 'name' => $key, 'contents' => file_get_contents($value->getRealPath()), - 'mine-type'=>$value->getMimeType(), + 'mine-type' => $value->getMimeType(), 'filename' => $value->getClientOriginalName(), -// 'contents' => fopen( './test.txt', 'r' ), - 'headers' => [ 'Content-type' => 'multipart/form-data'] +// 'headers' => [ 'Content-type' => 'application/octet-stream'] +// 'Content-type' => 'multipart/form-data', ]); - }else{ - array_push($multipart , [ - 'name' => $key, + } else { + array_push($multipart, [ + 'name' => $key, 'contents' => $value, ]); } diff --git a/routes/web.php b/routes/web.php index a452635..c53d9eb 100755 --- a/routes/web.php +++ b/routes/web.php @@ -93,7 +93,7 @@ // Idendification routes $router->group(['prefix' => '/identifications'] , function () use ($router){ $router->post('','WalletServiceController@post'); - $router->post('{id_identification}','WalletServiceController@post'); + $router->post('{id_identification}','WalletServiceController@postWithFiles'); $router->get('{id_user}','WalletServiceController@get'); $router->get('verify/{user_phone}','WalletServiceController@get'); });