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.

132 lines
3.3 KiB

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