Fix upload image endpoint

This commit is contained in:
Djery-Tom 2021-10-25 14:58:26 +01:00
parent 95b5cabc00
commit c421290635
3 changed files with 3924 additions and 1445 deletions

View File

@ -42,6 +42,8 @@ trait ConsumesExternalService
$headers['Authorization'] = $this->key; $headers['Authorization'] = $this->key;
} }
$headers['Content-Type'] = 'multipart/form-data';
$client = new Client([ $client = new Client([
'base_uri' => $this->baseUri, 'base_uri' => $this->baseUri,
'headers' => $headers 'headers' => $headers
@ -57,22 +59,33 @@ trait ConsumesExternalService
{ {
$multipart = []; $multipart = [];
foreach ($body as $key => $value) { foreach ($body as $key => $value) {
if(is_array($value)){
foreach ($value as $v){
array_push($multipart, $this->convertToMultiFormArray($key.'[]',$v));
}
}else{
array_push($multipart, $this->convertToMultiFormArray($key,$value));
}
}
return $multipart;
}
private function convertToMultiFormArray($key , $value): array
{
if ($value instanceof UploadedFile) { if ($value instanceof UploadedFile) {
array_push($multipart, [ return [
'name' => $key, 'name' => $key,
'contents' => file_get_contents($value->getRealPath()), 'contents' => file_get_contents($value->getRealPath()),
'mine-type' => $value->getMimeType(), 'mine-type' => $value->getMimeType(),
'filename' => $value->getClientOriginalName(), 'filename' => $value->getClientOriginalName(),
// 'headers' => [ 'Content-type' => 'application/octet-stream'] // 'headers' => [ 'Content-type' => 'application/octet-stream']
// 'Content-type' => 'multipart/form-data', // 'Content-type' => 'multipart/form-data',
]); ];
} else { } else {
array_push($multipart, [ return[
'name' => $key, 'name' => $key,
'contents' => $value, 'contents' => $value,
]); ];
} }
} }
return $multipart;
}
} }

View File

@ -5,15 +5,16 @@
"license": "MIT", "license": "MIT",
"type": "project", "type": "project",
"require": { "require": {
"php": "^7.2.5", "php": "^7.3|^8.0",
"dusterio/lumen-passport": "^0.2.18", "dusterio/lumen-passport": "^0.3.0",
"guzzlehttp/guzzle": "^6.5", "guzzlehttp/guzzle": "^7.3",
"laravel/lumen-framework": "^7.0" "laravel/lumen-framework": "^8.0",
"ext-json": "*"
}, },
"require-dev": { "require-dev": {
"fzaninotto/faker": "^1.9.1", "fzaninotto/faker": "^1.9.1",
"mockery/mockery": "^1.3.1", "mockery/mockery": "^1.3.1",
"phpunit/phpunit": "^8.5" "phpunit/phpunit": "^9.5"
}, },
"autoload": { "autoload": {
"classmap": [ "classmap": [
@ -21,7 +22,9 @@
"database/factories" "database/factories"
], ],
"psr-4": { "psr-4": {
"App\\": "app/" "App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
} }
}, },
"autoload-dev": { "autoload-dev": {

5309
composer.lock generated Executable file → Normal file

File diff suppressed because it is too large Load Diff