+ Fix bugs while upload files
This commit is contained in:
parent
9878189ee9
commit
b921c059eb
|
@ -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(
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\MultipartStream;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
|
||||
trait ConsumesExternalService
|
||||
|
@ -25,20 +28,33 @@ trait ConsumesExternalService
|
|||
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]);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -47,8 +63,8 @@ trait ConsumesExternalService
|
|||
'contents' => file_get_contents($value->getRealPath()),
|
||||
'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, [
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue