From: Vincent Vanwaelscappel Date: Wed, 28 Jun 2023 07:16:22 +0000 (+0200) Subject: wip #5898 @1 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=dcccac6e9dc290d724b6a876f0fc3cf6bf6d13fa;p=fluidbook-toolbox.git wip #5898 @1 --- diff --git a/app/Console/Commands/FluidbookSocialImage.php b/app/Console/Commands/FluidbookSocialImage.php new file mode 100644 index 000000000..b6a6a65de --- /dev/null +++ b/app/Console/Commands/FluidbookSocialImage.php @@ -0,0 +1,29 @@ +argument('id')); + $fluidbooks = FluidbookPublication::withoutGlobalScopes()->where('created_ok', '1')->whereIn('id', $ids)->get(); + foreach ($fluidbooks as $fluidbook) { + $this->line('Generate social image for ' . $fluidbook->id); + \App\Jobs\FluidbookSocialImage::dispatchSync($fluidbook, $this->option('force')); + } + } +} diff --git a/app/Fluidbook/SocialImage.php b/app/Fluidbook/SocialImage.php index 50db969bb..7a11b3b75 100644 --- a/app/Fluidbook/SocialImage.php +++ b/app/Fluidbook/SocialImage.php @@ -7,13 +7,14 @@ use Cubist\Util\CommandLine; use Cubist\Util\Files\Files; use Cubist\Util\Graphics\Image; -class SocialImage{ +class SocialImage +{ /** * @param $fluidbook FluidbookPublication * @return string */ - public static function createSocialImage($fluidbook) + public static function createSocialImage($fluidbook, $force = false) { $id = $fluidbook->id; $wid = $fluidbook->getAssetDir() . '/'; @@ -30,13 +31,13 @@ class SocialImage{ $limit = max(time() - (3600 * 24 * 30), $fluidbook->updated_at->getTimestamp()); $minsize = 20 * 1024; - if (isset($_GET['force']) || !file_exists($res) || filemtime($res) < $limit || filesize($res) < $minsize) { + if ($force || isset($_GET['force']) || !file_exists($res) || filemtime($res) < $limit || filesize($res) < $minsize) { $lock = self::socialImagePath($id, 'lock'); if (!file_exists($lock) || filemtime($lock) < time() - 3600) { touch($lock); - $force = (file_exists($res) && filemtime($res) < $limit) || isset($_GET['forcecompile']); - $url = route('fluidbook_preview_with_time', ['version' => 'online', 'id' => $id, 'hash' => $fluidbook->hash, 'time' => time(), 'nointerface' => 1, 'force' => $force ? '1' : '0', 'puppeteer' => '1']); + $force = $force || (file_exists($res) && filemtime($res) < $limit) || isset($_GET['forcecompile']); + $url = route('fluidbook_preview_with_time', ['version' => 'online', 'id' => $id, 'hash' => $fluidbook->hash, 'time' => time(), 'nointerface' => 1, 'force' => $force ? '1' : '0', 'puppeteer' => '1', 'path' => 'index.html']); $w = 1200; $h = 628; @@ -45,12 +46,14 @@ class SocialImage{ $cl->setArg(null, resource_path('fluidbookpublication/social_screenshot/social_screenshot.js')); $cl->setArg('width', $w); $cl->setArg('height', $h); - $cl->setArg('delay', 10); + $cl->setArg('delay', 2); $cl->setArg('scale', 0.5); $cl->setArg('dest', $res); - $cl->setArg('url', '"'.$url.'"'); + $cl->setArg('url', '"' . $url . '"'); $cl->execute(); unlink($lock); + + self::getSocialImageSize($fluidbook); } } } @@ -64,8 +67,8 @@ class SocialImage{ */ public static function getSocialImageSize($fluidbook) { - $socialImage = self::createSocialImage($fluidbook); - if(!file_exists($socialImage)){ + $socialImage = self::socialImagePath($fluidbook->id); + if (!file_exists($socialImage)) { return null; } $sizeFile = self::socialImagePath($fluidbook->id, 'size'); diff --git a/app/Jobs/FluidbookSocialImage.php b/app/Jobs/FluidbookSocialImage.php new file mode 100644 index 000000000..312c61384 --- /dev/null +++ b/app/Jobs/FluidbookSocialImage.php @@ -0,0 +1,52 @@ +fluidbook = $id; + $id = $this->fluidbook->id; + } else { + $this->fluidbook = FluidbookPublication::withoutGlobalScopes()->find($id); + } + if (!$this->fluidbook) { + throw new \Exception('Unable to generate social image for fluidbook ' . $id . ' : fluidbook not found'); + } + $this->force = $force; + } + + public function uniqueId() + { + return $this->fluidbook->id; + } + + public function handle() + { + SocialImage::createSocialImage($this->fluidbook, $this->force); + } + +} diff --git a/app/Models/FluidbookPublication.php b/app/Models/FluidbookPublication.php index 2d0f2104a..87b0f8c7c 100644 --- a/app/Models/FluidbookPublication.php +++ b/app/Models/FluidbookPublication.php @@ -29,6 +29,7 @@ use App\Http\Controllers\Admin\Operations\FluidbookPublication\Services\ExportPd use App\Http\Controllers\Admin\Operations\FluidbookPublication\Services\SocialImageOperation; use App\Http\Controllers\Admin\Operations\FluidbookPublication\StatsOperation; use App\Jobs\FluidbookImagesPreprocess; +use App\Jobs\FluidbookSocialImage; use App\Models\Base\ToolboxSettingsModel; use App\Models\Traits\CheckHash; use App\Models\Traits\PublicationSettings; @@ -280,6 +281,7 @@ class FluidbookPublication extends ToolboxSettingsModel if ($this->_compositionUpdated) { FluidbookImagesPreprocess::dispatch($this->id); } + FluidbookSocialImage::dispatch($this->id); return parent::onSaved(); } diff --git a/resources/fluidbookpublication/social_screenshot/social_screenshot.js b/resources/fluidbookpublication/social_screenshot/social_screenshot.js index 14956cc07..20954d803 100644 --- a/resources/fluidbookpublication/social_screenshot/social_screenshot.js +++ b/resources/fluidbookpublication/social_screenshot/social_screenshot.js @@ -3,7 +3,7 @@ const commandLineArgs = require('command-line-args'); const optionDefinitions = [ {name: 'url', type: String}, {name: 'dest', type: String}, - {name: 'delay', type: Number, defaultOption: 10}, + {name: 'delay', type: Number, defaultOption: 2}, {name: 'scale', type: Number, defaultOption: 1}, {name: 'width', type: Number, defaultOption: 1920}, {name: 'height', type: Number, defaultOption: 1080} @@ -27,7 +27,7 @@ const optionDefinitions = [ deviceScaleFactor: options.scale, }); await page.setDefaultNavigationTimeout(0); - await page.goto(options.url); + await page.goto(options.url, {waitUntil: 'networkidle2'}); await new Promise(r => setTimeout(r, 1000 * options.delay)); await page.screenshot({path: options.dest, type: 'jpeg', quality: 95}); await browser.close();