Check PhantomJS response is valid PDF

This commit is contained in:
David Bomba 2021-01-13 10:25:33 +11:00
parent 43626c92a2
commit b2b464fb9e
3 changed files with 39 additions and 2 deletions

View File

@ -34,6 +34,7 @@ class SystemLog extends Model
const CATEGORY_GATEWAY_RESPONSE = 1;
const CATEGORY_MAIL = 2;
const CATEGORY_WEBHOOK = 3;
const CATEGORY_PDF = 3;
/* Event IDs*/
const EVENT_PAYMENT_RECONCILIATION_FAILURE = 10;
@ -47,7 +48,8 @@ class SystemLog extends Model
const EVENT_MAIL_RETRY_QUEUE = 31; //we use this to queue emails that are spooled and not sent due to the email queue quota being exceeded.
const EVENT_WEBHOOK_RESPONSE = 40;
const EVENT_PDF_RESPONSE = 50;
/*Type IDs*/
const TYPE_PAYPAL = 300;
const TYPE_STRIPE = 301;
@ -60,6 +62,8 @@ class SystemLog extends Model
const TYPE_UPSTREAM_FAILURE = 401;
const TYPE_WEBHOOK_RESPONSE = 500;
const TYPE_PDF_FAILURE = 600;
const TYPE_PDF_SUCCESS = 601;
protected $fillable = [
'client_id',

View File

@ -11,10 +11,12 @@
namespace App\Utils\PhantomJS;
use App\Jobs\Util\SystemLogger;
use App\Models\CreditInvitation;
use App\Models\Design;
use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation;
use App\Models\SystemLog;
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
@ -77,7 +79,7 @@ class Phantom
$phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/?request=%7Burl:%22{$url}%22,renderType:%22pdf%22%7D";
$pdf = CurlUtils::get($phantom_url);
// Storage::makeDirectory($path, 0775);
$this->checkMime($pdf, $invitation, $entity);
$instance = Storage::disk(config('filesystems.default'))->put($file_path, $pdf);
@ -101,6 +103,36 @@ class Phantom
return $response;
}
/* Check if the returning PDF is valid. */
private function checkMime($pdf, $invitation, $entity)
{
$finfo = new \finfo(FILEINFO_MIME);
if($finfo->buffer($pdf) != 'application/pdf; charset=binary')
{
SystemLogger::dispatch(
$pdf,
SystemLog::CATEGORY_PDF,
SystemLog::EVENT_PDF_RESPONSE,
SystemLog::TYPE_PDF_FAILURE,
$invitation->contact->client
);
}
else {
SystemLogger::dispatch(
"Entity PDF generated sucessfully => " . $invitation->{$entity}->number,
SystemLog::CATEGORY_PDF,
SystemLog::EVENT_PDF_RESPONSE,
SystemLog::TYPE_PDF_SUCCESS,
$invitation->contact->client
);
}
}
public function displayInvitation(string $entity, string $invitation_key)
{
$key = $entity.'_id';

View File

@ -35,6 +35,7 @@ class PhantomJsTest extends TestCase
public function testInValidPdfMime()
{
$pdf = file_get_contents(base_path('/tests/Unit/Phantom/invalid.pdf'));
$finfo = new \finfo(FILEINFO_MIME);