Alex Puiu
2 years ago
10 changed files with 205 additions and 185 deletions
-
33app/Http/Controllers/FileController.php
-
38app/Http/Controllers/SearchAndDisplaceController.php
-
35app/SearchDisplace/Convertor/Convertor.php
-
37app/SearchDisplace/Documents/DocumentFile.php
-
4app/SearchDisplace/Ingest/HandleReceivedDocument.php
-
4app/SearchDisplace/Ingest/SendDocument.php
-
4app/SearchDisplace/SearchAndDisplace.php
-
98app/SearchDisplace/SearchAndDisplaceJSON.php
-
129app/SearchDisplace/SearchAndDisplaceXML.php
-
8resources/js/components/ProcessFile/ProcessFile.ts
@ -0,0 +1,35 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\SearchDisplace\Convertor; |
||||
|
|
||||
|
use Symfony\Component\Process\Process; |
||||
|
|
||||
|
/** |
||||
|
* Convert documents from formats supported by Libre Office |
||||
|
*/ |
||||
|
|
||||
|
class Convertor { |
||||
|
/** |
||||
|
* @param $to desired file format |
||||
|
* @param $document absolute file path |
||||
|
* @param $tmp - if true file will be saved to tmp directory for download |
||||
|
* |
||||
|
* @return string $path to converted file |
||||
|
*/ |
||||
|
public static function convert($to, $document, $tmp = false) |
||||
|
{ |
||||
|
$path = pathinfo($document); |
||||
|
$dir = $path['dirname']; |
||||
|
$original = $dir . '/' . $path['basename']; |
||||
|
if(!$tmp) { |
||||
|
$folder = $dir . '/'; |
||||
|
} else { |
||||
|
$folder = storage_path('app/tmp/'); |
||||
|
} |
||||
|
|
||||
|
$process = new Process(['soffice', '--convert-to', $to, $original, '--outdir', $folder]); |
||||
|
$process->run(); |
||||
|
|
||||
|
return $path['filename'] . '.' . $to; |
||||
|
} |
||||
|
} |
@ -1,98 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace App\SearchDisplace; |
|
||||
|
|
||||
use Illuminate\Support\Facades\Storage; |
|
||||
use App\SearchDisplace\Output\HtmlOutput; |
|
||||
|
|
||||
class SearchAndDisplaceJSON |
|
||||
{ |
|
||||
protected $file; |
|
||||
protected $searchers; |
|
||||
protected $searchOnly; |
|
||||
protected $content; |
|
||||
|
|
||||
public function __construct($file, $searchers, $searchOnly) |
|
||||
{ |
|
||||
$searchOnly ? $this->content = $file : $this->file = $file . '/document.json'; |
|
||||
$this->searchers = $searchers; |
|
||||
|
|
||||
$this->storage = Storage::disk('local'); |
|
||||
} |
|
||||
|
|
||||
public function execute() |
|
||||
{ |
|
||||
if(isset($this->content)) { |
|
||||
$search = new SearchAndDisplace( |
|
||||
stripslashes($this->content), |
|
||||
[ |
|
||||
'searchers' => $this->searchers, |
|
||||
], |
|
||||
true |
|
||||
); |
|
||||
|
|
||||
return $search->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; |
|
||||
} |
|
||||
|
|
||||
$searchDisplace = $this->applySD($content); |
|
||||
|
|
||||
$convert = new HtmlOutput($searchDisplace); |
|
||||
|
|
||||
return $convert->getData(); |
|
||||
} |
|
||||
|
|
||||
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) { |
|
||||
foreach($changed['indexes'] as $key => $searcher) { |
|
||||
foreach($searcher as $change) { |
|
||||
if($change['start']) { |
|
||||
$indexes[$key][] = $change; |
|
||||
$element->paragraph = $key; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
$element->content = $changed['content']; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return [ |
|
||||
'content' => $elements, |
|
||||
'indexes' => $indexes |
|
||||
]; |
|
||||
} |
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue