also looking at this
Add error handling
#62
1 file
+20
-12
| @@ -2,9 +2,11 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Meema\MeemaApi\Models; |
| 4 | 4 | |
| 5 | use Exception; | |
| 5 | 6 | use GuzzleHttp\Client as GuzzleClient; |
| 6 | 7 | use GuzzleHttp\Psr7; |
| 7 | 8 | use GuzzleHttp\Psr7\Request; |
| 9 | use GuzzleHttp\Psr7\Utils; | |
| 8 | 10 | use Meema\MeemaApi\Client; |
| 9 | 11 | |
| 10 | 12 | class Storage |
| @@ -33,26 +35,32 @@ class Storage | ||
| 33 | 35 | */ |
| 34 | 36 | public function upload($path) |
| 35 | 37 | { |
| 36 | $file = fopen($path, 'r'); | |
| 37 | $fileName = basename($path); | |
| 38 | try { | |
| 39 | $file = fopen($path, 'r'); | |
| 40 | $fileName = basename($path); | |
| 38 | 41 | |
| 39 | $stream = Psr7\stream_for($file); | |
| 40 | $mimeType = mime_content_type($file); | |
| 42 | $stream = Utils::streamFor($file); | |
| 43 | $mimeType = mime_content_type($file); | |
| 41 | 44 | |
| 42 | $signedUrl = $this->client->request('POST', 'storage/signed-url', ['content_type' => $mimeType]); | |
| 45 | $signedUrl = $this->client->request('POST', 'storage/signed-url', ['content_type' => $mimeType]); | |
| 43 | 46 | |
| 44 | if (is_array($signedUrl) && $signedUrl['url']) { | |
| 45 | $headers = $signedUrl['headers']; | |
| 46 | unset($headers['Host']); | |
| 47 | if (is_array($signedUrl) && $signedUrl['url']) { | |
| 48 | $headers = $signedUrl['headers']; | |
| 49 | unset($headers['Host']); | |
| 47 | 50 | |
| 48 | $this->uploadFile($signedUrl['url'], $headers, $fileName, $stream); | |
| 51 | $this->uploadFile($signedUrl['url'], $headers, $fileName, $stream); | |
| 49 | 52 | |
| 50 | $uploadData = ['key' => $signedUrl['key'], 'file_name' => $fileName]; | |
| 53 | $uploadData = ['key' => $signedUrl['key'], 'file_name' => $fileName]; | |
| 51 | 54 | |
| 52 | $response = $this->client->request('POST', 'upload', $uploadData); | |
| 55 | $response = $this->client->request('POST', 'upload', $uploadData); | |
| 56 | ||
| 57 | return $response; | |
| 58 | } | |
| 59 | } catch (Exception $e) { | |
| 60 | throw $e; | |
| 53 | 61 | } |
| 54 | 62 | |
| 55 | return $response; | |
| 63 | return ['message' => 'File did not successfully upload.']; | |
| 56 | 64 | } |
| 57 | 65 | |
| 58 | 66 | /** |