Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
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.

172 lines
4.6 KiB

  1. <?php
  2. namespace App\Jobs;
  3. use App\Ingest\DocxAndOdtConvertor;
  4. use App\Ingest\DocxWriter;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Storage;
  12. use Spatie\WebhookServer\WebhookCall;
  13. class RecreateDocument implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $id;
  17. protected $data;
  18. protected $storage;
  19. protected $path;
  20. protected $url;
  21. protected $secret;
  22. /**
  23. * Create a new job instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct($id, $data)
  28. {
  29. $this->id = $id;
  30. $this->data = $data;
  31. $this->storage = Storage::disk('local');
  32. $this->path = '';
  33. $this->url = env('WEBHOOK_CORE_URL') . '/webhooks';
  34. $this->secret = env('WEBHOOK_CORE_SECRET');
  35. }
  36. /**
  37. * Execute the job.
  38. *
  39. * @return void
  40. */
  41. public function handle()
  42. {
  43. try {
  44. $this->setupData();
  45. $this->createDocx();
  46. $this->convertToOriginalDocumentFormat();
  47. $this->sendResponse('success');
  48. } catch (\Exception $exception) {
  49. \Illuminate\Support\Facades\Log::info('RecreateDocument@handle: ' . $exception->getMessage());
  50. \Illuminate\Support\Facades\Log::info($exception->getTraceAsString());
  51. $this->sendResponse('fail');
  52. }
  53. }
  54. protected function setupData()
  55. {
  56. $text = $this->data['contents']['text'];
  57. $textMapper = [];
  58. foreach ($this->data['contents']['elements'] as $element) {
  59. $textMapper[$element['hash']] =substr(
  60. $text,
  61. $element['range_start'],
  62. $element['range_end'] - $element['range_start'] + 1
  63. );
  64. }
  65. $this->data['elements'] = $this->updateText($this->data['elements'], $textMapper);
  66. }
  67. protected function updateText($elements, $textMapper)
  68. {
  69. foreach ($elements as $index => $element) {
  70. if (array_key_exists('hash', $element)) {
  71. $elements[$index]['text'] = $textMapper[$element['hash']];
  72. }
  73. if (
  74. array_key_exists('text_object', $element) &&
  75. array_key_exists('text', $element['text_object'])
  76. ) {
  77. $elements[$index]['text_object']['text'] = $textMapper[$element['text_object']['hash']];
  78. }
  79. if (isset($elements[$index]['elements'])) {
  80. $elements[$index]['elements'] = $this->updateText($elements[$index]['elements'], $textMapper);
  81. }
  82. }
  83. return $elements;
  84. }
  85. protected function createDocx()
  86. {
  87. $path = 'contracts/' . $this->id . '-document.docx';
  88. $writer = new DocxWriter($this->storage, $path);
  89. $writer->execute($this->data);
  90. $this->path = $path;
  91. }
  92. /**
  93. * @throws \Exception
  94. */
  95. protected function convertToOriginalDocumentFormat()
  96. {
  97. if ($this->data['document_format'] === 'docx') {
  98. return;
  99. }
  100. $convertor = new DocxAndOdtConvertor($this->storage, $this->path);
  101. if ($this->data['document_format'] === 'pdf') {
  102. $convertor->convertToPdfWithLibreOffice();
  103. }
  104. if ($this->data['document_format'] === 'odt') {
  105. $convertor->convertToODT();
  106. }
  107. if ($this->data['document_format'] === 'rtf') {
  108. $convertor->convertToRTF();
  109. }
  110. if ($this->data['document_format'] === 'doc') {
  111. $convertor->convertToDOC();
  112. }
  113. if ($this->data['document_format'] === 'txt') {
  114. $convertor->convertToTXT();
  115. }
  116. $this->path = $convertor->getPath();
  117. }
  118. protected function sendResponse($status)
  119. {
  120. try {
  121. WebhookCall::create()
  122. ->url($this->url)
  123. ->payload(['data' => [
  124. 'id' => $this->id,
  125. 'content' => '',
  126. 'file_result_type' => 'document-recreated',
  127. 'document_format' => $this->data['document_format'],
  128. 'status' => $status,
  129. ]])
  130. ->useSecret($this->secret)
  131. ->dispatch();
  132. return true;
  133. } catch (\Exception $exception) {
  134. Log::error('RecreateDocument@sendDocument: ' . $exception->getMessage());
  135. return false;
  136. }
  137. }
  138. }