]> _ Git - fluidbook-toolbox.git/commitdiff
wait #5512 @5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 6 Oct 2022 16:52:07 +0000 (18:52 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 6 Oct 2022 16:52:07 +0000 (18:52 +0200)
app/Console/Commands/FluidbookPreprocessImages.php [new file with mode: 0644]
app/Http/Controllers/Admin/Operations/FluidbookPublication/CompositionOperation.php
app/Jobs/FluidbookCompiler.php
app/Jobs/FluidbookDocumentFileProcess.php
app/Jobs/FluidbookDocumentUpload.php
app/Jobs/FluidbookImagesPreprocess.php [new file with mode: 0644]
app/Models/FluidbookDocument.php
app/Models/FluidbookPublication.php
app/Util/FluidbookFarm.php

diff --git a/app/Console/Commands/FluidbookPreprocessImages.php b/app/Console/Commands/FluidbookPreprocessImages.php
new file mode 100644 (file)
index 0000000..45287da
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Jobs\FluidbookCompiler;
+use App\Jobs\FluidbookImagesPreprocess;
+use App\Models\FluidbookPublication;
+use Cubist\Backpack\Console\Commands\CubistCommand;
+use Cubist\Util\PHP;
+
+class FluidbookPreprocessImages extends CubistCommand
+{
+    protected $signature = 'fluidbook:preprocess {id}';
+    protected $description = 'Compile a fluidbook';
+
+    /**
+     * @throws \Exception
+     */
+    public function handle()
+    {
+        PHP::neverStop(true);
+        FluidbookImagesPreprocess::dispatchSync($this->argument('id'));
+    }
+}
index e8d55b9b52842a556a44d0dd61c17804737d11ff..facf3807ddedf28094968d01180d2ad9fe81dd80 100644 (file)
@@ -3,7 +3,9 @@
 namespace App\Http\Controllers\Admin\Operations\FluidbookPublication;
 
 use App\Jobs\FluidbookDocumentUpload;
+use App\Jobs\FluidbookImagesPreprocess;
 use App\Models\FluidbookDocument;
+use App\Models\FluidbookPublication;
 use Cubist\Backpack\Http\Controllers\Base\XSendFileController;
 use Cubist\Util\Files\Files;
 use Cubist\Util\Str;
@@ -14,7 +16,6 @@ trait CompositionOperation
 {
     protected function setupCompositionRoutes($segment, $routeName, $controller)
     {
-        Route::match(['get'], $segment . '/{id}/composition', $controller . '@getComposition');
         Route::match(['get'], $segment . '/docs/{doc_id}/thumb_{doc_page}.jpg', $controller . '@getThumb');
         Route::match(['post'], $segment . '/uploaddocument', $controller . '@upload');
         Route::match(['get'], $segment . '/uploadProgress/{uploadID}', $controller . '@uploadProgress');
@@ -25,6 +26,7 @@ trait CompositionOperation
     {
     }
 
+
     protected function getThumb($doc_id, $doc_page)
     {
 
@@ -43,6 +45,7 @@ trait CompositionOperation
         return XSendFileController::sendfile($path);
     }
 
+
     protected function upload()
     {
         $uploadID = Str::random();
@@ -55,7 +58,7 @@ trait CompositionOperation
         $document->save();
         move_uploaded_file($file->getPathname(), Files::mkdir($document->path()) . 'original.pdf');
         FluidbookDocumentUpload::updateProgression($uploadID, __('Mise en file d\'attente du traitement du document'), 1.2);
-        FluidbookDocumentUpload::dispatch($uploadID, $document, backpack_user());
+        FluidbookDocumentUpload::dispatch($uploadID, $document, backpack_user(), request('fluidbook_id', null));
         return response()->json(['uploadID' => $uploadID, 'document' => $document->id]);
     }
 
index ad6beac19f0a4b7c2ee1f893ffe53ac90f78d08e..cdb26daf2a073d87d1638833ab6094c91f61c0ee 100644 (file)
@@ -287,6 +287,7 @@ class FluidbookCompiler extends Base implements CompilerInterface
     {
         parent::__construct();
 
+
         $this->setFluidbook($book);
         $this->setCommand($command);
 
@@ -1189,6 +1190,8 @@ class FluidbookCompiler extends Base implements CompilerInterface
 
     public function handle()
     {
+        $this->log('Preprocess images');
+        FluidbookImagesPreprocess::dispatchSync($this->book_id);
         $this->log('Start compile process');
 
         // Raw copy of some directories
@@ -2790,7 +2793,7 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
 
 
         if ($this->phonegap) {
-            $this->vdir->copy(WS_COMPILE_ASSETS . '/_html5/js/libs/phonegap/' . $this->phonegapVersion . '/cordova-' . $this->phonegap . '.js', 'data/cordova.js');
+            // $this->vdir->copy(WS_COMPILE_ASSETS . '/_html5/js/libs/phonegap/' . $this->phonegapVersion . '/cordova-' . $this->phonegap . '.js', 'data/cordova.js');
         }
         $this->vdir->copyDirectory($this->assets . '/js/libs/fluidbook/workers', 'js/libs/fluidbook/workers');
         $this->vdir->copyDirectory($this->assets . '/js/libs/stand', 'js/libs/stand');
index d1baea8b56d645c157c835f845740337844c4bd6..970032b840f89731930d0e978fe174079041e288 100644 (file)
@@ -39,9 +39,6 @@ class FluidbookDocumentFileProcess extends Base
      */
     public function isDone()
     {
-
-        $res = $this->document->hasFile($this->page, $this->format, $this->resolution, $this->withText, $this->withGraphics, $this->version);
-        //dd($res, $this->document->_getPath($this->page, $this->format, $this->resolution, $this->withText, $this->withGraphics, $this->version));
-        return $res;
+        return $this->document->hasFile($this->page, $this->format, $this->resolution, $this->withText, $this->withGraphics, $this->version);
     }
 }
index 33e7967a6a05030d702e5563c97c2eb8a8ad5ab8..c5e007bbd3ac2e77996ee02ba13376cd289dc47d 100644 (file)
@@ -15,17 +15,22 @@ class FluidbookDocumentUpload extends Base
      */
     protected $document;
 
+    /**
+     * @var int
+     */
+    protected $fluidbook_id;
 
     /**
      * @var User
      */
     protected $user;
 
-    public function __construct($uploadID, FluidbookDocument $document, $user)
+    public function __construct($uploadID, FluidbookDocument $document, $user, $fluidbook_id = null)
     {
         $this->onQueue('fluidbookprocess');
         $this->uploadID = $uploadID;
         $this->document = $document;
+        $this->fluidbook_id = $fluidbook_id;
         $this->setUser($user);
     }
 
diff --git a/app/Jobs/FluidbookImagesPreprocess.php b/app/Jobs/FluidbookImagesPreprocess.php
new file mode 100644 (file)
index 0000000..da19822
--- /dev/null
@@ -0,0 +1,104 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Models\FluidbookPublication;
+use Cubist\Util\ArrayUtil;
+
+class FluidbookImagesPreprocess extends Base
+{
+    protected $book_id;
+    /**
+     * @var FluidbookPublication
+     */
+    protected $book;
+
+    /**
+     * @var FluidbookDocumentFileProcess[]
+     */
+    protected $jobs = [];
+
+    public function __construct($book_id)
+    {
+        $this->book_id = $book_id;
+        parent::__construct();
+    }
+
+    public function handle()
+    {
+        $this->book = FluidbookPublication::find($this->book_id);
+        $settings = $this->book->getSettings();
+        $resolutions = FluidbookCompiler::getBookResolutions($this->book);
+        $rasterizePages = ArrayUtil::parseRange($settings->rasterizePages);
+        $vectorPages = array_diff(ArrayUtil::parseRange($settings->vectorPages), $rasterizePages);
+
+        switch ($settings->mobileVersion) {
+            case 'html5-desktop':
+                $globalBackgroundToGenerate = [true, false];
+                $svg = true;
+                break;
+            case 'html5-images':
+                $globalBackgroundToGenerate = [true];
+                $svg = false;
+                break;
+            default:
+                $globalBackgroundToGenerate = [false];
+                $svg = true;
+                break;
+        }
+
+        if ($settings->mobileNavigationType === 'mobilefirst') {
+            $imdir = 'mf';
+        } else {
+            $imdir = 'html';
+        }
+
+        for ($page = 1; $page <= $this->book->getPagesNumber(); $page++) {
+            $thisrasterize = in_array($page, $rasterizePages);
+            $thisimagesvg = !$thisrasterize && $svg;
+            $pageBackgroundsToGenerate = $thisrasterize ? [true] : $globalBackgroundToGenerate;
+            foreach ($resolutions as $r) {
+                foreach ($pageBackgroundsToGenerate as $withText) {
+                    $this->getFile($page, $settings->imageFormat, $r, $withText, true, $imdir);
+                }
+            }
+            if ($thisimagesvg) {
+                $this->getFile($page, 'svg', 150, true, in_array($page, $vectorPages), 'html');
+            }
+            $this->getFile($page, $settings->imageFormat, 'thumb');
+        }
+
+        while (true) {
+            if ($this->_checkJobs()) {
+                return;
+            }
+            usleep(0.25 * 1000000);
+        }
+    }
+
+    protected function _checkJobs()
+    {
+        $nbjobs = count($this->jobs);
+        $done = 0;
+        foreach ($this->jobs as $job) {
+            if ($job->isDone()) {
+                $done++;
+            }
+        }
+        echo $done . '/' . $nbjobs . "\n";
+        return $done === $nbjobs;
+    }
+
+    protected function getFile($page, $format = 'jpg', $resolution = 150, $withText = true, $withGraphics = true, $version = 'html')
+    {
+        $c = $this->book->composition[$page];
+        $doc = $this->book->getDocument($page);
+        $doc->clearFilesCache();
+        $job = new FluidbookDocumentFileProcess($doc, $c[1], $format, $resolution, $withText, $withGraphics, $version);
+        if ($job->isDone()) {
+            return;
+        }
+        dispatch($job);
+        $this->jobs[] = $job;
+    }
+}
index c6ed4615a820a80fa9e33c6f31c3e1db6988c9d6..3e855e5530287615db0e1e31d26472d12d74ce45 100644 (file)
@@ -83,6 +83,7 @@ class FluidbookDocument extends ToolboxModel
             // Texts
             ['svg', 150, true, false],
         ];
+
         $nbfiles = count($files);
         $delay = 0;
         for ($i = 1; $i <= $this->pages; $i++) {
@@ -232,7 +233,7 @@ class FluidbookDocument extends ToolboxModel
                     return false;
                 }
             }
-            Cache::put($cacheKey, $path);
+            Cache::tags('fluidbook_document_' . $this->id)->put($cacheKey, $path);
             return true;
         }
     }
@@ -521,10 +522,15 @@ class FluidbookDocument extends ToolboxModel
     public function clearAllFiles()
     {
         $cmd = 'cd ' . $this->path() . ';find . ! -name \'original.pdf\' ! -name \'.\' ! -name \'..\' -exec rm -rf {} +';
-        Cache::tags('fluidbook_document_' . $this->id)->flush();
+        $this->clearFilesCache();
         `$cmd`;
     }
 
+    public function clearFilesCache()
+    {
+        Cache::tags('fluidbook_document_' . $this->id)->flush();
+    }
+
     protected function updateProgression($uploadID, $message, $progress)
     {
         FluidbookDocumentUpload::updateProgression($uploadID, $message, $progress);
index 995dfacefe70711e28379e65cfe377dfc0feefd5..228c38404cb4d7e0e4c744bad908afddb4921271 100644 (file)
@@ -13,6 +13,7 @@ use App\Http\Controllers\Admin\Operations\FluidbookPublication\DownloadOperation
 use App\Http\Controllers\Admin\Operations\FluidbookPublication\PreviewOperation;
 use App\Http\Controllers\Admin\Operations\FluidbookPublication\StatsOperation;
 use App\Http\Controllers\Admin\Operations\FluidbookPublication\EditOperation;
+use App\Jobs\FluidbookImagesPreprocess;
 use App\Models\Base\ToolboxSettingsModel;
 use App\Models\Traits\PublicationSettings;
 use App\Models\Traits\SCORMVersionTrait;
@@ -137,6 +138,7 @@ class FluidbookPublication extends ToolboxSettingsModel
     {
         $this->setComposedAttributes();
         $this->checkHash();
+        FluidbookImagesPreprocess::dispatch($this->id);
         return parent::onSaved();
     }
 
index f68086e65a4e35d5ba352c919047d1b6799ecc81..0e0f978d86b0eee2657f3579e8a0f35d8b47dd18 100644 (file)
@@ -59,14 +59,15 @@ class FluidbookFarm
         return self::$_farmServers[$i];
     }
 
-    public static function getFCGIConnexion(array $farm, $timeout = 30): NetworkSocket
+    public static function getFCGIConnexion(array $farm, $timeout = 240): NetworkSocket
     {
         $timeout *= 1000;
         return new NetworkSocket($farm['host'], $farm['port'] ?? 9457, $timeout, $timeout);
     }
 
-    public static function sendRequest($farmer, $url, $params = [], $timeout = 30)
+    public static function sendRequest($farmer, $url, $params = [], $timeout = 240)
     {
+        set_time_limit(0);
         $client = new Client();
         $response = $client->sendRequest(self::getFCGIConnexion($farmer, $timeout), new PostRequest($url, http_build_query($params)));
         return trim($response->getBody());