From: Vincent Vanwaelscappel Date: Wed, 24 Aug 2022 19:48:19 +0000 (+0200) Subject: wip #5399 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=916c633154e7189693ca3b6e3c16fec9a02cf16b;p=fluidbook-toolbox.git wip #5399 @2 --- diff --git a/app/Http/Controllers/Admin/Operations/FluidbookPublication/PreviewOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookPublication/PreviewOperation.php index 4f8426dd7..ee0694ea1 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookPublication/PreviewOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookPublication/PreviewOperation.php @@ -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); } } diff --git a/app/Jobs/FluidbookCompiler.php b/app/Jobs/FluidbookCompiler.php index b4ddcd4da..b5fe57649 100644 --- a/app/Jobs/FluidbookCompiler.php +++ b/app/Jobs/FluidbookCompiler.php @@ -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"> 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"> $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; } diff --git a/app/Models/FluidbookPublication.php b/app/Models/FluidbookPublication.php index de89ac656..3b142db8d 100644 --- a/app/Models/FluidbookPublication.php +++ b/app/Models/FluidbookPublication.php @@ -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; diff --git a/public/.htaccess b/public/.htaccess index ab989af58..d4416c8e5 100644 --- a/public/.htaccess +++ b/public/.htaccess @@ -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