From a72cf83242e2160a511233e20f7dc17911e00dc4 Mon Sep 17 00:00:00 2001 From: Denis Colli Spalenza Date: Fri, 26 Jan 2024 11:48:32 -0300 Subject: [PATCH] feat: add s3 support to import and export --- composer.json | 3 ++- src/Exportable.php | 6 ++++++ src/Importable.php | 8 ++++++++ src/Providers/FastExcelServiceProvider.php | 19 ++++++++++++++++++- src/config/fast-excel.php | 19 +++++++++++++++++++ src/functions/fastexcel.php | 16 ++++++++++++++++ 6 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/config/fast-excel.php diff --git a/composer.json b/composer.json index 8600612..751e6d9 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "require": { "php": "^8.0", "illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0|^10.0", - "openspout/openspout": "^4.1.1" + "openspout/openspout": "^4.1.1", + "spatie/temporary-directory": "^2.2" }, "require-dev": { "illuminate/database": "^6.20.12 || ^7.30.4 || ^8.24.0 || ^9.0|^10.0", diff --git a/src/Exportable.php b/src/Exportable.php index 8b063ec..463fad3 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -4,6 +4,7 @@ use Generator; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use InvalidArgumentException; use OpenSpout\Common\Entity\Row; @@ -125,6 +126,11 @@ private function exportOrDownload($path, $function, callable $callback = null) } } $writer->close(); + + if (config('fast-excel.s3_disk_support')) { + $content = Storage::disk()->get($path); + Storage::disk('s3')->put($path, $content); + } } /** diff --git a/src/Importable.php b/src/Importable.php index ae5612d..0e0ed63 100644 --- a/src/Importable.php +++ b/src/Importable.php @@ -3,6 +3,7 @@ namespace Rap2hpoutre\FastExcel; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use OpenSpout\Common\Entity\Cell; use OpenSpout\Reader\SheetInterface; @@ -91,6 +92,13 @@ public function importSheets($path, callable $callback = null) */ private function reader($path) { + if (config('fast-excel.s3_disk_support')) { + $content = Storage::disk('s3')->get($path); + $tempDir = obtainTempDirectory(); + $path = $tempDir->path(basename($path)); + Storage::disk('local')->put($path, $content); + } + if (Str::endsWith($path, 'csv')) { $options = new \OpenSpout\Reader\CSV\Options(); $this->setOptions($options); diff --git a/src/Providers/FastExcelServiceProvider.php b/src/Providers/FastExcelServiceProvider.php index 3a89308..c736cb3 100644 --- a/src/Providers/FastExcelServiceProvider.php +++ b/src/Providers/FastExcelServiceProvider.php @@ -6,6 +6,8 @@ class FastExcelServiceProvider extends ServiceProvider { + public const CONFIG_FILENAME = 'fast-excel'; + /** * Bootstrap any application services. * @@ -13,7 +15,9 @@ class FastExcelServiceProvider extends ServiceProvider */ public function boot() { - // + $this->publishes([ + $this->getConfigFile() => config_path(self::CONFIG_FILENAME . '.php'), + ], 'config'); } /** @@ -25,6 +29,11 @@ public function boot() */ public function register() { + $this->mergeConfigFrom( + $this->getConfigFile(), + self::CONFIG_FILENAME + ); + $this->app->bind('fastexcel', function ($app, $data = null) { if (is_array($data)) { $data = collect($data); @@ -33,4 +42,12 @@ public function register() return new \Rap2hpoutre\FastExcel\FastExcel($data); }); } + + /** + * @return string + */ + protected function getConfigFile(): string + { + return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . self::CONFIG_FILENAME . '.php'; + } } diff --git a/src/config/fast-excel.php b/src/config/fast-excel.php new file mode 100644 index 0000000..9acddb1 --- /dev/null +++ b/src/config/fast-excel.php @@ -0,0 +1,19 @@ +return [ + /* + |-------------------------------------------------------------------------- + | S3 Support + |-------------------------------------------------------------------------- + | + | To use S3 disk support, set this to true. + | You also need to setup S3 on your Laravel application + | To do this you can use follow environment variables: + | - AWS_ACCESS_KEY_ID + | - AWS_SECRET_ACCESS_KEY + | - AWS_DEFAULT_REGION + | - AWS_BUCKET + | - AWS_ENDPOINT + | + | Read more in https://laravel.com/docs/10.x/filesystem#s3-driver-configuration + */ + 's3_disk_support' => false, +]; diff --git a/src/functions/fastexcel.php b/src/functions/fastexcel.php index 37ba638..33b0d48 100644 --- a/src/functions/fastexcel.php +++ b/src/functions/fastexcel.php @@ -21,3 +21,19 @@ function fastexcel($data = null) return $data === null ? app()->make('fastexcel') : app()->makeWith('fastexcel', $data); } } + +if(!function_exists('obtainTempDirectory')) { + /** + * Get a temporary directory. + * + * @return TemporaryDirectory + */ + function obtainTempDirectory(): TemporaryDirectory + { + return (new TemporaryDirectory()) + ->name('fast-excel-'.microtime()) + ->force() + ->deleteWhenDestroyed() + ->create(); + } +}