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
77 lines
1.7 KiB
<?php
|
|
|
|
namespace App\SearchDisplace\Documents;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
use DOMDocument;
|
|
|
|
class DocumentFile
|
|
{
|
|
protected $storage;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->storage = Storage::disk('local');
|
|
}
|
|
|
|
public function getAfterIngest($id)
|
|
{
|
|
$path = $this->getPath($id);
|
|
|
|
// Ingest success.
|
|
if ($this->storage->exists("$path/document.html")) {
|
|
return [
|
|
'status' => 'success',
|
|
'content' => $this->getDocumentContent($path, $id),
|
|
];
|
|
}
|
|
|
|
// Ingest fail.
|
|
if ($this->storage->exists("$path/document")) {
|
|
return [
|
|
'status' => 'fail',
|
|
];
|
|
}
|
|
|
|
return [
|
|
'status' => 'processing',
|
|
'message' => 'Document has not been processed yet.',
|
|
];
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$path = $this->getPath($id);
|
|
|
|
return $this->storage->deleteDirectory($path);
|
|
}
|
|
|
|
protected function getPath($id)
|
|
{
|
|
return "contracts/$id";
|
|
}
|
|
|
|
protected function getDocumentContent($path, $id)
|
|
{
|
|
$document = $this->storage->path($path) . '/document.html';
|
|
|
|
$this->updateImagesPath($document, $id);
|
|
|
|
return file_get_contents($document);
|
|
}
|
|
|
|
public static function updateImagesPath($document, $id)
|
|
{
|
|
$html = new DOMDocument();
|
|
$html->loadHTMLFile($document, LIBXML_NOERROR);
|
|
$url = url('/') . "/contracts-images/$id/";
|
|
|
|
foreach($html->getElementsByTagName('img') as $image) {
|
|
$src = $image->getAttribute('src');
|
|
|
|
$image->setAttribute('src', $url . $src);
|
|
}
|
|
|
|
$html->saveHTMLFile($document);
|
|
}
|
|
}
|