]> _ Git - cubist_util.git/commitdiff
wait #5925 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 10 May 2023 10:22:47 +0000 (12:22 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 10 May 2023 10:22:47 +0000 (12:22 +0200)
src/CommandLine/Poppler.php
src/Files/Files.php

index 5828dd38a594ed309229a70ad2f89e1ff960bacc..9ac8dadd1eff1dc9e5f94e6866f9a613d21adb63 100644 (file)
@@ -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;
index dd93bee4bd6b0579ca26c03814011b3216f5b4a2..288ca4e016ba32dcd43fb152c957f3a475c4701f 100644 (file)
@@ -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