From 6b8704b1cfb684409e561c58194f6aecc172a034 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 28 Aug 2021 00:10:42 +1000 Subject: [PATCH] Added Echoarea NA file import --- app/Console/Commands/EchoareaImport.php | 40 ++++++++++ app/Jobs/EchoareaImport.php | 98 +++++++++++++++++++++++++ app/Jobs/NodelistImport.php | 2 +- 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 app/Console/Commands/EchoareaImport.php create mode 100644 app/Jobs/EchoareaImport.php diff --git a/app/Console/Commands/EchoareaImport.php b/app/Console/Commands/EchoareaImport.php new file mode 100644 index 0000000..663cc50 --- /dev/null +++ b/app/Console/Commands/EchoareaImport.php @@ -0,0 +1,40 @@ +argument('domain')))->singleOrFail(); + return Job::dispatchSync($this->argument('file'),$do,$this->option('prefix'),$this->option('unlink')); + } +} \ No newline at end of file diff --git a/app/Jobs/EchoareaImport.php b/app/Jobs/EchoareaImport.php new file mode 100644 index 0000000..78bf5a0 --- /dev/null +++ b/app/Jobs/EchoareaImport.php @@ -0,0 +1,98 @@ +file = $file; + $this->do = $do; + $this->prefix = $prefix; + $this->delete_file = $delete_file; + $this->delete_recs = $delete_recs; + } + + /** + * Execute the job. + * + * @return void + * @throws \Exception + */ + public function handle() + { + // Get the file from the host + $file = $this->getFileFromHost('',self::importkey,$this->file); + $lines = $this->getFileLines($file); + Log::debug(sprintf('%s:Processing [%d] lines.',static::LOGKEY,$lines)); + + $fh = fopen($file,'r'); + + $p = $c = 0; + while (! feof($fh)) { + $line = stream_get_line($fh, 0, "\n"); + + // Remove any embedded CR and BOM + $line = str_replace("\r",'',$line); + $line = preg_replace('/^\x{feff}/u','',$line); + + if (! $line) + continue; + + $c++; + + list($area,$description) = preg_split('/[\s|\t]+/',$line,2); + + if ($this->do->echoareas->pluck('name')->search($area) !== FALSE) { + Log::info(sprintf('%s: Area [%s] already exists.',self::LOGKEY,$area)); + continue; + } + + $o = new Echoarea; + $o->name = $area; + $o->description = ($this->prefix ?: '').$description; + $o->active = TRUE; + $o->public = TRUE; + + $this->do->echoareas()->save($o); + } + + fclose($fh); + + if ($this->delete_file and $c) + unlink($file); + + Log::info(sprintf('%s:Updated %d records',self::LOGKEY,$p)); + } +} diff --git a/app/Jobs/NodelistImport.php b/app/Jobs/NodelistImport.php index 106f01c..6d5a869 100644 --- a/app/Jobs/NodelistImport.php +++ b/app/Jobs/NodelistImport.php @@ -89,7 +89,7 @@ class NodelistImport implements ShouldQueue if ($this->delete_recs) $no->addresses()->detach(); - $p = $c =0; + $p = $c = 0; $region = NULL; $host = NULL;