Skip to content

Commit

Permalink
fix: Use storage upload (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
glennmichael123 committed May 6, 2021
1 parent 5c70915 commit 28e6401
Showing 1 changed file with 2 additions and 62 deletions.
64 changes: 2 additions & 62 deletions src/Models/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

namespace Meema\MeemaClient\Models;

use Exception;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;
use Meema\MeemaClient\Client;

class Storage
Expand All @@ -33,64 +28,9 @@ public function __construct(Client $client)
*
* @return array
*/
public function upload($path)
public function upload($path, $contents = null, $config = [])
{
try {
$file = fopen($path, 'r');
$fileName = basename($path);

$stream = Utils::streamFor($file);
$mimeType = mime_content_type($file);

$signedUrl = $this->client->request('POST', 'storage/signed-url', ['content_type' => $mimeType]);

if (is_array($signedUrl) && $signedUrl['url']) {
$headers = $signedUrl['headers'];
unset($headers['Host']);

$this->uploadFile($signedUrl['url'], $headers, $fileName, $stream);

$uploadData = ['key' => $signedUrl['key'], 'file_name' => $fileName];

$response = $this->client->request('POST', 'upload', $uploadData);

return $response;
}
} catch (Exception $e) {
throw $e;
}

return ['message' => 'File did not successfully upload.'];
}

/**
* Upload the file stream to s3.
*
* @param string $signedUrl
* @param array $headers
* @param string $fileName
* @param GuzzleHttp\Psr7 $stream
*
* @return void
*/
protected function uploadFile($signedUrl, $headers, $fileName, $stream)
{
$client = new GuzzleClient();
$request = new Request(
'PUT',
$signedUrl,
['headers' => json_encode($headers)],
new Psr7\MultipartStream(
[
[
'name' => $fileName,
'contents' => $stream,
],
]
)
);

$client->send($request);
return $this->client->request('POST', 'storage/upload', compact('path', 'contents', 'config'));
}

/**
Expand Down

0 comments on commit 28e6401

Please sign in to comment.