From: Vincent Vanwaelscappel Date: Thu, 25 Aug 2022 16:01:15 +0000 (+0200) Subject: wip #5396 @3 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=0c0bc479e973438a4d656dc8663e33314cb74f04;p=fluidbook-toolbox.git wip #5396 @3 --- diff --git a/app/Http/Controllers/Admin/Operations/FluidbookPublication/PreviewOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookPublication/PreviewOperation.php index ee0694ea1..20d4a35a1 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookPublication/PreviewOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookPublication/PreviewOperation.php @@ -5,7 +5,11 @@ namespace App\Http\Controllers\Admin\Operations\FluidbookPublication; use App\Http\Middleware\CheckIfAdmin; use App\Jobs\FluidbookCompiler; use App\Models\FluidbookPublication; +use App\Models\FluidbookTheme; +use App\Models\User; use Cubist\Backpack\Http\Controllers\Base\XSendFileController; +use Cubist\Util\Graphics\Color; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Route; trait PreviewOperation @@ -14,18 +18,14 @@ trait PreviewOperation { // 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') + return $this->_preview($segment, $id, $hash, null, 'index.html'); + })->where('id', '([0-9]+)(-[0-9]+)?') ->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') + return $this->_preview($segment, $id, $hash, $time, $path); + })->where('id', '([0-9]+)(-[0-9]+)?') ->where('hash', '[0-9a-f]{32}') ->where('path', '.*') ->withoutMiddleware([CheckIfAdmin::class]); @@ -36,21 +36,158 @@ trait PreviewOperation $this->crud->addButtonFromView('line', 'preview', 'fluidbook_publication.preview', 'end'); } + protected function _preview($segment, $id, $hash, $time = null, $path = null) + { + $q = request()->getQueryString(); + if ($q) { + $q = '?' . $q; + } + $nointerface = !!request('nointerface', false); + $shortLoading = !!request('shortLoading', false); + + if (null === $time || ($time < (time() - 60) && !$nointerface && !$shortLoading)) { + $url = backpack_url($segment . '/preview/' . $id . '_' . $hash . '_' . time()) . '/' . $q; + return $this->loadingCompile($url, $id, $hash); + } + + self::_getFluidbookAndTheme($id, $hash, $fluidbook, $theme); + $check = $this->_checkDemoLinkAuth($fluidbook); + if ($check !== true) { + return $check; + } + return $this->preview($fluidbook, $theme, $path); + } + /** - * @throws \Exception + * @param $fluidbook + * @return true|\Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ - public function preview($id, $hash, $path = 'index.html') + protected function _checkDemoLinkAuth($fluidbook) { - $fluidbook = FluidbookPublication::where('id', $id)->where('hash', $hash)->first(); - if (null === $fluidbook) { + if (!Auth::guest()) { + return true; + } + if ($fluidbook->redirectDemo) { + return redirect($fluidbook->redirectDemo); + } + if ($fluidbook->disableDemo) { abort(404); } + } + + + /** + * @param $url string + * @param $id string + * @param $hash string + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response + */ + public function loadingCompile($url, $id, $hash) + { + self::_getFluidbookAndTheme($id, $hash, $fluidbook, $theme); + + $bgcolor = Color::colorToCSS($theme->backgroundColor); + $scolor = Color::colorToCSS($theme->loadingSecColor); + $tcolor = $lcolor = Color::colorToCSS($theme->couleurL); + if ($tcolor == $bgcolor) { + $tcolor = $scolor; + } + + $res = ''; + $res .= ''; + $res .= ''; + if (!request()->input('shortLoading', false)) { + $res .= ''; + $res .= ''; + $res .= '' . $fluidbook->title . ''; + $res .= ''; + $res .= ''; + $res .= ' + + + + '; + + $res .= '

' . __('Compilation du fluidbook en cours') . '...

'; + $res .= '

' . __('Cette étape ne sera pas nécessaire lorsque le fluidbook sera installé sur son emplacement définitif') . '

'; + } else { + $res .= ''; + $res .= ''; + $res .= ''; + } + + $res .= ''; + $res .= ''; + return response($res); + } + + /** + * @throws \Exception + */ + public function preview($fluidbook, $theme, $path = 'index.html') + { + $nointerface = !!request('nointerface', false); + $shortLoading = !!request('shortLoading', false); - $dest = $fluidbook->getFinalPath(); + $dest = $fluidbook->getFinalPath($theme); if ($path === 'index.html') { - $compiler = new FluidbookCompiler($fluidbook); + $compiler = new FluidbookCompiler($fluidbook, theme: $theme); $compiler->handle(); } return XSendFileController::sendfile($dest . '/' . $path); } + + /** + * @param $id string + * @param $hash string + * @param $fluidbook FluidbookPublication + * @param $theme FluidbookTheme + * @return void + */ + protected static function _getFluidbookAndTheme($id, $hash, &$fluidbook, &$theme) + { + $ee = explode('-', $id); + if (count($ee) === 1) { + $forceThemeData = request('theme', false); + if ($forceThemeData) { + $theme = FluidbookTheme::fromArray(json_decode($forceThemeData, true)); + } + } else { + $theme = FluidbookTheme::find($ee[1]); + $id = $ee[0]; + } + + $fluidbook = FluidbookPublication::where('id', $id)->where('hash', $hash)->first(); + if (null === $fluidbook) { + abort(404); + } + if (!isset($theme)) { + $theme = $fluidbook->getTheme(); + } + } } diff --git a/app/Jobs/FluidbookCompiler.php b/app/Jobs/FluidbookCompiler.php index 825c75c86..bf75a7cec 100644 --- a/app/Jobs/FluidbookCompiler.php +++ b/app/Jobs/FluidbookCompiler.php @@ -272,13 +272,13 @@ class FluidbookCompiler extends Base implements CompilerInterface * @param $standalone * @param $appcache * @param $home - * @param $forceTheme + * @param $theme FluidbookTheme|null * @param $hybrid * @param Command|null $command * @throws \Exception */ - function __construct(FluidbookPublication $book, $version = null, $phonegap = false, $phonegapVersion = 'latest', $dir = null, $standalone = false, $appcache = false, $home = false, $forceTheme = false, $hybrid = false, Command $command = null) + function __construct(FluidbookPublication $book, $version = null, $phonegap = false, $phonegapVersion = 'latest', $dir = null, $standalone = false, $appcache = false, $home = false, FluidbookTheme $theme = null, $hybrid = false, Command $command = null) { parent::__construct(); @@ -306,8 +306,7 @@ class FluidbookCompiler extends Base implements CompilerInterface $this->book_id = $this->getFluidbook()->id; $this->log('Start compilation'); - $forceThemeId = FluidbookTheme::hashThemeArray($forceTheme); - $this->dir = $this->getFluidbook()->getFinalPath(); + $this->dir = $this->getFluidbook()->getFinalPath($theme); $this->vdir = new VirtualDirectory($this->dir); $this->wdir = $this->getFluidbook()->getAssetDir(); @@ -317,13 +316,8 @@ class FluidbookCompiler extends Base implements CompilerInterface $this->pages = $this->getFluidbook()->composition; $this->maxRes = min(self::MAX_RES, $this->fluidbookSettings->maxResolution); - if (is_array($forceTheme)) { - $this->theme = FluidbookTheme::fromArray($forceTheme); - } else if (is_numeric($forceTheme)) { - $this->theme = FluidbookTheme::find($forceTheme); - } else { - $this->theme = FluidbookTheme::find($this->getFluidbook()->theme); - } + $this->theme = $theme ?? $this->getFluidbook()->getTheme(); + $this->themeSettings = $this->theme->getPageData(); $this->log('Got data from database'); diff --git a/app/Models/FluidbookPublication.php b/app/Models/FluidbookPublication.php index 3b142db8d..e3f27b9f4 100644 --- a/app/Models/FluidbookPublication.php +++ b/app/Models/FluidbookPublication.php @@ -154,6 +154,14 @@ class FluidbookPublication extends ToolboxSettingsModel return __($str); } + /** + * @return FluidbookTheme + */ + public function getTheme() + { + return FluidbookTheme::find($this->theme); + } + /** * @return string */ @@ -257,9 +265,13 @@ class FluidbookPublication extends ToolboxSettingsModel return $this->getDocumentSize($page)[1]; } - public function getFinalPath() + public function getFinalPath($theme = null) { - return protected_path('fluidbookpublication/final/' . $this->id); + $dir = $this->id; + if (null === $theme || $theme->id != $this->theme) { + $dir .= '-' . $theme->id; + } + return protected_path('fluidbookpublication/final/' . $dir); } public function getAssetDir() diff --git a/app/Models/FluidbookTheme.php b/app/Models/FluidbookTheme.php index d9f6b8bc6..5c52e7a40 100644 --- a/app/Models/FluidbookTheme.php +++ b/app/Models/FluidbookTheme.php @@ -756,7 +756,12 @@ class FluidbookTheme extends ToolboxSettingsModel */ public static function fromArray(array $a) { - return new FluidbookTheme($a); + $res = new FluidbookTheme(); + $res->id = self::hashThemeArray($a); + foreach ($a as $k => $v) { + $res->setAttribute($k, $v); + } + return $res; } public function getStorage()