Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add s3 support to import and export #347

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/Exportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Importable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 18 additions & 1 deletion src/Providers/FastExcelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@

class FastExcelServiceProvider extends ServiceProvider
{
public const CONFIG_FILENAME = 'fast-excel';

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
$this->publishes([
$this->getConfigFile() => config_path(self::CONFIG_FILENAME.'.php'),
], 'config');
}

/**
Expand All @@ -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);
Expand All @@ -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';
}
}
19 changes: 19 additions & 0 deletions src/config/fast-excel.php
Original file line number Diff line number Diff line change
@@ -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,
];
16 changes: 16 additions & 0 deletions src/functions/fastexcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}