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.
172 lines
4.6 KiB
172 lines
4.6 KiB
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Ingest\DocxConvertor;
|
|
use App\Ingest\DocxWriter;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Spatie\WebhookServer\WebhookCall;
|
|
|
|
class RecreateDocument implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected $id;
|
|
protected $data;
|
|
protected $storage;
|
|
protected $path;
|
|
protected $url;
|
|
protected $secret;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($id, $data)
|
|
{
|
|
$this->id = $id;
|
|
$this->data = $data;
|
|
$this->storage = Storage::disk('local');
|
|
|
|
$this->path = '';
|
|
|
|
$this->url = env('WEBHOOK_CORE_URL') . '/webhooks';
|
|
$this->secret = env('WEBHOOK_CORE_SECRET');
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
try {
|
|
$this->setupData();
|
|
|
|
$this->createDocx();
|
|
|
|
$this->convertToOriginalDocumentFormat();
|
|
|
|
$this->sendResponse('success');
|
|
} catch (\Exception $exception) {
|
|
\Illuminate\Support\Facades\Log::info('RecreateDocument@handle: ' . $exception->getMessage());
|
|
\Illuminate\Support\Facades\Log::info($exception->getTraceAsString());
|
|
|
|
$this->sendResponse('fail');
|
|
}
|
|
}
|
|
|
|
protected function setupData()
|
|
{
|
|
$text = $this->data['contents']['text'];
|
|
$textMapper = [];
|
|
|
|
foreach ($this->data['contents']['elements'] as $element) {
|
|
$textMapper[$element['hash']] =substr(
|
|
$text,
|
|
$element['range_start'],
|
|
$element['range_end'] - $element['range_start'] + 1
|
|
);
|
|
}
|
|
|
|
$this->data['elements'] = $this->updateText($this->data['elements'], $textMapper);
|
|
}
|
|
|
|
protected function updateText($elements, $textMapper)
|
|
{
|
|
foreach ($elements as $index => $element) {
|
|
if (array_key_exists('hash', $element)) {
|
|
$elements[$index]['text'] = $textMapper[$element['hash']];
|
|
}
|
|
|
|
if (
|
|
array_key_exists('text_object', $element) &&
|
|
array_key_exists('text', $element['text_object'])
|
|
) {
|
|
$elements[$index]['text_object']['text'] = $textMapper[$element['text_object']['hash']];
|
|
}
|
|
|
|
if (isset($elements[$index]['elements'])) {
|
|
$elements[$index]['elements'] = $this->updateText($elements[$index]['elements'], $textMapper);
|
|
}
|
|
}
|
|
|
|
return $elements;
|
|
}
|
|
|
|
protected function createDocx()
|
|
{
|
|
$path = 'contracts/' . $this->id . '-document.docx';
|
|
|
|
$writer = new DocxWriter($this->storage, $path);
|
|
|
|
$writer->execute($this->data);
|
|
|
|
$this->path = $path;
|
|
}
|
|
|
|
/**
|
|
* @throws \Exception
|
|
*/
|
|
protected function convertToOriginalDocumentFormat()
|
|
{
|
|
if ($this->data['document_format'] === 'docx') {
|
|
return;
|
|
}
|
|
|
|
$convertor = new DocxConvertor($this->storage, $this->path);
|
|
|
|
if ($this->data['document_format'] === 'pdf') {
|
|
$convertor->convertToPdfWithLibreOffice();
|
|
}
|
|
|
|
if ($this->data['document_format'] === 'odt') {
|
|
$convertor->convertToODT();
|
|
}
|
|
|
|
if ($this->data['document_format'] === 'rtf') {
|
|
$convertor->convertToRTF();
|
|
}
|
|
|
|
if ($this->data['document_format'] === 'doc') {
|
|
$convertor->convertToDOC();
|
|
}
|
|
|
|
if ($this->data['document_format'] === 'txt') {
|
|
$convertor->convertToTXT();
|
|
}
|
|
|
|
$this->path = $convertor->getPath();
|
|
}
|
|
|
|
protected function sendResponse($status)
|
|
{
|
|
try {
|
|
WebhookCall::create()
|
|
->url($this->url)
|
|
->payload(['data' => [
|
|
'id' => $this->id,
|
|
'content' => '',
|
|
'file_result_type' => 'document-recreated',
|
|
'document_format' => $this->data['document_format'],
|
|
'status' => $status,
|
|
]])
|
|
->useSecret($this->secret)
|
|
->dispatch();
|
|
|
|
return true;
|
|
} catch (\Exception $exception) {
|
|
Log::error('RecreateDocument@sendDocument: ' . $exception->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|