Repo for the search and displace core module including the interface to select files and search and displace operations to run on them. https://searchanddisplace.com
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.

82 lines
2.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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\SearchDisplace\Ingest;
  3. use App\Events\IngestDocumentReceived;
  4. use App\SearchDisplace\SearchAndDisplaceOriginalDocument;
  5. use Illuminate\Support\Facades\Storage;
  6. class HandleReceivedDocument
  7. {
  8. protected $id;
  9. protected $content;
  10. protected $fileResultType;
  11. protected $status;
  12. public function __construct($payload)
  13. {
  14. $this->id = $payload['data']['id'];
  15. $this->content = $payload['data']['content'];
  16. $this->fileResultType = $payload['data']['file_result_type'];
  17. $this->status = $payload['data']['status'];
  18. }
  19. public function handle()
  20. {
  21. if ($this->fileResultType === 'md') {
  22. $this->handleDocumentMD();
  23. return;
  24. }
  25. $this->handleDocumentJson();
  26. }
  27. protected function handleDocumentMD()
  28. {
  29. $storage = Storage::disk('local');
  30. $fileName = 'document';
  31. // The .md extension signals the success status, the lack of signals the fail status.
  32. if ($this->status === 'success') {
  33. $fileName = $fileName . '.md';
  34. }
  35. $dir = "contracts/$this->id";
  36. try {
  37. $storage->makeDirectory($dir);
  38. $storage->put("$dir/$fileName", $this->content['document']);
  39. foreach ($this->content['images'] as $image) {
  40. $name = $image['name'];
  41. $type = $image['type'];
  42. $contents = $image['contents'];
  43. $contents = str_replace('data:image/' . $type . ';base64,', '', $contents);
  44. $storage->put("$dir/$name.$type", base64_decode($contents));
  45. }
  46. // Emit event so other sections of the app can work on it.
  47. IngestDocumentReceived::dispatch($this->id);
  48. } catch (\Exception $exception) {
  49. \Illuminate\Support\Facades\Log::info('Exception: ' . $exception->getTraceAsString());
  50. }
  51. }
  52. protected function handleDocumentJson()
  53. {
  54. $handler = new SearchAndDisplaceOriginalDocument();
  55. if ($this->status !== 'success') {
  56. $handler->onIngestFail($this->id);
  57. return;
  58. }
  59. $handler->applySD($this->id, $this->content);
  60. }
  61. }