From: Vincent Vanwaelscappel Date: Wed, 10 May 2023 10:22:47 +0000 (+0200) Subject: wait #5925 @1 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=dd6eca49abff868674235a58e36aabf414fbec17;p=cubist_util.git wait #5925 @1 --- diff --git a/src/CommandLine/Poppler.php b/src/CommandLine/Poppler.php index 5828dd3..9ac8dad 100644 --- a/src/CommandLine/Poppler.php +++ b/src/CommandLine/Poppler.php @@ -9,6 +9,8 @@ use Cubist\Util\Math; class Poppler { + const RESOLUTION_FACTOR = 72; + public static function extractArea($file, $page, $rect, $to = null, $options = array(), $cache = null) { if (null === $to && null === $cache) { @@ -22,7 +24,7 @@ class Poppler 'internalformat' => 'png'); $options = array_merge($defaultOptions, $options); - $options['factor'] = $options['resolution'] / 72; + $options['factor'] = $options['resolution'] / self::RESOLUTION_FACTOR; if ($options['format'] === 'jpeg') { $extension = 'jpg'; @@ -42,6 +44,8 @@ class Poppler $cacheFileWithoutExt = $cache . $hash; $cacheFile = $cacheFileWithoutExt . '.' . $extension; + $lockFile = $cacheFile . '.lock'; + if (null === $to) { $res = $cacheFile; $dest = $cacheFileWithoutExt; @@ -57,12 +61,9 @@ class Poppler } } -// error_log('####--------------####'); -// error_log('___ Resolution: ' . $options['resolution']); -// error_log('___ Factor: ' . $factor); -// error_log('extractArea: '. $f); -// error_log("CACHE MISS: $cacheFile"); - + if (isset($lockFile)) { + Files::getLock($lockFile); + } $pdftoppm = new CommandLine('pdftoppm'); $pdftoppm->setArg('r', $options['resolution']); @@ -88,6 +89,10 @@ class Poppler $pdftoppm->setArg(null, $dest); $pdftoppm->execute(); + if (isset($lockFile)) { + Files::releaseLock($lockFile); + } + if (null !== $cacheFile) { if (null === $to) { return $cacheFile; diff --git a/src/Files/Files.php b/src/Files/Files.php index dd93bee..288ca4e 100644 --- a/src/Files/Files.php +++ b/src/Files/Files.php @@ -409,4 +409,20 @@ class Files } } + public static function getLock($lockFile, $maxAge = 300, $touch = true) + { + while (file_exists($lockFile) && filemtime($lockFile) > (time() - $maxAge)) { + sleep(5); + } + if ($touch) { + touch($lockFile); + } + } + + public static function releaseLock($lockFile) + { + if (file_exists($lockFile)) { + unlink($lockFile); + } + } } \ No newline at end of file