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.

64 lines
1.6 KiB

  1. <?php
  2. namespace App\SearchDisplace\Documents;
  3. use Illuminate\Support\Facades\Storage;
  4. class DocumentFile
  5. {
  6. protected $storage;
  7. public function __construct()
  8. {
  9. $this->storage = Storage::disk('local');
  10. }
  11. public function getAfterIngest($id)
  12. {
  13. $path = $this->getPath($id);
  14. // Ingest success.
  15. if ($this->storage->exists("$path/document.md")) {
  16. return [
  17. 'status' => 'success',
  18. 'content' => $this->getDocumentContent($id, $path),
  19. ];
  20. }
  21. // Ingest fail.
  22. if ($this->storage->exists("$path/document")) {
  23. return [
  24. 'status' => 'fail',
  25. ];
  26. }
  27. return [
  28. 'status' => 'processing',
  29. 'message' => 'Document has not been processed yet.',
  30. ];
  31. }
  32. public function destroy($id)
  33. {
  34. $path = $this->getPath($id);
  35. return $this->storage->deleteDirectory($path);
  36. }
  37. protected function getPath($id)
  38. {
  39. return "contracts/$id";
  40. }
  41. protected function getDocumentContent($id, $path)
  42. {
  43. $content = $this->storage->get("$path/document.md");
  44. $imageFullPath = url('/') . '/contracts-images/' . $id . '/';
  45. $imageFullPath = str_replace( ' ', '%20', $imageFullPath);
  46. // @TODO Use preg_replace to find correctly formatted images and any wild cards for the image caption.
  47. // return str_replace('![](./', '![](' . $imageFullPath, $content);
  48. return str_replace('](./', '](' . $imageFullPath, $content);
  49. }
  50. }