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.

59 lines
1.4 KiB

  1. <?php
  2. namespace App\SearchDisplace\Ingest;
  3. use GuzzleHttp\Client;
  4. class SendDocument
  5. {
  6. protected $url;
  7. public function __construct()
  8. {
  9. $this->url = env('SD_INGEST_URL') . '/ingest';
  10. }
  11. public function execute($documentPath, $id)
  12. {
  13. try {
  14. $response = $this->sendRequest($documentPath, $id);
  15. if ($response['status'] === 'fail') {
  16. $message = array_key_exists('message', $response)
  17. ? $response['message']
  18. : 'Something went wrong.';
  19. throw new \Exception($message);
  20. }
  21. // The file in Ingest si in Processing state.
  22. } catch (\Exception $exception) {
  23. throw new \Exception($exception->getMessage());
  24. }
  25. }
  26. public function sendRequest($documentPath, $id)
  27. {
  28. $client = new Client();
  29. $response = $client->request('post', $this->url, [
  30. 'headers' => [
  31. 'Accept' => 'application/json',
  32. ],
  33. 'multipart' => [
  34. [
  35. 'name' => 'id',
  36. 'contents' => $id,
  37. ],
  38. [
  39. 'name' => 'document',
  40. 'contents' => fopen($documentPath, 'r'),
  41. ]
  42. ],
  43. ]);
  44. return json_decode($response->getBody()->getContents(), true);
  45. }
  46. }