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.
58 lines
1.7 KiB
58 lines
1.7 KiB
<?php
|
|
|
|
namespace App\Ingest;
|
|
|
|
use App\Jobs\IngestDocuments;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class DocumentHandler
|
|
{
|
|
protected $id;
|
|
protected $document;
|
|
|
|
const DOCX_MIME_TYPE = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
|
const DOC_MIME_TYPE = 'application/msword';
|
|
const RTF_MIME_TYPE = 'text/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::ODT_MIME_TYPE => 'odt',
|
|
self::PDF_MIME_TYPE => 'pdf',
|
|
self::PDF_WPS_MIME_TYPE => 'pdf',
|
|
self::PLAIN_TEXT_TYPE => 'txt',
|
|
];
|
|
|
|
public function __construct($id, $document)
|
|
{
|
|
$this->id = $id;
|
|
$this->document = $document;
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
$storage = Storage::disk('local');
|
|
|
|
$file = request()->file('document');
|
|
$mimeType = $file->getClientMimeType();
|
|
|
|
if (!array_key_exists($mimeType, $this->supportedFiles)) {
|
|
throw new \Exception('File not supported.');
|
|
}
|
|
|
|
$type = $this->supportedFiles[$mimeType];
|
|
|
|
$path = $storage->putFileAs("contracts", $file, "$this->id.$type");
|
|
|
|
IngestDocuments::dispatch($path, $type);
|
|
}
|
|
}
|