]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5898 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 28 Jun 2023 07:16:22 +0000 (09:16 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 28 Jun 2023 07:16:22 +0000 (09:16 +0200)
app/Console/Commands/FluidbookSocialImage.php [new file with mode: 0644]
app/Fluidbook/SocialImage.php
app/Jobs/FluidbookSocialImage.php [new file with mode: 0644]
app/Models/FluidbookPublication.php
resources/fluidbookpublication/social_screenshot/social_screenshot.js

diff --git a/app/Console/Commands/FluidbookSocialImage.php b/app/Console/Commands/FluidbookSocialImage.php
new file mode 100644 (file)
index 0000000..b6a6a65
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\FluidbookPublication;
+use Cubist\Backpack\Console\Commands\CubistCommand;
+use Cubist\Util\ArrayUtil;
+use Cubist\Util\PHP;
+
+class FluidbookSocialImage extends CubistCommand
+{
+    protected $signature = 'fluidbook:socialimage {id} {--force}';
+    protected $description = 'Generate image for social networks';
+
+    /**
+     * @throws \Exception
+     */
+    public function handle()
+    {
+        PHP::neverStop(true);
+
+        $ids = ArrayUtil::parseRange($this->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'));
+        }
+    }
+}
index 50db969bbefa03a42aca8f03cabbf81287ebcefb..7a11b3b75f13c26a31da437e79e158a82db2f378 100644 (file)
@@ -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 (file)
index 0000000..312c613
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Fluidbook\SocialImage;
+use App\Models\FluidbookPublication;
+
+class FluidbookSocialImage extends Base
+{
+
+
+    /**
+     * @var FluidbookPublication
+     */
+    protected $fluidbook;
+
+    /**
+     * @var bool
+     */
+    protected $force;
+
+    /**
+     * @param $id int|FluidbookPublication
+     * @param $force bool
+     * @throws \Exception
+     */
+
+    public function __construct($id, $force = false)
+    {
+        if ($id instanceof FluidbookPublication) {
+            $this->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);
+    }
+
+}
index 2d0f2104acb463d6825328cfc6d6c95e7e97a99c..87b0f8c7c7c16a97e18eba30e2e331daeb13177c 100644 (file)
@@ -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();
     }
 
index 14956cc0722c64023104f96c51a03715e583c03c..20954d803bf888abdf5c1cbe2bf84514e8cf6425 100644 (file)
@@ -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();