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.

77 lines
1.7 KiB

  1. <?php
  2. namespace App\SearchDisplace\Documents;
  3. use Illuminate\Support\Facades\Storage;
  4. use DOMDocument;
  5. class DocumentFile
  6. {
  7. protected $storage;
  8. public function __construct()
  9. {
  10. $this->storage = Storage::disk('local');
  11. }
  12. public function getAfterIngest($id)
  13. {
  14. $path = $this->getPath($id);
  15. // Ingest success.
  16. if ($this->storage->exists("$path/document.html")) {
  17. return [
  18. 'status' => 'success',
  19. 'content' => $this->getDocumentContent($path, $id),
  20. ];
  21. }
  22. // Ingest fail.
  23. if ($this->storage->exists("$path/document")) {
  24. return [
  25. 'status' => 'fail',
  26. ];
  27. }
  28. return [
  29. 'status' => 'processing',
  30. 'message' => 'Document has not been processed yet.',
  31. ];
  32. }
  33. public function destroy($id)
  34. {
  35. $path = $this->getPath($id);
  36. return $this->storage->deleteDirectory($path);
  37. }
  38. protected function getPath($id)
  39. {
  40. return "contracts/$id";
  41. }
  42. protected function getDocumentContent($path, $id)
  43. {
  44. $document = $this->storage->path($path) . '/document.html';
  45. $this->updateImagesPath($document, $id);
  46. return file_get_contents($document);
  47. }
  48. public static function updateImagesPath($document, $id)
  49. {
  50. $html = new DOMDocument();
  51. $html->loadHTMLFile($document, LIBXML_NOERROR);
  52. $url = url('/') . "/contracts-images/$id/";
  53. foreach($html->getElementsByTagName('img') as $image) {
  54. $src = $image->getAttribute('src');
  55. $image->setAttribute('src', $url . $src);
  56. }
  57. $html->saveHTMLFile($document);
  58. }
  59. }