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.

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