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.

161 lines
4.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace App\SearchDisplace\Ingest;
  3. use App\Events\IngestDocumentReceived;
  4. use App\Events\SDAppliedOnOriginalDocument;
  5. use App\SearchDisplace\SearchAndDisplaceOriginalDocument;
  6. use GuzzleHttp\Client;
  7. use GuzzleHttp\Exception\ClientException;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Facades\Storage;
  10. class HandleReceivedDocument
  11. {
  12. protected $id;
  13. protected $content;
  14. protected $fileResultType;
  15. protected $documentFormat;
  16. protected $status;
  17. public function __construct($payload)
  18. {
  19. $this->id = $payload['data']['id'];
  20. $this->content = $payload['data']['content'];
  21. $this->fileResultType = $payload['data']['file_result_type'];
  22. $this->status = $payload['data']['status'];
  23. if (isset($payload['data']['document_format'])) {
  24. $this->documentFormat = $payload['data']['document_format'];
  25. }
  26. }
  27. /**
  28. * @throws \Exception|\GuzzleHttp\Exception\GuzzleException
  29. */
  30. public function handle()
  31. {
  32. if ($this->fileResultType === 'md') {
  33. $this->handleDocumentMD();
  34. return;
  35. }
  36. if ($this->fileResultType === 'document-recreated') {
  37. $this->downloadDocumentFromIngest();
  38. return;
  39. }
  40. $this->handleDocumentJson();
  41. }
  42. protected function handleDocumentMD()
  43. {
  44. $storage = Storage::disk('local');
  45. $fileName = 'document';
  46. // The .md extension signals the success status, the lack of signals the fail status.
  47. if ($this->status === 'success') {
  48. $fileName = $fileName . '.md';
  49. }
  50. $dir = "contracts/$this->id";
  51. try {
  52. $storage->makeDirectory($dir);
  53. $storage->put("$dir/$fileName", $this->content['document']);
  54. foreach ($this->content['images'] as $image) {
  55. $name = $image['name'];
  56. $type = $image['type'];
  57. $contents = $image['contents'];
  58. $contents = str_replace('data:image/' . $type . ';base64,', '', $contents);
  59. $storage->put("$dir/$name.$type", base64_decode($contents));
  60. }
  61. // Emit event so other sections of the app can work on it.
  62. IngestDocumentReceived::dispatch($this->id);
  63. } catch (\Exception $exception) {
  64. \Illuminate\Support\Facades\Log::info('Exception: ' . $exception->getTraceAsString());
  65. }
  66. }
  67. /**
  68. * @throws \Exception|\GuzzleHttp\Exception\GuzzleException
  69. */
  70. protected function downloadDocumentFromIngest()
  71. {
  72. $storage = Storage::disk('local');
  73. if ($this->status === 'fail') {
  74. $storage->deleteDirectory("contracts/$this->id");
  75. return;
  76. }
  77. if ( ! $this->documentFormat) {
  78. throw new \Exception('No document format specified.');
  79. }
  80. $filePath = "document.$this->documentFormat";
  81. $storeDocumentAtPath = $storage->path('contracts/' . $this->id . '-' . $filePath);
  82. try {
  83. // Change the path where to store the document if the operation was run from CLI.
  84. $storeAtPathFromJsonFile = (new SearchAndDisplaceOriginalDocument())->getStoreAtPathFromJsonFile($this->id);
  85. if ($storeAtPathFromJsonFile) {
  86. $storeDocumentAtPath = $storeAtPathFromJsonFile .
  87. '/' .
  88. $this->id .
  89. '-displaced.' .
  90. $this->documentFormat;
  91. }
  92. } catch (\Exception $exception) {
  93. \Illuminate\Support\Facades\Log::info('Exception thrown when tried reading the storeDocumentAtPath from JSON file.');
  94. }
  95. $client = new Client();
  96. $url = env('SD_INGEST_URL') . '/recreate-document/' . $this->id . '?file_path=' . $filePath;
  97. try {
  98. $client->request('GET', $url, [
  99. 'sink' => $storeDocumentAtPath,
  100. ]);
  101. } catch (ClientException $clientException) {
  102. $error = json_decode($clientException->getResponse()->getBody()->getContents(), true);
  103. throw new \Exception($error['message']);
  104. } finally {
  105. $storage->deleteDirectory("contracts/$this->id");
  106. }
  107. SDAppliedOnOriginalDocument::dispatch($this->id);
  108. }
  109. /**
  110. * @throws \Exception|\GuzzleHttp\Exception\GuzzleException
  111. */
  112. protected function handleDocumentJson()
  113. {
  114. $handler = new SearchAndDisplaceOriginalDocument();
  115. if ($this->status !== 'success') {
  116. $handler->onIngestFail($this->id);
  117. return;
  118. }
  119. if ( ! $this->documentFormat) {
  120. throw new \Exception('No document format specified.');
  121. }
  122. $handler->applySD($this->id, $this->content, $this->documentFormat);
  123. }
  124. }