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.

54 lines
1.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\SearchDisplace\Ingest;
  3. use App\Events\IngestDocumentReceived;
  4. use Illuminate\Support\Facades\Storage;
  5. class HandleReceivedDocument
  6. {
  7. protected $id;
  8. protected $content;
  9. protected $status;
  10. public function __construct($payload)
  11. {
  12. $this->id = $payload['data']['id'];
  13. $this->content = $payload['data']['content'];
  14. $this->status = $payload['data']['status'];
  15. }
  16. public function handle()
  17. {
  18. $storage = Storage::disk('local');
  19. $fileName = 'document';
  20. // The .md extension signals the success status, the lack of signals the fail status.
  21. if ($this->status === 'success') {
  22. $fileName = $fileName . '.md';
  23. }
  24. $dir = "contracts/$this->id";
  25. try {
  26. $storage->makeDirectory($dir);
  27. $storage->put("$dir/$fileName", $this->content['document']);
  28. foreach ($this->content['images'] as $image) {
  29. $name = $image['name'];
  30. $type = $image['type'];
  31. $contents = $image['contents'];
  32. $contents = str_replace('data:image/' . $type . ';base64,', '', $contents);
  33. $storage->put("$dir/$name.$type", base64_decode($contents));
  34. }
  35. IngestDocumentReceived::dispatch($this->id);
  36. } catch (\Exception $exception) {
  37. \Illuminate\Support\Facades\Log::info('Exception: ' . $exception->getTraceAsString());
  38. }
  39. }
  40. }