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.

108 lines
2.5 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. */
  40. public function handle()
  41. {
  42. $content = '';
  43. // File exists, send content.
  44. if ($this->filePath) {
  45. $this->storage = Storage::disk('local');
  46. // @TODO Check if the file exists multiple times?
  47. if ( ! $this->storage->exists($this->filePath)) {
  48. throw new \Exception('File does not exist yet.');
  49. }
  50. $content = $this->storage->get($this->filePath);
  51. }
  52. $sent = $this->sendTheData($content);
  53. // if ($this->filePath && $sent) {
  54. if ($this->filePath) {
  55. $this->storage->delete($this->filePath);
  56. }
  57. }
  58. public function failed()
  59. {
  60. if ($this->filePath) {
  61. $this->storage->delete($this->filePath);
  62. }
  63. }
  64. /**
  65. * Send the data to the core trough webhooks
  66. *
  67. * @param $content
  68. * @param string $status
  69. */
  70. private function sendTheData($content)
  71. {
  72. try {
  73. WebhookCall::create()
  74. ->url($this->url)
  75. ->payload(['data' => [
  76. 'id' => $this->id,
  77. 'content' => $content,
  78. 'status' => $content ? 'success' : 'fail',
  79. ]])
  80. ->useSecret($this->secret)
  81. ->dispatch();
  82. return true;
  83. } catch (\Exception $exception) {
  84. Log::error('SendToCore@sendTheData' . $exception->getMessage());
  85. return false;
  86. }
  87. }
  88. }