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.

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