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.

169 lines
4.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Storage;
  9. use Spatie\WebhookServer\WebhookCall;
  10. class SendToCore implements ShouldQueue
  11. {
  12. use Dispatchable, InteractsWithQueue, Queueable;
  13. protected $url;
  14. protected $secret;
  15. protected $directoryPath;
  16. protected $fileResultType;
  17. protected $documentFormat;
  18. protected $id;
  19. protected $hasFailed;
  20. /**
  21. * @var \Illuminate\Contracts\Filesystem\Filesystem
  22. */
  23. private $storage;
  24. /**
  25. * Create a new job instance.
  26. *
  27. * @param $id
  28. * @param string $fileResultType
  29. * @param string $documentFormat
  30. * @param null $directoryPath
  31. * @param bool $hasFailed
  32. */
  33. public function __construct($id, string $fileResultType, string $documentFormat, $directoryPath = null, bool $hasFailed = false)
  34. {
  35. $this->url = env('WEBHOOK_CORE_URL') . '/webhooks';
  36. $this->secret = env('WEBHOOK_CORE_SECRET');
  37. $this->id = $id;
  38. $this->fileResultType = $fileResultType;
  39. $this->documentFormat = $documentFormat;
  40. $this->directoryPath = $directoryPath;
  41. $this->hasFailed = $hasFailed;
  42. }
  43. /**
  44. * Execute the job.
  45. *
  46. * @return void
  47. * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
  48. */
  49. public function handle()
  50. {
  51. $content = [];
  52. // Directory exists, send content.
  53. if ($this->directoryPath && ! $this->hasFailed) {
  54. $this->storage = Storage::disk('local');
  55. // @TODO Check if the file exists multiple times?
  56. if ( ! $this->storage->exists($this->directoryPath)) {
  57. throw new \Exception('File does not exist yet.');
  58. }
  59. $content = $this->getContent();
  60. }
  61. $sent = $this->sendTheData($content);
  62. // if ($this->directoryPath && $sent) {
  63. if ($this->directoryPath) {
  64. if ( ! $this->storage) {
  65. $this->storage = Storage::disk('local');
  66. }
  67. $this->storage->deleteDirectory($this->directoryPath);
  68. }
  69. }
  70. public function failed()
  71. {
  72. if ($this->directoryPath) {
  73. if ( ! $this->storage) {
  74. $this->storage = Storage::disk('local');
  75. }
  76. $this->storage->delete($this->directoryPath);
  77. }
  78. }
  79. /**
  80. * Send the data to the core through webhooks
  81. *
  82. * @param array$content
  83. * @return bool
  84. */
  85. protected function sendTheData(array $content)
  86. {
  87. try {
  88. WebhookCall::create()
  89. ->url($this->url)
  90. ->payload(['data' => [
  91. 'id' => $this->id,
  92. 'content' => $content,
  93. 'file_result_type' => $this->fileResultType,
  94. 'document_format' => $this->documentFormat,
  95. 'status' => count($content) > 0 ? 'success' : 'fail',
  96. ]])
  97. ->useSecret($this->secret)
  98. ->dispatch();
  99. return true;
  100. } catch (\Exception $exception) {
  101. Log::error('SendToCore@sendTheData: ' . $exception->getMessage());
  102. return false;
  103. }
  104. }
  105. protected function getContent()
  106. {
  107. $extension = $this->fileResultType === 'md' ? 'md' : 'json';
  108. $filePath = "$this->directoryPath/document.$extension";
  109. $document = $this->storage->get($filePath);
  110. $document = $this->encodeContent($document);
  111. $images = [];
  112. if ($extension === 'md') {
  113. $allFiles = $this->storage->allFiles($this->directoryPath);
  114. foreach ($allFiles as $file) {
  115. // @TODO We are using this check in the 'PDFConvertor' file, refactor and improve.
  116. if (in_array(pathinfo($file, PATHINFO_EXTENSION), ['jpg', 'png'])) {
  117. $name = pathinfo($file, PATHINFO_FILENAME);
  118. $type = pathinfo($file, PATHINFO_EXTENSION);
  119. $images[] = [
  120. 'name' => $name,
  121. 'type' => $type,
  122. 'contents' => 'data:image/' . $type . ';base64,' . base64_encode($this->storage->get($file)),
  123. ];
  124. }
  125. }
  126. }
  127. return [
  128. 'document' => $document,
  129. 'images' => $images,
  130. ];
  131. }
  132. protected function encodeContent($content)
  133. {
  134. $encoding = mb_detect_encoding($content, 'UTF-8, ISO-8859-1, WINDOWS-1252, WINDOWS-1251', true);
  135. if ($encoding != 'UTF-8') {
  136. $content = iconv($encoding, 'UTF-8//IGNORE', $content);
  137. }
  138. return $content;
  139. }
  140. }