From 991891166168d7d1f3ddcc9226c927baf01b6bc1 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 2 May 2024 14:59:15 +0700 Subject: [PATCH 1/2] Introduce example exporter class & resolve media image path method --- src/Exporter/ExampleExporter.php | 28 +++++++++++++ src/Importer/Importer.php | 69 ++++++++++++++++---------------- 2 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 src/Exporter/ExampleExporter.php diff --git a/src/Exporter/ExampleExporter.php b/src/Exporter/ExampleExporter.php new file mode 100644 index 0000000..1812154 --- /dev/null +++ b/src/Exporter/ExampleExporter.php @@ -0,0 +1,28 @@ + ExportColumn::make($item->getName())->label($item->getLabel()), $this->columns); + } + + public function getExportFileName(): string + { + return sprintf('%s-example', str($this->label)->trim()->replace(' ', '-')); + } + + public function collection(): Collection + { + return collect($this->examples)->map(fn ($item) => (object) $item); + } +} diff --git a/src/Importer/Importer.php b/src/Importer/Importer.php index bca889c..23056d9 100644 --- a/src/Importer/Importer.php +++ b/src/Importer/Importer.php @@ -6,19 +6,23 @@ use Botble\DataSynchronize\Contracts\Importer\WithMapping; use Botble\DataSynchronize\DataTransferObjects\ChunkImportResponse; use Botble\DataSynchronize\DataTransferObjects\ChunkValidateResponse; -use Botble\DataSynchronize\Exporter\ExportColumn; -use Botble\DataSynchronize\Exporter\Exporter; +use Botble\DataSynchronize\Exporter\ExampleExporter; +use Botble\Media\Facades\RvMedia; use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Contracts\View\View; -use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Validator; use Illuminate\Support\LazyCollection; +use Illuminate\Support\Str; use Spatie\SimpleExcel\SimpleExcelReader; +use Symfony\Component\HttpFoundation\BinaryFileResponse; abstract class Importer { + protected bool $isValidating = false; + abstract public function columns(): array; abstract public function getValidateUrl(): string; @@ -112,6 +116,8 @@ public function render(): View public function validate(string $fileName, int $offset = 0, int $limit = 100): ChunkValidateResponse { + $this->isValidating = true; + $rows = $this->transformRows($this->getRowsByOffset($fileName, $offset, $limit)); $total = request()->integer('total') ?: $this->getRows($fileName)->count(); @@ -176,7 +182,7 @@ public function getDoneMessage(int $count): string { return trans('packages/data-synchronize::data-synchronize.import.done_message', [ 'count' => number_format($count), - 'label' => $this->getLabel(), + 'label' => strtolower($this->getLabel()), ]); } @@ -214,13 +220,11 @@ public function transformRows(array $rows): array ->mapWithKeys(function (ImportColumn $column) use ($row) { $value = $row[$column->getName()] ?? null; - if ($column->isNullable() && empty($value)) { - return [$column->getName() => null]; - } - - if ($column->isBoolean() && is_string($value)) { - $value = $value === $column->getTrueValue() ? 1 : 0; - } + $value = match (true) { + $column->isNullable() && empty($value) => null, + $column->isBoolean() && is_string($value) => $value === $column->getTrueValue() ? 1 : 0, + default => $value, + }; return [$column->getName() => $value]; }) @@ -244,36 +248,31 @@ public function filesystem(): Filesystem return Storage::disk(config('packages.data-synchronize.data-synchronize.storage.disk')); } - public function downloadExample(string $format) + public function downloadExample(string $format): BinaryFileResponse { - $examples = $this->getExamples(); $columns = $this->getColumns(); - $label = $this->getLabel(); - - $exporter = new class ($examples, $columns, $label) extends Exporter { - public function __construct(protected array $examples, protected array $columns, protected string $label) - { - } - - public function columns(): array - { - return array_map(fn (ImportColumn $item) => ExportColumn::make($item->getName())->label($item->getLabel()), $this->columns); - } - - public function getExportFileName(): string - { - return sprintf('%s-example', str($this->label)->trim()->replace(' ', '-')); - } - - public function collection(): Collection - { - return collect($this->examples)->map(fn ($item) => (object) $item); - } - }; + $exporter = (new ExampleExporter($this->getExamples(), $columns, $this->getLabel())); return $exporter ->format($format) ->acceptedColumns(array_map(fn (ImportColumn $column) => $column->getName(), $columns)) ->export(); } + + protected function resolveMediaImage(string $url, ?string $directory = null): string + { + if (! Str::startsWith($url, ['http://', 'https://'])) { + return $url; + } + + $result = RvMedia::uploadFromUrl($url, 0, $directory); + + if ($result['error']) { + Log::error($result['message']); + + return $url; + } + + return $result['data']->url; + } } From 49fd3489710b9ccd4406b47fc4198fa10715a7db Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 2 May 2024 15:00:00 +0700 Subject: [PATCH 2/2] Introduce example exporter class & resolve media image path method --- src/Importer/Importer.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Importer/Importer.php b/src/Importer/Importer.php index 23056d9..28a018e 100644 --- a/src/Importer/Importer.php +++ b/src/Importer/Importer.php @@ -21,8 +21,6 @@ abstract class Importer { - protected bool $isValidating = false; - abstract public function columns(): array; abstract public function getValidateUrl(): string; @@ -116,8 +114,6 @@ public function render(): View public function validate(string $fileName, int $offset = 0, int $limit = 100): ChunkValidateResponse { - $this->isValidating = true; - $rows = $this->transformRows($this->getRowsByOffset($fileName, $offset, $limit)); $total = request()->integer('total') ?: $this->getRows($fileName)->count();