Skip to content

Commit

Permalink
refactor pdf scan
Browse files Browse the repository at this point in the history
  • Loading branch information
kingjia90 committed Apr 17, 2024
1 parent be56713 commit c1d4c2a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
3 changes: 3 additions & 0 deletions lib/Messenger/Handler/AssetUpdateTasksHandler.php
Expand Up @@ -64,6 +64,9 @@ private function saveAsset(Asset $asset): void
private function processDocument(Asset\Document $asset): void
{
if ($asset->isPdfScanningEnabled() && $asset->getMimeType() === 'application/pdf' && !$asset->getScanStatus()) {
$asset->setCustomSetting($asset::CUSTOM_SETTING_PDF_SCAN_STATUS, PdfScanStatus::IN_PROGRESS->value);
$this->saveAsset($asset);

if ($asset->checkIfPdfContainsJS()) {
$asset->setCustomSetting($asset::CUSTOM_SETTING_PDF_SCAN_STATUS, PdfScanStatus::SAFE->value);
$note = 'safe';
Expand Down
7 changes: 7 additions & 0 deletions models/Asset.php
Expand Up @@ -17,6 +17,7 @@

use Doctrine\DBAL\Exception\DeadlockException;
use Exception;
use Pimcore\Model\Asset\Enum\PdfScanStatus;
use function in_array;
use function is_array;
use League\Flysystem\FilesystemException;
Expand Down Expand Up @@ -498,6 +499,12 @@ public function save(array $parameters = []): static

$parameters['isUpdate'] = $isUpdate; // need for $this->update() for certain types (image, video, document)

if ($this->getDataChanged()) {
if ($this->getType() === 'document') {
$this->setCustomSetting($this::CUSTOM_SETTING_PDF_SCAN_STATUS, null);

Check failure on line 504 in models/Asset.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, lowest, false)

Access to undefined constant Pimcore\Model\Asset::CUSTOM_SETTING_PDF_SCAN_STATUS.

Check failure on line 504 in models/Asset.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, highest, false)

Access to undefined constant Pimcore\Model\Asset::CUSTOM_SETTING_PDF_SCAN_STATUS.

Check failure on line 504 in models/Asset.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, lowest, false)

Access to undefined constant Pimcore\Model\Asset::CUSTOM_SETTING_PDF_SCAN_STATUS.

Check failure on line 504 in models/Asset.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, false)

Access to undefined constant Pimcore\Model\Asset::CUSTOM_SETTING_PDF_SCAN_STATUS.

Check failure on line 504 in models/Asset.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, true, 6.4.x-dev)

Access to undefined constant Pimcore\Model\Asset::CUSTOM_SETTING_PDF_SCAN_STATUS.

Check failure on line 504 in models/Asset.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.1, lowest, false)

Access to undefined constant Pimcore\Model\Asset::CUSTOM_SETTING_PDF_SCAN_STATUS.

Check failure on line 504 in models/Asset.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.1, highest, false)

Access to undefined constant Pimcore\Model\Asset::CUSTOM_SETTING_PDF_SCAN_STATUS.
}
}

// we wrap the save actions in a loop here, to restart the database transactions in the case it fails
// if a transaction fails it gets restarted $maxRetries times, then the exception is thrown out
// especially useful to avoid problems with deadlocks in multi-threaded environments (forked workers, ...)
Expand Down
5 changes: 0 additions & 5 deletions models/Asset/Document.php
Expand Up @@ -167,11 +167,6 @@ public function getText(int $page = null): ?string

public function checkIfPdfContainsJS(): bool
{
$this->setCustomSetting(
self::CUSTOM_SETTING_PDF_SCAN_STATUS,
Model\Asset\Enum\PdfScanStatus::IN_PROGRESS->value
);

$chunkSize = 1024;
$filePointer = $this->getStream();

Expand Down

0 comments on commit c1d4c2a

Please sign in to comment.