Repo for the search and displace core module including the interface to select files and search and displace operations to run on them. https://searchanddisplace.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

82 lines
2.2 KiB

<?php
namespace App\SearchDisplace\Ingest;
use App\Events\IngestDocumentReceived;
use App\SearchDisplace\SearchAndDisplaceOriginalDocument;
use Illuminate\Support\Facades\Storage;
class HandleReceivedDocument
{
protected $id;
protected $content;
protected $fileResultType;
protected $status;
public function __construct($payload)
{
$this->id = $payload['data']['id'];
$this->content = $payload['data']['content'];
$this->fileResultType = $payload['data']['file_result_type'];
$this->status = $payload['data']['status'];
}
public function handle()
{
if ($this->fileResultType === 'md') {
$this->handleDocumentMD();
return;
}
$this->handleDocumentJson();
}
protected function handleDocumentMD()
{
$storage = Storage::disk('local');
$fileName = 'document';
// The .md extension signals the success status, the lack of signals the fail status.
if ($this->status === 'success') {
$fileName = $fileName . '.md';
}
$dir = "contracts/$this->id";
try {
$storage->makeDirectory($dir);
$storage->put("$dir/$fileName", $this->content['document']);
foreach ($this->content['images'] as $image) {
$name = $image['name'];
$type = $image['type'];
$contents = $image['contents'];
$contents = str_replace('data:image/' . $type . ';base64,', '', $contents);
$storage->put("$dir/$name.$type", base64_decode($contents));
}
// Emit event so other sections of the app can work on it.
IngestDocumentReceived::dispatch($this->id);
} catch (\Exception $exception) {
\Illuminate\Support\Facades\Log::info('Exception: ' . $exception->getTraceAsString());
}
}
protected function handleDocumentJson()
{
$handler = new SearchAndDisplaceOriginalDocument();
if ($this->status !== 'success') {
$handler->onIngestFail($this->id);
return;
}
$handler->applySD($this->id, $this->content);
}
}