]> _ Git - fluidbook-toolbox.git/commitdiff
wip #4628 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 21 Jul 2022 18:08:32 +0000 (20:08 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 21 Jul 2022 18:08:32 +0000 (20:08 +0200)
app/Console/Commands/WorkshopMigration.php
app/Http/Controllers/Admin/Operations/FluidbookPublication/CompositionOperation.php
app/Jobs/FluidbookDocumentFileProcess.php
app/Jobs/FluidbookDocumentUpload.php
app/Models/FluidbookDocument.php
app/Util/FluidbookFarm.php
resources/views/fields/fluidbook_composition.blade.php

index de4040babbd7e9a88c465c819505fae435fc8a9f..1fc18bff420426d84a552997e4cccb3102524f78 100644 (file)
@@ -92,6 +92,7 @@ class WorkshopMigration extends CubistCommand
             $c->bookmarks = str_replace('"titre":', '"title":', $this->_unserializeAndJSON($e->bookmarks, '[]'));
             $c->saveWithoutFlushingCache();
         }
+        DB::update('ALTER TABLE ' . $c->getTable() . ' AUTO_INCREMENT = 200000;');
     }
 
     protected function _unserializeAndJSON($s, $default = '[]')
index 12751452458f59a5efe36cb2678b2f722be5655f..64c6c0daf8250f4113942b7c6db14e7bc7f45343 100644 (file)
@@ -18,6 +18,7 @@ trait CompositionOperation
         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');
     }
 
     protected function setupCompositionDefaults()
@@ -36,7 +37,6 @@ trait CompositionOperation
     protected function upload()
     {
         $uploadID = Str::random();
-        FluidbookDocumentUpload::updateProgression($uploadID, __('Copie du document'), 1);
 
         $file = request()->file('file');
         $document = new FluidbookDocument();
@@ -50,4 +50,9 @@ trait CompositionOperation
         FluidbookDocumentUpload::dispatch($uploadID, $document, backpack_user());
         return response()->json(['uploadID' => $uploadID]);
     }
+
+    protected function uploadProgress($uploadID)
+    {
+        return response()->json(FluidbookDocumentUpload::getProgression($uploadID));
+    }
 }
index 970032b840f89731930d0e978fe174079041e288..d1baea8b56d645c157c835f845740337844c4bd6 100644 (file)
@@ -39,6 +39,9 @@ class FluidbookDocumentFileProcess extends Base
      */
     public function isDone()
     {
-        return $this->document->hasFile($this->page, $this->format, $this->resolution, $this->withText, $this->withGraphics, $this->version);
+
+        $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;
     }
 }
index 13ea82c5c4f4bd108b3c9ba6c20a6932c23e472c..f2d368c2c9b7ce453d84076b8467e52abd002c84 100644 (file)
@@ -16,7 +16,6 @@ class FluidbookDocumentUpload extends Base
     protected $document;
 
 
-
     /**
      * @var User
      */
@@ -32,8 +31,10 @@ class FluidbookDocumentUpload extends Base
 
     public function handle()
     {
+
         FluidbookDocumentUpload::updateProgression($this->uploadID, __('Début du traitement du document'), 1.25);
-        $this->document->processUpload($this->uploadID);
+        $this->document->processUpload($this->uploadID, $this->isSync());
+
     }
 
     public static function updateProgression($id, $message, $progress)
index 08ad55933b625ead1f1244cffb001ae3fdd4c145..cce846fbbdbad48e518b52e47894a4440a7057d0 100644 (file)
@@ -45,18 +45,34 @@ class FluidbookDocument extends ToolboxModel
         return sqrt($a4surface / $docSurface);
     }
 
-    public function processUpload($uploadID)
+    public function processUpload($uploadID, $sync = false)
     {
         FluidbookDocumentUpload::updateProgression($uploadID, __('Analyse du document'), 1.3);
         $this->checkInfos();
-        FluidbookDocumentUpload::updateProgression($uploadID, __('Génération des miniatures (:done/:pages)', ['pages' => $this->pages, 'done' => 0]), 2);
+        FluidbookDocumentUpload::updateProgression($uploadID, __('Nettoyage du document'), 1.5);
+        $this->fixPDF();
+        FluidbookDocumentUpload::updateProgression($uploadID, __('Découpe du document'), 1.8);
+        $this->splitPDF();
+        FluidbookDocumentUpload::updateProgression($uploadID, __('Conversion des pages (:done/:pages)', ['pages' => $this->pages, 'done' => 0]), 2);
         $jobs = [];
+
+        $files = [['jpg', 'thumb'], ['jpg', 150]];
+
         for ($i = 1; $i <= $this->pages; $i++) {
-            $job = new FluidbookDocumentFileProcess($this, $i, 'jpg', 'thumb');
-            $job->dispatch();
-            $jobs[] = $job;
+            foreach ($files as $file) {
+                $job = new FluidbookDocumentFileProcess($this, $i, $file[0], $file[1]);
+                if ($sync) {
+                    dispatch_sync($job);
+                } else {
+                    dispatch($job);
+                }
+                $jobs[] = $job;
+            }
         }
 
+        $nbjobs = count($jobs);
+        $nbfiles = count($files);
+
         while (true) {
             $done = 0;
             foreach ($jobs as $job) {
@@ -65,12 +81,12 @@ class FluidbookDocument extends ToolboxModel
                     $done++;
                 }
             }
-            $progress = $done / $this->pages;
-            FluidbookDocumentUpload::updateProgression($uploadID, __('Génération des miniatures (:done/:pages)', ['pages' => $this->pages, 'done' => $done]), 2 + $progress);
-            usleep(0.25 * 1000000);
+            $progress = $done / $nbjobs;
+            FluidbookDocumentUpload::updateProgression($uploadID, __('Conversion des pages (:done/:pages)', ['pages' => $this->pages, 'done' => round($done / $nbfiles)]), 2 + $progress);
             if ($progress === 1) {
                 break;
             }
+            usleep(0.25 * 1000000);
         }
     }
 
@@ -83,7 +99,7 @@ class FluidbookDocument extends ToolboxModel
     public function checkInfos()
     {
         if (null === $this->pdf_data) {
-            $infos = PDFTools::infos();
+            $infos = PDFTools::infos($this->path('original.pdf'));
             $this->pages = $infos['pages'];
             $this->pdf_data = $infos['infos'];
             $this->bookmarks = $infos['bookmarks'];
@@ -92,6 +108,21 @@ class FluidbookDocument extends ToolboxModel
         }
     }
 
+    public function fixPDF()
+    {
+        $fixed = $this->path('fixed.pdf');
+        $original = $this->path('original.pdf');
+        if (!file_exists($fixed) || filesize($fixed) === 0 || filemtime($fixed) < filemtime($original)) {
+            PDFTools::fixPDF($original, $fixed);
+        }
+        return $fixed;
+    }
+
+    public function splitPDF()
+    {
+        PDFTools::split($this->fixPDF(), $this->path('pdf'));
+    }
+
     /**
      * @return string
      */
@@ -102,6 +133,7 @@ class FluidbookDocument extends ToolboxModel
 
     public function hasFile($page, $format = 'jpg', $resolution = 150, $withText = true, $withGraphics = true, $version = 'html')
     {
+        $this->_normalize($format, $resolution, $withText, $withGraphics, $version);
         $cacheKey = $this->fileCacheKey($page, $format, $resolution, $withText, $withGraphics, $version);
         if (Cache::has($cacheKey)) {
             return true;
@@ -130,13 +162,8 @@ class FluidbookDocument extends ToolboxModel
         }
     }
 
-    protected function _getPath($page, $format = 'jpg', $resolution = 150, $withText = true, $withGraphics = true, $version = 'html')
+    protected function _normalize(&$format, &$resolution, &$withText, &$withGraphics, &$version)
     {
-        $cacheKey = $this->fileCacheKey($page, $format, $resolution, $withText, $withGraphics, $version);
-        if (Cache::has($cacheKey)) {
-            return Cache::get($cacheKey);
-        }
-
         if ($format === 'jpeg') {
             $format = 'jpg';
         }
@@ -151,6 +178,16 @@ class FluidbookDocument extends ToolboxModel
             $withText = true;
             $version = '';
         }
+    }
+
+    public function _getPath($page, $format = 'jpg', $resolution = 150, $withText = true, $withGraphics = true, $version = 'html')
+    {
+        $cacheKey = $this->fileCacheKey($page, $format, $resolution, $withText, $withGraphics, $version);
+        if (Cache::has($cacheKey)) {
+            return Cache::get($cacheKey);
+        }
+
+        $this->_normalize($format, $resolution, $withText, $withGraphics, $version);
 
         $dir = $this->path($version) . '/';
 
@@ -161,7 +198,6 @@ class FluidbookDocument extends ToolboxModel
                 $file .= '-' . $resolution;
             }
             $file .= '.svg';
-
         } else if ($format === 'png' || $format === 'jpg') {
             $prefix = $withText ? 't' : 'h';
             if ($resolution === 'thumb') {
@@ -178,11 +214,13 @@ class FluidbookDocument extends ToolboxModel
 
     protected function fileCacheKey($page, $format = 'jpg', $resolution = 150, $withText = true, $withGraphics = true, $version = 'html')
     {
+        $this->_normalize($format, $resolution, $withText, $withGraphics, $version);
         return 'FluidbookDocument_' . $this->id . '_' . $page . '_' . $format . '_' . $resolution . '_' . ($withText ? '1' : '0') . '_' . ($withGraphics ? '1' : '0') . '_' . $version;
     }
 
     public function getFile($page, $format = 'jpg', $resolution = 150, $withText = true, $withGraphics = true, $version = 'html', $force = false)
     {
+        $this->_normalize($format, $resolution, $withText, $withGraphics, $version);
         if ($force) {
             $this->removeFile($page, $format, $resolution, $withText, $withGraphics, $version);
         }
@@ -206,7 +244,7 @@ class FluidbookDocument extends ToolboxModel
     public function _getFile($page, $format = 'jpg', $resolution = 150, $withText = true, $withGraphics = true, $version = 'html')
     {
         if (!$this->hasFile($page, $format, $resolution, $withText, $withGraphics, $version)) {
-            return FluidbookFarm::getFile($page, $format, $resolution, $withText, $withGraphics, $version, $this->getResolutionRatio(), $this->getMobileFirstRatio(), $this->path(), $force);
+            return FluidbookFarm::getFile($page, $format, $resolution, $withText, $withGraphics, $version, $this->getResolutionRatio(), $this->getMobileFirstRatio(), $this->path() . '/');
         }
 
         $path = $this->_getPath($page, $format, $resolution, $withText, $withGraphics, $version);
index 8cf49192348c424e2297cbe761d141e341b4d19f..707e6ae16afbbc9c988130fd297b10400565c0b9 100644 (file)
@@ -50,7 +50,7 @@ class FluidbookFarm
         return self::$_farmServers[$i];
     }
 
-    public static function getFile($page, $format, $resolution, $withText, $withGraphics, $version, $resolutionRatio, $mobileFirstRatio, $path, $force)
+    public static function getFile($page, $format, $resolution, $withText, $withGraphics, $version, $resolutionRatio, $mobileFirstRatio, $path, $force = false)
     {
         $start = microtime(true);
         $farmer = self::pickOneServer();
@@ -72,8 +72,9 @@ class FluidbookFarm
             echo $cl->getOutput();
             $res = false;
         }
+
         $time = round(microtime(true) - $start, 4);
-        $log = '[' . $farmer['name'] . ']' . "\t" . date('Y-m-d H:i:s') . "\t\t\t\t" . $time . "\t\t\t\t$page|$format|$resolution|$withText|$withGraphics|$version\t\t\t\t$res\t\t\t\t" + $cl->getOutput() + "\n";
+        $log = '[' . $farmer['name'] . ']' . "\t" . date('Y-m-d H:i:s') . "\t\t\t\t" . $time . "\t\t\t\t$page|$format|$resolution|$withText|$withGraphics|$version\t\t\t\t$res\t\t\t\t" . $cl->getOutput() . "\n";
 
         $fp = fopen($path . '/farm.log', 'a+');
         fwrite($fp, $log);
index 547454ff36af5ecfb0cb5390275092b386830323..6e2a5124626e39f7c55bdec448f780d69064234f 100644 (file)
@@ -6,6 +6,7 @@
                 var conversion = '';
                 var conversionOperation;
                 var conversionPages;
+                var progressInterval;
 
                 function initPages() {
                     var data = JSON.parse($("#compositionField").val());
                     });
                     $(document).on('submit', '#compositionUploadForm', function () {
                         $(this).ajaxSubmit({
-                            complete: function (data) {
-                                console.log(data);
+                            success: function (data) {
+                                var uploadID = data.uploadID;
+                                progressInterval = setInterval(function () {
+                                    updateProgressBar(uploadID);
+                                }, 250);
                             },
                             uploadProgress: function (event, position, total, percentComplete) {
-                                console.log(percentComplete);
+                                var progress = position / total;
+                                var message = '{{__('Chargement du document')}}';
+                                if (progress === 1) {
+                                    message = '{{__('Initialisation de la conversion')}}';
+                                }
+                                showProgressBar(message, progress);
                             },
                             error: function (data) {
                                 new Noty({
                     });
                 }
 
+                function updateProgressBar(uploadID) {
+                    $.ajax({
+                        url: '{{backpack_url($entry->getOption('name').'/uploadProgress')}}' + '/' + uploadID,
+                        success: function (data) {
+                            showProgressBar(data.message, data.progress);
+                        }
+                    })
+                }
+
+                function showProgressBar(message, progress) {
+                    console.log(message, progress);
+                    if(progress===3){
+                        hideProgressBar();
+                    }
+                }
+
+                function hideProgressBar(){
+                    clearInterval(progressInterval);
+                    console.log('End conversion');
+                }
+
                 function updateComposition() {
                     var res = {};
                     var numbers = [];