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.
169 lines
4.8 KiB
169 lines
4.8 KiB
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Spatie\WebhookServer\WebhookCall;
|
|
|
|
class SendToCore implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable;
|
|
|
|
protected $url;
|
|
protected $secret;
|
|
protected $directoryPath;
|
|
protected $fileResultType;
|
|
protected $documentFormat;
|
|
protected $id;
|
|
protected $hasFailed;
|
|
|
|
/**
|
|
* @var \Illuminate\Contracts\Filesystem\Filesystem
|
|
*/
|
|
private $storage;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @param $id
|
|
* @param string $fileResultType
|
|
* @param string $documentFormat
|
|
* @param null $directoryPath
|
|
* @param bool $hasFailed
|
|
*/
|
|
public function __construct($id, string $fileResultType, string $documentFormat, $directoryPath = null, bool $hasFailed = false)
|
|
{
|
|
$this->url = env('WEBHOOK_CORE_URL') . '/webhooks';
|
|
$this->secret = env('WEBHOOK_CORE_SECRET');
|
|
|
|
$this->id = $id;
|
|
$this->fileResultType = $fileResultType;
|
|
$this->documentFormat = $documentFormat;
|
|
$this->directoryPath = $directoryPath;
|
|
$this->hasFailed = $hasFailed;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
|
*/
|
|
public function handle()
|
|
{
|
|
$content = [];
|
|
|
|
// Directory exists, send content.
|
|
if ($this->directoryPath && ! $this->hasFailed) {
|
|
$this->storage = Storage::disk('local');
|
|
|
|
// @TODO Check if the file exists multiple times?
|
|
if ( ! $this->storage->exists($this->directoryPath)) {
|
|
throw new \Exception('File does not exist yet.');
|
|
}
|
|
|
|
$content = $this->getContent();
|
|
}
|
|
|
|
$sent = $this->sendTheData($content);
|
|
|
|
// if ($this->directoryPath && $sent) {
|
|
if ($this->directoryPath) {
|
|
if ( ! $this->storage) {
|
|
$this->storage = Storage::disk('local');
|
|
}
|
|
|
|
$this->storage->deleteDirectory($this->directoryPath);
|
|
}
|
|
}
|
|
|
|
public function failed()
|
|
{
|
|
if ($this->directoryPath) {
|
|
if ( ! $this->storage) {
|
|
$this->storage = Storage::disk('local');
|
|
}
|
|
|
|
$this->storage->delete($this->directoryPath);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send the data to the core through webhooks
|
|
*
|
|
* @param array$content
|
|
* @return bool
|
|
*/
|
|
protected function sendTheData(array $content)
|
|
{
|
|
try {
|
|
WebhookCall::create()
|
|
->url($this->url)
|
|
->payload(['data' => [
|
|
'id' => $this->id,
|
|
'content' => $content,
|
|
'file_result_type' => $this->fileResultType,
|
|
'document_format' => $this->documentFormat,
|
|
'status' => count($content) > 0 ? 'success' : 'fail',
|
|
]])
|
|
->useSecret($this->secret)
|
|
->dispatch();
|
|
|
|
return true;
|
|
} catch (\Exception $exception) {
|
|
Log::error('SendToCore@sendTheData: ' . $exception->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
protected function getContent()
|
|
{
|
|
$extension = $this->fileResultType === 'md' ? 'md' : 'json';
|
|
$filePath = "$this->directoryPath/document.$extension";
|
|
|
|
$document = $this->storage->get($filePath);
|
|
$document = $this->encodeContent($document);
|
|
|
|
$images = [];
|
|
|
|
if ($extension === 'md') {
|
|
$allFiles = $this->storage->allFiles($this->directoryPath);
|
|
|
|
foreach ($allFiles as $file) {
|
|
// @TODO We are using this check in the 'PDFConvertor' file, refactor and improve.
|
|
if (in_array(pathinfo($file, PATHINFO_EXTENSION), ['jpg', 'png'])) {
|
|
$name = pathinfo($file, PATHINFO_FILENAME);
|
|
$type = pathinfo($file, PATHINFO_EXTENSION);
|
|
|
|
$images[] = [
|
|
'name' => $name,
|
|
'type' => $type,
|
|
'contents' => 'data:image/' . $type . ';base64,' . base64_encode($this->storage->get($file)),
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
return [
|
|
'document' => $document,
|
|
'images' => $images,
|
|
];
|
|
}
|
|
|
|
protected function encodeContent($content)
|
|
{
|
|
$encoding = mb_detect_encoding($content, 'UTF-8, ISO-8859-1, WINDOWS-1252, WINDOWS-1251', true);
|
|
|
|
if ($encoding != 'UTF-8') {
|
|
$content = iconv($encoding, 'UTF-8//IGNORE', $content);
|
|
}
|
|
|
|
return $content;
|
|
}
|
|
}
|