also looking at this
fix: Use storage upload
#81
1 file
+2
-62
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -2,11 +2,6 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Meema\MeemaClient\Models; |
| 4 | 4 | |
| 5 | use Exception; | |
| 6 | use GuzzleHttp\Client as GuzzleClient; | |
| 7 | use GuzzleHttp\Psr7; | |
| 8 | use GuzzleHttp\Psr7\Request; | |
| 9 | use GuzzleHttp\Psr7\Utils; | |
| 10 | 5 | use Meema\MeemaClient\Client; |
| 11 | 6 | |
| 12 | 7 | class Storage |
| @@ -33,64 +28,9 @@ class Storage | ||
| 33 | 28 | * |
| 34 | 29 | * @return array |
| 35 | 30 | */ |
| 36 | public function upload($path) | |
| 31 | public function upload($path, $contents = null, $config = []) | |
| 37 | 32 | { |
| 38 | try { | |
| 39 | $file = fopen($path, 'r'); | |
| 40 | $fileName = basename($path); | |
| 41 | ||
| 42 | $stream = Utils::streamFor($file); | |
| 43 | $mimeType = mime_content_type($file); | |
| 44 | ||
| 45 | $signedUrl = $this->client->request('POST', 'storage/signed-url', ['content_type' => $mimeType]); | |
| 46 | ||
| 47 | if (is_array($signedUrl) && $signedUrl['url']) { | |
| 48 | $headers = $signedUrl['headers']; | |
| 49 | unset($headers['Host']); | |
| 50 | ||
| 51 | $this->uploadFile($signedUrl['url'], $headers, $fileName, $stream); | |
| 52 | ||
| 53 | $uploadData = ['key' => $signedUrl['key'], 'file_name' => $fileName]; | |
| 54 | ||
| 55 | $response = $this->client->request('POST', 'upload', $uploadData); | |
| 56 | ||
| 57 | return $response; | |
| 58 | } | |
| 59 | } catch (Exception $e) { | |
| 60 | throw $e; | |
| 61 | } | |
| 62 | ||
| 63 | return ['message' => 'File did not successfully upload.']; | |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Upload the file stream to s3. | |
| 68 | * | |
| 69 | * @param string $signedUrl | |
| 70 | * @param array $headers | |
| 71 | * @param string $fileName | |
| 72 | * @param GuzzleHttp\Psr7 $stream | |
| 73 | * | |
| 74 | * @return void | |
| 75 | */ | |
| 76 | protected function uploadFile($signedUrl, $headers, $fileName, $stream) | |
| 77 | { | |
| 78 | $client = new GuzzleClient(); | |
| 79 | $request = new Request( | |
| 80 | 'PUT', | |
| 81 | $signedUrl, | |
| 82 | ['headers' => json_encode($headers)], | |
| 83 | new Psr7\MultipartStream( | |
| 84 | [ | |
| 85 | [ | |
| 86 | 'name' => $fileName, | |
| 87 | 'contents' => $stream, | |
| 88 | ], | |
| 89 | ] | |
| 90 | ) | |
| 91 | ); | |
| 92 | ||
| 93 | $client->send($request); | |
| 33 | return $this->client->request('POST', 'storage/upload', compact('path', 'contents', 'config')); | |
| 94 | 34 | } |
| 95 | 35 | |
| 96 | 36 | /** |