storage = Storage::disk('local');
}
public function getAfterIngest($id)
{
$path = $this->getPath($id);
// Ingest success.
if ($this->storage->exists("$path/document.json")) {
return [
'status' => 'success',
'content' => $this->getDocumentContent($path),
];
}
// 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)
{
$content = json_decode($this->storage->get("$path/document.json"));
return $this->convertToHTML($content);
}
protected function convertToHTML($elements)
{
$html = '';
$url = url('/') . '/contracts-images';
foreach($elements as $key => $element) {
if($element->tag !== 'img') {
$html .= "<$element->tag style=\"$element->style\">$element->content$element->tag>";
} else {
$src = $url . '/' . str_replace(' ', '%20', $element->src);
$html .= "
style src=\"$src\" alt=\"$element->details\">";
}
if($key !== array_key_last($elements))
$html .= '
';
}
return $html;
}
}