From 823bd348d2c03c514e0b1b8ecfbbffed739fd9ad Mon Sep 17 00:00:00 2001 From: panabonic Date: Tue, 22 Aug 2023 16:06:46 +0300 Subject: [PATCH] few updates --- app/Services/Registries/RegistryImportService.php | 7 +++++-- app/Services/Registries/RulesetImportService.php | 10 +++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/Services/Registries/RegistryImportService.php b/app/Services/Registries/RegistryImportService.php index 2b8607f..46c25b0 100644 --- a/app/Services/Registries/RegistryImportService.php +++ b/app/Services/Registries/RegistryImportService.php @@ -29,11 +29,14 @@ class RegistryImportService { } - public function download($url, $dir = null): ?Asset { + public function download($url, $dir = null, $filename = null): ?Asset { + $urlInfo = parse_url($url); + if (empty($urlInfo['host'])) $url = str_replace('//', '/', "https://faufcc.ru/{$url}"); $info = pathinfo($url); if ($info['extension']) { $path = "public/documents/registries"; - $path = $dir ? "{$path}/{$dir}/{$info['basename']}" : "{$path}/{$info['basename']}"; + $filename = $filename ? "{$filename}.{$info['extension']}" : $info['basename']; + $path = $dir ? "{$path}/{$dir}/{$filename}" : "{$path}/{$filename}"; $asset = Asset::query()->where(['path' => $path])->first(); if (!$asset && Storage::put($path, Http::get($url)->body())) $asset = $this->makeAsset($path); elseif ($asset) var_dump($asset->path); diff --git a/app/Services/Registries/RulesetImportService.php b/app/Services/Registries/RulesetImportService.php index 7b915f6..3dbe73e 100644 --- a/app/Services/Registries/RulesetImportService.php +++ b/app/Services/Registries/RulesetImportService.php @@ -48,7 +48,7 @@ class RulesetImportService extends RegistryImportService { if (!$this->registry->entries()->where(['name' => $entryName])->exists()) { echo ("Importing {$entryName}\n"); $link = $item->find('a', 0); - $this->importItem("{$this->url}{$link->href}", $subcategory); + $this->importItem("{$this->url}{$link->href}", $subcategory, $entryName); } else echo("Already imported {$entryName}\n"); //if ($k >= 5) return; } @@ -56,13 +56,12 @@ class RulesetImportService extends RegistryImportService { } } - public function importItem(string $url, Category $category) { + public function importItem(string $url, Category $category, $name) { $dom = new Dom; $dom->loadFromUrl($url); $link = $dom->find('p.ACTUALNAME a', 0); - list($number, $name) = explode('«', $link->text); + list($number) = explode('«', $link->text); $number = trim($number); - $name = trim($name, '» '); $entry = $category->entries()->firstOrCreate(['registry_id' => $category->registry_id, 'number' => $number]); $entry->update(['name' => $name]); $this->importOperations($entry, array_slice($dom->find('table.inc1 tr')->toArray(), 1)); @@ -100,7 +99,8 @@ class RulesetImportService extends RegistryImportService { list($orderName, $orderDate) = explode(' от ', $link->text); $data['order-name'] = $orderName; $data['order-date'] = $orderDate ? Date::create($orderDate) : null; - $data['order-document'] = $this->download($link->href, 'ruleset'); + $filename = $orderName ? Str::slug("Приказ {$orderName} от {$orderDate}") : null; + $data['order-document'] = $this->download($link->href, 'ruleset', $filename); } else $data['listings'][] = ['name' => Str::replace('Постановление правительства №', 'pp', $link->text)]; } } else {