]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5399 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 24 Aug 2022 19:48:19 +0000 (21:48 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 24 Aug 2022 19:48:19 +0000 (21:48 +0200)
app/Http/Controllers/Admin/Operations/FluidbookPublication/PreviewOperation.php
app/Jobs/FluidbookCompiler.php
app/Models/FluidbookPublication.php
public/.htaccess

index 4f8426dd79ef5eeed6a27f0a5ab2da6420739035..ee0694ea166b3f12ae90a38b481ce0c0384bed84 100644 (file)
@@ -3,30 +3,32 @@
 namespace App\Http\Controllers\Admin\Operations\FluidbookPublication;
 
 use App\Http\Middleware\CheckIfAdmin;
+use App\Jobs\FluidbookCompiler;
 use App\Models\FluidbookPublication;
+use Cubist\Backpack\Http\Controllers\Base\XSendFileController;
 use Illuminate\Support\Facades\Route;
 
 trait PreviewOperation
 {
     protected function setupPreviewRoutes($segment, $routeName, $controller)
     {
-//        // Redirect to the url with a timestamp to prevent cache
-//        Route::match(['get'], $segment . '/preview/{id}_{hash}', function ($id, $hash) use ($segment) {
-//            return redirect(backpack_url($segment . '/preview/' . $id . '_' . $hash . '_' . time()));
-//        })->whereNumber('id')
-//            ->where('hash', '[0-9a-f]{32}')
-//            ->withoutMiddleware([CheckIfAdmin::class]);
-//
-//        Route::match(['get'], $segment . '/preview/{id}_{hash}_{time}/{path?}', function ($id, $hash, $time, $path = 'index.html') use ($segment, $controller) {
-//            // If timestamp is too old, redirect to a more recent one
-//            if ($path === 'index.html' && (time() - $time) > 30) {
-//                return redirect(backpack_url($segment . '/preview/' . $id . '_' . $hash . '_' . time() . '/'));
-//            }
-//            return $this->preview($id, $hash, $path);
-//        })->whereNumber('id')
-//            ->where('hash', '[0-9a-f]{32}')
-//            ->where('path', '.*')
-//            ->withoutMiddleware([CheckIfAdmin::class]);
+        // Redirect to the url with a timestamp to prevent cache
+        Route::match(['get'], $segment . '/preview/{id}_{hash}', function ($id, $hash) use ($segment) {
+            return redirect(backpack_url($segment . '/preview/' . $id . '_' . $hash . '_' . time()) . '/');
+        })->whereNumber('id')
+            ->where('hash', '[0-9a-f]{32}')
+            ->withoutMiddleware([CheckIfAdmin::class]);
+
+        Route::match(['get'], $segment . '/preview/{id}_{hash}_{time}/{path?}', function ($id, $hash, $time, $path = 'index.html') use ($segment, $controller) {
+            // If timestamp is too old, redirect to a more recent one
+            if ($path === 'index.html' && (time() - $time) > 30) {
+                return redirect(backpack_url($segment . '/preview/' . $id . '_' . $hash . '_' . time()) . '/');
+            }
+            return $this->preview($id, $hash, $path);
+        })->whereNumber('id')
+            ->where('hash', '[0-9a-f]{32}')
+            ->where('path', '.*')
+            ->withoutMiddleware([CheckIfAdmin::class]);
     }
 
     protected function setupPreviewDefaults()
@@ -34,17 +36,21 @@ trait PreviewOperation
         $this->crud->addButtonFromView('line', 'preview', 'fluidbook_publication.preview', 'end');
     }
 
+    /**
+     * @throws \Exception
+     */
     public function preview($id, $hash, $path = 'index.html')
     {
         $fluidbook = FluidbookPublication::where('id', $id)->where('hash', $hash)->first();
         if (null === $fluidbook) {
             abort(404);
         }
-        $dest = $fluidbook->getFinalPath();
 
+        $dest = $fluidbook->getFinalPath();
         if ($path === 'index.html') {
-            $fluidbook->compile($dest);
+            $compiler = new FluidbookCompiler($fluidbook);
+            $compiler->handle();
         }
-        return response('Preview of fluidbook ' . $id . ': ' . $path);
+        return XSendFileController::sendfile($dest . '/' . $path);
     }
 }
index b4ddcd4da2743e84d2d7a327b09c4e0a7157fb8e..b5fe576494e30d332d7a018c08fce54550609a1a 100644 (file)
@@ -12,6 +12,7 @@ use App\Models\FluidbookTheme;
 use App\Models\FluidbookTranslate;
 use App\Models\Traits\FluidbookPlayerBranches;
 use App\Util\FluidbookLinks;
+use Cubist\Backpack\Magic\Fields\Checkbox;
 use Cubist\Excel\ExcelToArray;
 use Cubist\Locale\Country;
 use Cubist\Locale\Locale;
@@ -346,8 +347,6 @@ class FluidbookCompiler extends Base implements CompilerInterface
 
         $this->scale = 1;
 
-        $this->numerotation = explode(',', $this->getFluidbook()->numerotation);
-
         if ($this->isMobileFirst()) {
             $this->cssScale = $this->cssOneScale = 480 / $this->width;
             $this->linkScale = $this->cssScale;
@@ -455,9 +454,11 @@ class FluidbookCompiler extends Base implements CompilerInterface
     public function initConfig()
     {
         $this->config = new Data(array_merge($this->fluidbookSettings->getRawData()['settings'], $this->themeSettings->getRawData()));
+        $this->config->pages = count($this->getFluidbook()->composition);
         $this->config->bookmarkDisablePages = ArrayUtil::parseRange($this->config->bookmarkDisablePages);
         $this->config->rasterizePages = ArrayUtil::parseRange($this->config->rasterizePages);
         $this->config->vectorPages = array_diff(ArrayUtil::parseRange($this->config->vectorPages), $this->config->rasterizePages);
+        $this->numerotation = $this->config->numerotation = explode(',', $this->getFluidbook()->page_numbers);
 
         $hideOnPages = ArrayUtil::parseRange($this->config->tabsHideOnPages);
         $this->config->tabsDisabledOnPages = ArrayUtil::parseRange($this->config->tabsDisabledOnPages);
@@ -476,14 +477,13 @@ class FluidbookCompiler extends Base implements CompilerInterface
 
     protected function populateConfig()
     {
-        $this->config->numerotation = explode(',', $this->getFluidbook()->numerotation);
         $this->config->id = $this->getFluidbook()->book_id;
         $this->config->cid = $this->getFluidbook()->cid;
         $this->config->cacheDate = time();
-        $this->config->width = $this->cssWidth;
-        $this->config->height = $this->cssHeight;
-        $this->config->optimalWidth = $this->optimalWidth;
-        $this->config->optimalHeight = $this->optimalHeight;
+        $this->config->width = round($this->cssWidth, 2);
+        $this->config->height = round($this->cssHeight, 2);
+        $this->config->optimalWidth = round($this->optimalWidth, 2);
+        $this->config->optimalHeight = round($this->optimalHeight, 2);
         $this->config->chapters = $this->getFluidbook()->chapters;
         $this->config->videoFormats = $this->getVideosFormats(false);
         $this->config->htmlmultimedia = $this->htmlmultimedia;
@@ -492,9 +492,9 @@ class FluidbookCompiler extends Base implements CompilerInterface
         $this->config->standardResolution = min($this->fluidbookSettings->maxResolution, 150);
         $this->config->pageLabels = $this->pageLabels;
         $this->config->pageZoomFactor = $this->z;
-        $this->config->multiply = $this->multiply;
-        $this->config->cssScale = $this->cssScale;
-        $this->config->pdfZoomFactor = $this->pdf2htmlRatio;
+        $this->config->multiply = round($this->multiply, 6);
+        $this->config->cssScale = round($this->cssScale, 6);
+        $this->config->pdfZoomFactor = round($this->pdf2htmlRatio, 6);
         if ($this->home) {
             $this->config->home = 'http://home';
         }
@@ -2866,13 +2866,21 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
 
     protected function writeConfig()
     {
-        $c = json_encode($this->config);
-        return 'var SETTINGS=' . $c . ';' . "\n";
+        $data = $this->config->getRawData();
+        foreach ($data as $k => $v) {
+            if (null === $v) {
+                $v = $data[$k] = '';
+            }
+            if ($this->getFluidbook()->getField($k) instanceof Checkbox) {
+                $v = $data[$k] = !!$v;
+            }
+        }
+        return 'var SETTINGS=' . json_encode($data) . ';' . "\n";
     }
 
     protected function writeCountries()
     {
-        $c = Zend_Locale::getTranslationList('Territory', $this->getFluidbook()->lang, 2);
+        $c = Country::getList($this->getFluidbook()->locale);
         asort($c);
         $this->config->countries = $c;
     }
@@ -2954,7 +2962,7 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
         $k = 'pagesDimensions.' . $page;
         if (!$this->config->has($k)) {
             $d = $this->getFluidbook()->getDocumentSize($page);
-            $res = [$this->cssWidth, $d[1] * ($this->cssWidth / $d[0])];
+            $res = [round($this->cssWidth, 2), round($d[1] * ($this->cssWidth / $d[0]), 2)];
             $this->config->set($k, $res);
             return $res;
         }
index de89ac656f8c308bb2739028e64e77c23e6a66c3..3b142db8df752ab3cbcb67c84f990c68f3c2ff51 100644 (file)
@@ -222,7 +222,10 @@ class FluidbookPublication extends ToolboxSettingsModel
                     continue;
                 }
                 $doc = self::_getDocument($data[0]);
-                $this->_documentsSize[$data[0]] = $doc->pdf_data['size'];
+                $s = $doc->pdf_data['size'];
+                $s[0] = round($s[0], 2);
+                $s[1] = round($s[1], 2);
+                $this->_documentsSize[$data[0]] = $s;
             }
         }
         return $this->_documentsSize;
index ab989af585e5daa3c5d25e4c4ad19750b7d9c3b5..d4416c8e5d9807f92f031521d40c80b31db8f37a 100644 (file)
@@ -7,18 +7,13 @@
 
     # Redirect to https if accessing from http
     RewriteCond %{HTTPS} off
-    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
+    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=308,L]
 
     # Handle Authorization Header
     RewriteCond %{HTTP:Authorization} .
     RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
 
-    RewriteRule ^admin(.*)$ $1 [L,R=301]
-
-    # Redirect Trailing Slashes If Not A Folder...
-    RewriteCond %{REQUEST_FILENAME} !-d
-    RewriteCond %{REQUEST_URI} (.+)/$
-    RewriteRule ^ %1 [L,R=301]
+    RewriteRule ^admin(.*)$ $1 [L,R=308]
 
     # Handle Front Controller...
     RewriteCond %{REQUEST_FILENAME} !-d