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.
 
 
 
 

71 lines
2.1 KiB

<?php
namespace App\Ingest;
use App\Jobs\IngestDocuments;
use Illuminate\Support\Facades\Storage;
class DocumentHandler
{
protected $id;
protected $fileResultType;
protected $mimeType;
protected $document;
protected $fromRequest;
const DOCX_MIME_TYPE = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
const DOC_MIME_TYPE = 'application/msword';
const RTF_MIME_TYPE = 'text/rtf';
const APPLICATION_RTF_MIME_TYPE = 'application/rtf';
const ODT_MIME_TYPE = 'application/vnd.oasis.opendocument.text';
const PDF_MIME_TYPE = 'application/pdf';
const PDF_WPS_MIME_TYPE = 'application/wps-office.pdf';
const DOCXOLD_MIME_TYPE = 'application/octet-stream';
const DOCX_WPS_TYPE = 'application/wps-office.docx';
const PLAIN_TEXT_TYPE = 'text/plain';
protected $supportedFiles = [
self::DOCX_MIME_TYPE => 'docx',
self::DOCXOLD_MIME_TYPE => 'docx',
self::DOCX_WPS_TYPE => 'docx',
self::DOC_MIME_TYPE => 'doc',
self::RTF_MIME_TYPE => 'rtf',
self::APPLICATION_RTF_MIME_TYPE => 'rtf',
self::ODT_MIME_TYPE => 'odt',
self::PDF_MIME_TYPE => 'pdf',
self::PDF_WPS_MIME_TYPE => 'pdf',
self::PLAIN_TEXT_TYPE => 'txt',
];
public function __construct($id, $fileResultType, $mimeType, $document, $fromRequest = true)
{
$this->id = $id;
$this->fileResultType = $fileResultType;
$this->mimeType = $mimeType;
$this->document = $document;
$this->fromRequest = $fromRequest;
}
public function handle()
{
$storage = Storage::disk('local');
if (!array_key_exists($this->mimeType, $this->supportedFiles)) {
throw new \Exception('File not supported.');
}
$type = $this->supportedFiles[$this->mimeType];
$id = str_replace(' ', '_', $this->id);
$path = $storage->putFileAs("contracts/$id", $this->document, "document.$type");
IngestDocuments::dispatch(
$this->id,
$this->fileResultType,
$path,
$type,
$this->fromRequest
);
}
}