diff --git a/src/Http/Controllers/UploadController.php b/src/Http/Controllers/UploadController.php index 1316aa7..ea63792 100644 --- a/src/Http/Controllers/UploadController.php +++ b/src/Http/Controllers/UploadController.php @@ -3,7 +3,6 @@ namespace Ahmedkandel\NovaS3MultipartUpload\Http\Controllers; use Ahmedkandel\NovaS3MultipartUpload\NovaS3MultipartUpload; -use Aws\S3\S3Client; use Illuminate\Support\Str; use Laravel\Nova\Http\Requests\NovaRequest; @@ -19,7 +18,7 @@ class UploadController /** * S3 client instance. * - * @var \Aws\S3\S3Client + * @var \Aws\S3\S3ClientInterface */ private $s3Client; @@ -36,10 +35,10 @@ public function preflightHeader() /** * Retrieve resource tool and Authorize then create S3Client.. * - * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @param \Laravel\Nova\Http\Requests\NovaRequest $request * @return void */ - private function init($request) + protected function init($request) { $resource = $request->findResourceOrFail(); @@ -53,16 +52,7 @@ private function init($request) abort_unless($this->tool->canUpload, 403); - $this->s3Client = new S3Client([ - 'credentials' => [ - 'key' => config("filesystems.disks.{$this->tool->disk}.key"), - 'secret' => config("filesystems.disks.{$this->tool->disk}.secret"), - ], - 'endpoint' => config("filesystems.disks.{$this->tool->disk}.endpoint"), - 'use_path_style_endpoint' => config("filesystems.disks.{$this->tool->disk}.use_path_style_endpoint"), - 'region' => config("filesystems.disks.{$this->tool->disk}.region"), - 'version' => 'latest', - ]); + $this->s3Client = app()->makeWith('novas3client', ['disk' => $this->tool->disk]); } /** diff --git a/src/ToolServiceProvider.php b/src/ToolServiceProvider.php index 4303b29..3615010 100644 --- a/src/ToolServiceProvider.php +++ b/src/ToolServiceProvider.php @@ -2,12 +2,27 @@ namespace Ahmedkandel\NovaS3MultipartUpload; +use Aws\S3\S3Client; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; use Laravel\Nova\Nova; class ToolServiceProvider extends ServiceProvider { + public function register() + { + parent::register(); + + // Construct s3 client for tool + $this->app->bind('novas3client', function ($app, $args) { + $disk = $args['disk']; + $config = $this->formatS3Config(config("filesystems.disks.{$disk}")); + return new S3Client($config); + }); + } + + /** * Bootstrap any application services. * @@ -25,6 +40,23 @@ public function boot() }); } + /** + * Format the given S3 configuration with the default options. + * + * @param array $config + * @return array + */ + protected function formatS3Config(array $config) + { + $config += ['version' => 'latest']; + + if (!empty($config['key']) && !empty($config['secret'])) { + $config['credentials'] = Arr::only($config, ['key', 'secret', 'token']); + } + + return $config; + } + /** * Register the tool's routes. *