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.

183 lines
4.1 KiB

3 years ago
3 years ago
  1. <?php
  2. namespace App\Jobs;
  3. use App\Ingest\Convertor;
  4. use App\Ingest\MDConvertor;
  5. use App\Parser\ParseXml;
  6. use App\Parser\DocxParser\ParseDocx;
  7. use App\Parser\HtmlParser\ParseHtml;
  8. use App\Parser\ParseHtmlArray;
  9. use App\Parser\ParseTextArray;
  10. use Illuminate\Bus\Queueable;
  11. use Illuminate\Contracts\Queue\ShouldQueue;
  12. use Illuminate\Foundation\Bus\Dispatchable;
  13. use Illuminate\Queue\InteractsWithQueue;
  14. use Illuminate\Support\Facades\Log;
  15. use Illuminate\Support\Facades\Storage;
  16. class IngestDocuments implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable;
  19. private $path;
  20. protected $type;
  21. /**
  22. * @var \Illuminate\Contracts\Filesystem\Filesystem
  23. */
  24. private $storage;
  25. /**
  26. * @var \App\Parser\DocxParser\ParseDocx
  27. */
  28. private $parserDocx;
  29. /**
  30. * @var \App\Parser\ParseXml
  31. */
  32. private $parserXml;
  33. /**
  34. * @var \App\Parser\HtmlParser\ParseHtml
  35. */
  36. private $parserHtml;
  37. /**
  38. * @var \App\Parser\ParseHtmlArray
  39. */
  40. private $parseHtmlArray;
  41. /**
  42. * @var \App\Parser\ParseTextArray
  43. */
  44. private $parserText;
  45. /**
  46. * Create a new job instance.
  47. *
  48. * @param string $path
  49. */
  50. public function __construct(string $path, $type)
  51. {
  52. $this->path = $path;
  53. $this->type = $type;
  54. $this->storage = Storage::disk('local');
  55. $this->parserDocx = new ParseDocx();
  56. $this->parserText = new ParseTextArray();
  57. $this->parserXml = new ParseXml();
  58. $this->parserHtml = new ParseHtml();
  59. $this->parseHtmlArray = new ParseHtmlArray();
  60. }
  61. /**
  62. * Execute the job.
  63. *
  64. * @return void
  65. */
  66. public function handle()
  67. {
  68. $convertor = new Convertor($this->path, $this->type);
  69. $this->path = $convertor->execute();
  70. $content = $this->getContent();
  71. if ( ! $content) {
  72. return;
  73. }
  74. // $content = $this->convertToUTF8($content);
  75. try {
  76. $filePath = $this->storeContent($content);
  77. SendToCore::dispatch($filePath);
  78. } catch (\Exception $e) {
  79. Log::error('Error writing in to the file: ' . $e->getMessage());
  80. // report($e);
  81. }
  82. }
  83. public function failed()
  84. {
  85. if ( ! $this->storage) {
  86. $this->storage = Storage::disk('local');
  87. }
  88. Log::error('Ingest documents failed.');
  89. // @TODO Delete docx, txt and md files.
  90. if ($this->storage->exists($this->path)) {
  91. $this->storage->delete($this->path);
  92. }
  93. SendToCore::dispatch(null);
  94. }
  95. protected function getContent()
  96. {
  97. if ($this->type === 'pdf') {
  98. // Wait while it finishes.
  99. while (!$this->storage->exists($this->path)) {
  100. sleep(1);
  101. }
  102. $textParser = new ParseTextArray(true);
  103. return $textParser->fromFile($this->storage->path($this->path));
  104. }
  105. return $this->parserText->fromFile($this->storage->path($this->path));
  106. }
  107. protected function convertToUTF8($content)
  108. {
  109. array_walk_recursive(
  110. $content,
  111. function (&$entry) {
  112. $entry = mb_convert_encoding(
  113. $entry,
  114. 'UTF-8'
  115. );
  116. }
  117. );
  118. return $content;
  119. }
  120. protected function storeContent($content)
  121. {
  122. $result = explode('.', $this->path);
  123. $name = $result[0];
  124. // Or json?
  125. $filePath = $this->storeMD($name, $content);
  126. // Delete converted file. We now have the .md file.
  127. $this->storage->delete($this->path);
  128. return $filePath;
  129. }
  130. protected function storeMD($name, $content)
  131. {
  132. $fileName = "$name.md";
  133. $convertor = new MDConvertor($content);
  134. $this->storage->put($fileName, $convertor->execute());
  135. return $fileName;
  136. }
  137. protected function storeJson($name, $content)
  138. {
  139. $fileName = "$name.json";
  140. $this->storage->put($fileName, $content);
  141. return $fileName;
  142. }
  143. }