]> _ Git - fluidbook_tools.git/commitdiff
wip #4666 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 8 Sep 2021 14:14:45 +0000 (16:14 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 8 Sep 2021 14:14:45 +0000 (16:14 +0200)
src/FluidbookTools.php
src/Jobs/ProcessFile.php [new file with mode: 0644]
src/Jobs/ProcessPage.php
src/PDF/Document.php

index 85ad3892296eb09bc4710ca1cb7f945326af76ec..7eb64133589c2b2a8d4bc5a1d8893433532b5a7a 100644 (file)
@@ -8,18 +8,29 @@ class FluidbookTools
      * @return string
      */
 
-    public static function resources_path($path)
+    public static function resource_path($path)
     {
-        return __DIR__ . '/' . self::_cleanPath($path);
+        return __DIR__ . '/../resources/' . self::_cleanPath($path);
     }
 
     /**
      * @param $path string
      * @return string
      */
-    public static function tools_path($path)
+    public static function tools_path($path, $chmod = false)
     {
-        return resource_path('tools/' . self::_cleanPath($path));
+        $res = self::resource_path('tools/' . self::_cleanPath($path));
+        if ($chmod) {
+            self::chmodExec($res);
+        }
+        return $res;
+    }
+
+    public static function chmodExec($path)
+    {
+        if (is_file($path)) {
+            chmod($path, 0755);
+        }
     }
 
     /**
diff --git a/src/Jobs/ProcessFile.php b/src/Jobs/ProcessFile.php
new file mode 100644 (file)
index 0000000..7a498eb
--- /dev/null
@@ -0,0 +1,287 @@
+<?php
+
+namespace Fluidbook\Tools\Jobs;
+
+use Fluidbook\Tools\PDF\PDFTools;
+use Fluidbook\Tools\SVG\SVGTools;
+
+class ProcessFile
+{
+    protected $format = 'jpg';
+    /**
+     * @var int
+     */
+    protected $resolution = 150;
+    /**
+     * @var bool
+     */
+    protected $withGraphics = true;
+    /**
+     * @var bool
+     */
+    protected $withTexts = true;
+    /**
+     * @var string
+     */
+    protected $version = 'html';
+    /**
+     * @var int
+     */
+    protected $page = 0;
+    /**
+     * @var string
+     */
+    protected $out;
+    /**
+     * @var ProcessPage
+     */
+    protected $job;
+
+    public function __construct($format = 'jpg', $resolution = 150, $withGraphics = true, $withTexts = true, $version = 'html')
+    {
+        $this->format = $format;
+        $this->resolution = $resolution;
+        $this->withGraphics = $withGraphics;
+        $this->withTexts = $withTexts;
+        $this->version = $version;
+    }
+
+    /**
+     * @param string $format
+     */
+    public function setFormat(string $format)
+    {
+        $this->format = $format;
+    }
+
+    /**
+     * @return string
+     */
+    public function getFormat(): string
+    {
+        if ($this->format === 'jpeg') {
+            return 'jpg';
+        }
+        return $this->format;
+    }
+
+    /**
+     * @param int $resolution
+     */
+    public function setResolution(int $resolution)
+    {
+        $this->resolution = $resolution;
+    }
+
+    /**
+     * @return int
+     */
+    public function getResolution(): int
+    {
+        return $this->resolution;
+    }
+
+    /**
+     * @return bool
+     */
+    public function isWithGraphics(): bool
+    {
+        return $this->withGraphics;
+    }
+
+    /**
+     * @param bool $withGraphics
+     */
+    public function setWithGraphics(bool $withGraphics)
+    {
+        $this->withGraphics = $withGraphics;
+    }
+
+    /**
+     * @return bool
+     */
+    public function isWithTexts(): bool
+    {
+        return $this->withTexts;
+    }
+
+    /**
+     * @param bool $withTexts
+     */
+    public function setWithTexts(bool $withTexts)
+    {
+        $this->withTexts = $withTexts;
+    }
+
+    /**
+     * @return int
+     */
+    public function getPage(): int
+    {
+        return $this->job->getPage();
+    }
+
+    /**
+     * @return mixed
+     */
+    public function getOut()
+    {
+        return $this->job->getOut();
+    }
+
+    /**
+     * @param mixed $out
+     */
+    public function setOut($out)
+    {
+        $this->out = $out;
+    }
+
+    /**
+     * @return string
+     */
+    public function getVersion(): string
+    {
+        if ($this->getFormat() === 'svg') {
+            return 'html';
+        } else if ($this->getFormat() === 'swf') {
+            return '';
+        }
+
+        return $this->version;
+    }
+
+    /**
+     * @param string $version
+     */
+    public function setVersion(string $version)
+    {
+        $this->version = $version;
+    }
+
+    /**
+     * @return ProcessPage
+     */
+    public function getJob(): ProcessPage
+    {
+        return $this->job;
+    }
+
+    /**
+     * @param ProcessPage $job
+     */
+    public function setJob(ProcessPage $job)
+    {
+        $this->job = $job;
+    }
+
+    public function getPath($force = false)
+    {
+        $dir = rtrim($this->getOut() . $this->getVersion(), '/') . '/';
+        $minsize = 1;
+        $res = '';
+        if ($this->getFormat() === 'svg') {
+            $prefix = $this->isWithGraphics() ? 'f' : 't';
+            $res = $dir . $prefix . 'o' . $this->getPage();
+            if ($this->isWithGraphics()) {
+                $res .= '-' . $this->getResolution();
+            }
+            $res .= '.svg';
+            $reffile = $this->makeSVGFile();
+            $minsize = 100;
+        } else if (in_array($this->getFormat(), ['png', 'jpg'])) {
+            $prefix = $this->isWithTexts() ? 't' : 'h';
+            if ($this->getResolution() === 'thumb') {
+                $res = $dir . 'p' . $this->getPage() . '.' . $this->getFormat();
+            } else {
+                $res = $dir . $prefix . $this->getPage() . '-' . $this->getResolution() . '.' . $this->getFormat();
+                $alt = $dir . $prefix . $this->getResolution() . '-' . $this->getPage() . '.' . $this->getFormat();
+            }
+        } else if ($this->getFormat() === 'swf') {
+            $res = $dir . 'p' . $this->getPage() . '.' . $this->getFormat();
+        }
+
+        $do = false;
+        if (!file_exists($res)) {
+            if (isset($alt) && file_exists($alt) && filesize($alt) > $minsize) {
+                rename($alt, $res);
+                $do = false;
+            } else {
+                $do = true;
+            }
+        } else if (filesize($res) < $minsize) {
+            $do = true;
+        } else if (isset($reffile) && filemtime($res) < filemtime($reffile)) {
+            $do = true;
+        } else if ($this->getFormat() === 'svg') {
+            $t = filemtime($res);
+            $do = $t < 1603181003 && $t > 1602662603;
+        }
+
+        if ($do || $force) {
+            $this->makeFile($res);
+        }
+        return $res;
+    }
+
+    public function makeFile($file)
+    {
+        $lock = $file . '.lock';
+        if (file_exists($lock) && filemtime($lock) > time() - 300) {
+            sleep(10);
+            return $this->getPath();
+        }
+        touch($lock);
+        if ($this->getFormat() === 'svg') {
+            if ($this->isWithGraphics()) {
+                $this->makeOptimizedSVGFile($file);
+            } else {
+                $this->makeTextSVGFile($file);
+            }
+        } else if (in_array($this->getFormat(), ['png', 'jpg'])) {
+            if ($this->getResolution() === 'thumb') {
+                PDFTools::makeMiniShot($this->getSplittedPDFPage(), $file, 1, $this->getFormat());
+            } else {
+                $rr = $this->getVersion() === 'html' ? $this->getJob()->getResolutionRatio() : $this->getJob()->getMobileFirstRatio();
+                PDFTools::makeShotPNM($this->getSplittedPDFPage(), $file, 1, '', $this->getResolution() * $rr, 85, 4, $this->isWithTexts(), null, null, $this->getFormat());
+            }
+        } else if ($this->getFormat() === 'swf') {
+            PDFTools::makeSWF($this->getSplittedPDFPage(), $file, 1, $this->getResolution(), 80);
+        }
+        unlink($lock);
+    }
+
+    public function makeSVGFile($force = false)
+    {
+        $svgFile = $this->out . '/html/fp' . $this->getPage() . '.svg';
+        if (!$force && file_exists($svgFile) && filesize($svgFile) > 0) {
+            return $svgFile;
+        }
+        PDFTools::makeBaseSVGFile($this->getSplittedPDFPage(), $svgFile, 1);
+        return $svgFile;
+    }
+
+    public function makeTextSVGFile($out)
+    {
+        $in = $this->makeSVGFile();
+        $inter = str_replace('to', 'tp', $out);
+        PDFTools::makeTextSVGFile($in, $inter);
+        SVGTools::optimizeSVG($inter, $out);
+    }
+
+    public function makeOptimizedSVGFile($out)
+    {
+        $in = $this->makeSVGFile();
+        SVGTools::optimizeSVGImages($in, $out, $this->getResolution());
+        SVGTools::optimizeSVG($out, $out);
+    }
+
+    protected function getSplittedPDFPage()
+    {
+        $res = $this->out . 'pdf/p' . $this->getPage() . '.pdf';
+        if (!file_exists($res)) {
+            $this->getJob()->splitDoc();
+        }
+        return $res;
+    }
+}
index 85c384d5d824ad6e47dc6152de30947f3ba8f1a9..601840f9952ce2c0f6798acde85b56e69c047cb6 100644 (file)
@@ -32,6 +32,11 @@ class ProcessPage implements ShouldQueue
      */
     protected $out;
 
+    /**
+     * @var ProcessFile[]
+     */
+    protected $files = [];
+
     /**
      * @param $document Document
      * @param $page integer
@@ -45,109 +50,42 @@ class ProcessPage implements ShouldQueue
         $this->files = $files;
     }
 
+    /**
+     * @return int
+     */
+    public function getPage(): int
+    {
+        return $this->page;
+    }
+
+    /**
+     * @return string
+     */
+    public function getOut(): string
+    {
+        return $this->out;
+    }
+
     public function handle()
     {
         start_measure('Process page ' . $this->page);
         foreach ($this->files as $file) {
-            $this->getFile($this->page, $file['format'], $file['resolution'], $file['withText'], $file['withGraphics'], 'html', false);
+            $this->getFile($this->page, $file, false);
         }
         stop_measure('Process page ' . $this->page);
     }
 
 
-    public function getFile($page, $format = 'jpg', $resolution = 150, $withText = true, $withGraphics = true, $version = 'html', $force = false)
-    {
-        if ($format === 'jpeg') {
-            $format = 'jpg';
-        }
-        if ($format === 'svg') {
-            $version = 'html';
-        } else if ($format === 'swf') {
-            $version = '';
-        }
-
-        if ($resolution === 'thumb') {
-            $withGraphics = true;
-            $withText = true;
-            $version = '';
-        }
-
-        $dir = rtrim($this->out . $version, '/') . '/';
-        $minsize = 1;
-        $file = '';
-        if ($format === 'svg') {
-            $prefix = $withGraphics ? 'f' : 't';
-            $file = $dir . $prefix . 'o' . $page;
-            if ($withGraphics) {
-                $file .= '-' . $resolution;
-            }
-            $file .= '.svg';
-            $reffile = $this->makeSVGFile($page, $force);
-            $minsize = 100;
-        } else if ($format === 'png' || $format === 'jpg') {
-            $prefix = $withText ? 't' : 'h';
-            if ($resolution === 'thumb') {
-                $file = $dir . 'p' . $page . '.' . $format;
-            } else {
-                $file = $dir . $prefix . $page . '-' . $resolution . '.' . $format;
-                $alt = $dir . $prefix . $resolution . '-' . $page . '.' . $format;
-            }
-        } else if ($format === 'swf') {
-            $file = $dir . 'p' . $page . '.' . $format;
-        }
-
-        $do = false;
-        if (!file_exists($file)) {
-            if (isset($alt) && file_exists($alt) && filesize($alt) > $minsize) {
-                rename($alt, $file);
-                $do = false;
-            } else {
-                $do = true;
-            }
-        } else if (filesize($file) < $minsize) {
-            $do = true;
-        } else if (isset($reffile) && filemtime($file) < filemtime($reffile)) {
-            $do = true;
-        } else if ($format === 'svg') {
-            $t = filemtime($file);
-            $do = $t < 1603181003 && $t > 1602662603;
-        }
-
-        if ($do || $force) {
-            $this->makeFile($file, $page, $format, $resolution, $withText, $withGraphics, $version);
-        }
-
-        return $file;
-    }
-
-    public function makeFile($file, $page, $format = 'jpg', $resolution = 150, $withText = true, $withGraphics = true, $version = 'html')
+    /**
+     * @param $page
+     * @param $file ProcessFile
+     * @param false $force
+     * @return string
+     */
+    public function getFile($page, $file, $force = false)
     {
-        $lock = $file . '.lock';
-        if (file_exists($lock) && filemtime($lock) > time() - 300) {
-            sleep(10);
-            return $this->getFile($page, $format, $resolution, $withText, $withGraphics, $version);
-        }
-        touch($lock);
-        if ($format === 'jpeg') {
-            $format = 'jpg';
-        }
-        if ($format === 'svg') {
-            if ($withGraphics) {
-                $this->makeOptimizedSVGFile($page, $resolution, $file);
-            } else {
-                $this->makeTextSVGFile($page, $file);
-            }
-        } else if ($format === 'png' || $format === 'jpg') {
-            if ($resolution === 'thumb') {
-                PDFTools::makeMiniShot($this->getSplittedPDFPage($page), $file, 1, $format);
-            } else {
-                $rr = $version === 'html' ? $this->getResolutionRatio() : $this->getMobileFirstRatio();
-                PDFTools::makeShotPNM($this->getSplittedPDFPage($page), $file, 1, '', $resolution * $rr, 85, 4, $withText, null, null, $format);
-            }
-        } else if ($format === 'swf') {
-            PDFTools::makeSWF($this->getSplittedPDFPage($page), $file, 1, $resolution, 80);
-        }
-        unlink($lock);
+        $file->setJob($this);
+        return $file->getPath($force);
     }
 
     public function getResolutionRatio()
@@ -160,40 +98,6 @@ class ProcessPage implements ShouldQueue
         return $this->document->getMobileFirstRatio();
     }
 
-    public function makeSVGFile($page, $force = false)
-    {
-        $svgFile = $this->out . '/html/fp' . $page . '.svg';
-        if (!$force && file_exists($svgFile) && filesize($svgFile) > 0) {
-            return $svgFile;
-        }
-        PDFTools::makeBaseSVGFile($this->getSplittedPDFPage($page), $svgFile, 1);
-        return $svgFile;
-    }
-
-    public function makeTextSVGFile($page, $out, $force = false)
-    {
-        $in = $this->makeSVGFile($page);
-        $inter = str_replace('to', 'tp', $out);
-        PDFTools::makeTextSVGFile($in, $inter);
-        SVGTools::optimizeSVG($inter, $out);
-    }
-
-    public function makeOptimizedSVGFile($page, $resolution, $out)
-    {
-        $in = $this->makeSVGFile($page);
-        SVGTools::optimizeSVGImages($in, $out, $resolution);
-        SVGTools::optimizeSVG($out, $out);
-    }
-
-    protected function getSplittedPDFPage($page)
-    {
-        $res = $this->out . 'pdf/p' . $page . '.pdf';
-        if (!file_exists($res)) {
-            $this->splitDoc();
-        }
-        return $res;
-    }
-
     public function splitDoc()
     {
         start_measure('Split PDF');
index 5224a4680d225b6ded2f2f6e56c0d262266bf9f8..d9f99bf75d5f646962e0a9cde6844863ca0507b0 100644 (file)
@@ -47,11 +47,11 @@ class Document
 
     public function getInfos($force = false)
     {
-        if (!$force && $this->pages) {
+        if (!$force && $this->pages > 0) {
             return;
         }
 
-        $fwstk = new CommandLine(FluidbookTools::tools_path('fwstk.sh'));
+        $fwstk = new CommandLine(FluidbookTools::tools_path('fwstk.sh',true));
         $fwstk->setArg('--input ' . $this->getPDFInput());
         $fwstk->setArg('--infos');
         $fwstk->execute();
@@ -99,9 +99,12 @@ class Document
      */
     public function processPages($dest, $files, $sync = false)
     {
+        start_measure('Process pages (doc)');
+
         for ($i = 1; $i <= $this->getPages(); $i++) {
             $this->processPage($i, $dest, $files, $sync);
         }
+        stop_measure('Process pages (doc)');
     }
 
     /**
@@ -110,8 +113,10 @@ class Document
      */
     public function processPage($page, $dest, $files, $sync = false)
     {
+        start_measure('Process page ' . $page . ' (doc)');
         $dispatchFunction = $sync ? 'dispatchSync' : 'dispatch';
         ProcessPage::$dispatchFunction($this, $page, $dest, $files);
+        stop_measure('Process page ' . $page . ' (doc)');
     }
 
     public function getResolutionRatio()