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.

120 lines
2.9 KiB

  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 $filePath;
  16. private $id;
  17. /**
  18. * @var \Illuminate\Contracts\Filesystem\Filesystem
  19. */
  20. private $storage;
  21. /**
  22. * Create a new job instance.
  23. *
  24. * @param $filePath
  25. */
  26. public function __construct($filePath = null)
  27. {
  28. $this->url = env('WEBHOOK_CORE_URL') . '/webhooks';
  29. $this->secret = env('WEBHOOK_CORE_SECRET');
  30. $this->filePath = $filePath;
  31. $string = str_replace('contracts/', '', $this->filePath);
  32. $result = explode('.', $string);
  33. $this->id = $result[0];
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
  40. */
  41. public function handle()
  42. {
  43. $content = '';
  44. // File exists, send content.
  45. if ($this->filePath) {
  46. $this->storage = Storage::disk('local');
  47. // @TODO Check if the file exists multiple times?
  48. if ( ! $this->storage->exists($this->filePath)) {
  49. throw new \Exception('File does not exist yet.');
  50. }
  51. $content = $this->storage->get($this->filePath);
  52. }
  53. $sent = $this->sendTheData($content);
  54. // if ($this->filePath && $sent) {
  55. if ($this->filePath) {
  56. $this->storage->delete($this->filePath);
  57. }
  58. }
  59. public function failed()
  60. {
  61. if ($this->filePath) {
  62. $this->storage->delete($this->filePath);
  63. }
  64. }
  65. /**
  66. * Send the data to the core trough webhooks
  67. *
  68. * @param $content
  69. * @return bool
  70. */
  71. protected function sendTheData($content)
  72. {
  73. try {
  74. WebhookCall::create()
  75. ->url($this->url)
  76. ->payload(['data' => [
  77. 'id' => $this->id,
  78. 'content' => $this->encodeContent($content),
  79. 'status' => $content ? 'success' : 'fail',
  80. ]])
  81. ->useSecret($this->secret)
  82. ->dispatch();
  83. return true;
  84. } catch (\Exception $exception) {
  85. Log::error('SendToCore@sendTheData' . $exception->getMessage());
  86. return false;
  87. }
  88. }
  89. protected function encodeContent($content)
  90. {
  91. $encoding = mb_detect_encoding($content, 'UTF-8, ISO-8859-1, WINDOWS-1252, WINDOWS-1251', true);
  92. if ($encoding != 'UTF-8') {
  93. $content = iconv($encoding, 'UTF-8//IGNORE', $content);
  94. }
  95. return $content;
  96. }
  97. }