Browse Source

S&D on original document recreates document with the original format

master
Orzu Ionut 3 years ago
parent
commit
dc131b7751
  1. 8
      app/Http/Controllers/SearchAndDisplaceOriginalDocumentController.php
  2. 31
      app/SearchDisplace/Ingest/HandleReceivedDocument.php
  3. 49
      app/SearchDisplace/SearchAndDisplaceOriginalDocument.php

8
app/Http/Controllers/SearchAndDisplaceOriginalDocumentController.php

@ -14,7 +14,8 @@ class SearchAndDisplaceOriginalDocumentController extends Controller
'required',
'file',
'max:10000',
'mimes:doc,dot,docx,dotx,docm,dotm,odt,rtf,pdf,txt',
// 'mimes:doc,dot,docx,dotx,docm,dotm,odt,rtf,pdf,txt',
'mimes:doc,docx,odt,rtf,pdf,txt',
],
// Searchers is encoded.
@ -36,7 +37,10 @@ class SearchAndDisplaceOriginalDocumentController extends Controller
try {
$handler = new SearchAndDisplaceOriginalDocument();
$id = $handler->start(request()->file('document'), json_decode(request()->get('searchers'), true));
$id = $handler->start(
request()->file('document'),
json_decode(request()->get('searchers'), true)
);
return response()->json([
'status' => 'ok',

31
app/SearchDisplace/Ingest/HandleReceivedDocument.php

@ -13,6 +13,7 @@ class HandleReceivedDocument
protected $id;
protected $content;
protected $fileResultType;
protected $documentFormat;
protected $status;
public function __construct($payload)
@ -21,10 +22,14 @@ class HandleReceivedDocument
$this->content = $payload['data']['content'];
$this->fileResultType = $payload['data']['file_result_type'];
$this->status = $payload['data']['status'];
if (isset($payload['data']['document_format'])) {
$this->documentFormat = $payload['data']['document_format'];
}
}
/**
* @throws \Exception
* @throws \Exception|\GuzzleHttp\Exception\GuzzleException
*/
public function handle()
{
@ -78,27 +83,32 @@ class HandleReceivedDocument
}
}
/**
* @throws \Exception|\GuzzleHttp\Exception\GuzzleException
*/
protected function downloadDocumentFromIngest()
{
$storage = Storage::disk('local');
if ($this->status === 'fail') {
$storage->deleteDirectory("contracts/$this->id");
return;
}
$filePath = 'document.docx';
$fullPath = 'contracts/' . $this->id . '-' . $filePath;
if ( ! $this->documentFormat) {
throw new \Exception('No document format specified.');
}
$url = env('SD_INGEST_URL') . '/recreate-document/' . $this->id . '?file_path=' . $filePath;
$filePath = "document.$this->documentFormat";
$storeDocumentAtPath = 'contracts/' . $this->id . '-' . $filePath;
$client = new Client();
$url = env('SD_INGEST_URL') . '/recreate-document/' . $this->id . '?file_path=' . $filePath;
try {
$client->request('GET', $url, [
'sink' => $storage->path($fullPath),
'sink' => $storage->path($storeDocumentAtPath),
]);
} catch (ClientException $clientException) {
$error = json_decode($clientException->getResponse()->getBody()->getContents(), true);
@ -109,6 +119,9 @@ class HandleReceivedDocument
}
}
/**
* @throws \Exception|\GuzzleHttp\Exception\GuzzleException
*/
protected function handleDocumentJson()
{
$handler = new SearchAndDisplaceOriginalDocument();
@ -119,6 +132,10 @@ class HandleReceivedDocument
return;
}
$handler->applySD($this->id, $this->content);
if ( ! $this->documentFormat) {
throw new \Exception('No document format specified.');
}
$handler->applySD($this->id, $this->content, $this->documentFormat);
}
}

49
app/SearchDisplace/SearchAndDisplaceOriginalDocument.php

@ -25,9 +25,10 @@ class SearchAndDisplaceOriginalDocument
/**
* @param $id
* @param $contents
* @param $documentFormat
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function applySD($id, $contents)
public function applySD($id, $contents, $documentFormat)
{
$data = json_decode($contents['document'], true);
@ -46,6 +47,8 @@ class SearchAndDisplaceOriginalDocument
$x = $this->applyResultsOnIngestData($data['contents'], $result);
$data['contents'] = $x;
$data['document_format'] = $documentFormat;
$this->sendDataToIngestToRebuild($id, $data);
} catch (\Exception $exception) {
\Illuminate\Support\Facades\Log::info($exception->getMessage());
@ -64,9 +67,16 @@ class SearchAndDisplaceOriginalDocument
{
$storage = Storage::disk('local');
$basePath = "contracts/$id";
$filePath = $basePath . '-document.docx';
return ! $storage->exists($basePath) && ! $storage->exists($filePath);
if ($storage->exists($basePath)) {
return false;
}
if ($this->findDocumentById($id) !== null) {
return false;
}
return true;
}
public function isInProgress($id)
@ -74,8 +84,7 @@ class SearchAndDisplaceOriginalDocument
$storage = Storage::disk('local');
$basePath = "contracts/$id";
// @TODO Set document extension.
return $storage->exists($basePath) && ! $storage->exists($basePath . '-document.docx');
return $storage->exists($basePath) && $this->findDocumentById($id) === null;
}
/**
@ -87,12 +96,34 @@ class SearchAndDisplaceOriginalDocument
{
$storage = Storage::disk('local');
if ($this->hasFailed($id) || $this->isInProgress($id)) {
throw new \Exception('Document is not processed.');
// @TODO Improve this. Right now we are calling 'findDocumentById' multiple times.
if ($this->hasFailed($id)) {
throw new \Exception('Document has failed.');
}
if ($this->isInProgress($id)) {
throw new \Exception('Document is still processing.');
}
return $storage->path($this->findDocumentById($id));
}
protected function findDocumentById($id)
{
$storage = Storage::disk('local');
$documentPartialPath = "contracts/$id-document";
$contractFiles = $storage->files('contracts');
foreach ($contractFiles as $contractFile) {
if (substr($contractFile, 0, strlen($documentPartialPath)) === $documentPartialPath) {
return $contractFile;
}
}
// @TODO Set document extension.
return $storage->path('contracts/' . $id . '-document.docx');
return null;
}
protected function applyResultsOnIngestData($ingestData, $sdResult)

Loading…
Cancel
Save