Alex Puiu
2 years ago
9 changed files with 147 additions and 45 deletions
-
12app/Http/Controllers/SearchAndDisplaceController.php
-
32app/SearchDisplace/Documents/DocumentFile.php
-
10app/SearchDisplace/Ingest/HandleReceivedDocument.php
-
4app/SearchDisplace/Ingest/SendDocument.php
-
98app/SearchDisplace/SearchAndDisplaceJSON.php
-
4app/SearchDisplace/Searchers/Duckling.php
-
15resources/js/components/ProcessFile/ProcessFile.ts
-
9resources/js/services/ApiService.ts
-
8routes/web.php
@ -0,0 +1,98 @@ |
|||
<?php |
|||
|
|||
namespace App\SearchDisplace; |
|||
|
|||
use Illuminate\Support\Facades\Storage; |
|||
|
|||
class SearchAndDisplaceJSON |
|||
{ |
|||
protected $file; |
|||
protected $searchers; |
|||
|
|||
public function __construct($file, $searchers) |
|||
{ |
|||
$this->file = $file . '/document.json'; |
|||
$this->searchers = $searchers; |
|||
|
|||
$this->storage = Storage::disk('local'); |
|||
} |
|||
|
|||
public function execute() |
|||
{ |
|||
if(! $this->storage->exists("contracts/$this->file")) { |
|||
return; |
|||
} |
|||
|
|||
try { |
|||
$content = $this->getContent(); |
|||
} catch (\Exception $exception) { |
|||
\Illuminate\Support\Facades\Log::info('EXCEPTION: ' . $exception->getMessage()); |
|||
|
|||
return; |
|||
} |
|||
|
|||
$sd = $this->applySD($content); |
|||
|
|||
return [ |
|||
'content' => $this->convertToHTML($sd['content']), |
|||
'indexes' => $sd['indexes'] |
|||
]; |
|||
} |
|||
|
|||
protected function getContent() |
|||
{ |
|||
return json_decode($this->storage->get("contracts/$this->file")); |
|||
} |
|||
|
|||
protected function applySD($elements) |
|||
{ |
|||
$indexes = []; |
|||
|
|||
foreach($elements as $element) { |
|||
if($element->tag === 'img') |
|||
continue; |
|||
|
|||
$search = new SearchAndDisplace( |
|||
stripslashes($element->content), |
|||
[ |
|||
'searchers' => $this->searchers, |
|||
], |
|||
false, |
|||
true |
|||
); |
|||
|
|||
$changed = $search->execute(); |
|||
|
|||
if($changed) { |
|||
$indexes = $changed['indexes']; |
|||
$element->content = $changed['content']; |
|||
} |
|||
} |
|||
|
|||
return [ |
|||
'content' => $elements, |
|||
'indexes' => $indexes |
|||
]; |
|||
} |
|||
|
|||
protected function convertToHTML($elements) |
|||
{ |
|||
$html = ''; |
|||
$url = url('/') . '/contracts-images'; |
|||
|
|||
foreach($elements as $key => $element) { |
|||
if($element->tag !== 'img') { |
|||
$html .= "<$element->tag style=\"$element->style\">$element->content</$element->tag>";
|
|||
} else { |
|||
$src = $url . '/' . str_replace(' ', '%20', $element->src); |
|||
$html .= "<img $element->style src=\"$src\" alt=\"$element->details\">";
|
|||
} |
|||
|
|||
if($key !== array_key_last($elements)) { |
|||
$html .= '<br>'; |
|||
} |
|||
} |
|||
|
|||
return $html; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue