Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
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.
 
 
 
 

72 lines
1.6 KiB

<?php
namespace App\Ingest;
use Illuminate\Support\Facades\Storage;
class DataJsonConvertor extends AbstractConvertor
{
protected $type;
public function __construct($path, $type)
{
parent::__construct(Storage::disk('local'), $path);
$this->type = $type;
}
/**
* Convert given document to JSON file which contains the document's data.
*
* @throws \Exception
*/
public function execute()
{
// if ($this->type === 'pdf') {
// $this->convertToDocx();
// }
if ($this->type !== 'docx') {
$this->convertToDocx();
}
$json = $this->convertDocxToJson();
$this->storage->put("$this->directoryPath/document.json", json_encode($json));
$this->deleteOriginalDocument();
}
protected function convertDocxToJson()
{
$reader = new DocxReader($this->storage, $this->path);
return $reader->execute();
}
/**
* Convert document to DOCX format in order to extract data.
*
* @throws \Exception
*/
protected function convertToDocx()
{
$office = new Office();
$success = $office->run(
'docx',
$this->storage->path($this->path),
$this->storage->path($this->directoryPath)
);
if (! $success) {
throw new \Exception('Failed when converting from ' . $this->type . ' to DOCX for file: ' . $this->path);
}
$this->deleteOriginalDocument();
$this->setPath(str_replace($this->type, 'docx', $this->path));
$this->type = 'docx';
}
}