]> _ Git - fluidbook-toolbox.git/commitdiff
wip #4592 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 15 Jul 2021 16:48:08 +0000 (18:48 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 15 Jul 2021 16:48:08 +0000 (18:48 +0200)
app/Jobs/GenerateThemePreview.php [new file with mode: 0644]
app/Models/FluidbookTheme.php
resources/js/social_screenshot/social_screenshot.js [new file with mode: 0644]

diff --git a/app/Jobs/GenerateThemePreview.php b/app/Jobs/GenerateThemePreview.php
new file mode 100644 (file)
index 0000000..4c327bf
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Models\FluidbookTheme;
+use Cubist\Util\CommandLine;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Contracts\Queue;
+
+
+class GenerateThemePreview implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    /**
+     * @var FluidbookTheme
+     */
+    public $theme;
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct(FluidbookTheme $theme)
+    {
+        $this->theme = $theme;
+    }
+
+    public function uniqueId()
+    {
+        return $this->theme->id;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        $dest=storage_path('themes') . '/' . $this->theme->getIdValue() . '.jpg';
+
+        $cl = new CommandLine('node');
+        $cl->setArg(null, resource_path('js/social_screenshot/social_screenshot.js'));
+        $cl->setArg('width', 1024);
+        $cl->setArg('height', 768);
+        $cl->setArg('delay', 2);
+        $cl->setArg('scale', 1);
+        $cl->setArg('dest', $dest);
+        $cl->setArg('url', 'https://workshop.fluidbook.com/viewerh/20098-' . $this->theme->getIdValue() . '_f2e0452eed6dba9878016ce5603fdc54_' . time() . '/?shortLoading=1#/page/2');
+        $cl->execute();
+    }
+}
index a7e4787128f3c67e8302131f167cfdd3808a436d..2bfe9f8d62018f1f2672e1b2b728a3229d465224 100644 (file)
@@ -5,6 +5,7 @@ namespace App\Models;
 
 
 use App\Fields\User;
+use App\Jobs\GenerateThemePreview;
 use Cubist\Backpack\Magic\Fields\Color;
 use Cubist\Backpack\Magic\Fields\Files;
 use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
@@ -644,6 +645,7 @@ class FluidbookTheme extends CubistMagicAbstractModel
             }
         }
         $ignore = ['id', 'name', 'owner', 'created_at', 'deleted_at', 'updated_at', 'slug'];
+        $t3dir = '/data1/extranet/www/fluidbook/themes3/';
 
         $data = [];
         foreach (FluidbookTheme::all() as $theme) {
@@ -666,7 +668,7 @@ class FluidbookTheme extends CubistMagicAbstractModel
                         $media = $theme->getMedia($v)->get(0);
                         if (null !== $media) {
                             $v = $media->getAttribute('file_name');
-                            $dir = '/data1/extranet/www/fluidbook/themes3/' . $theme->id;
+                            $dir = $t3dir . $theme->id;
                             if (!file_exists($dir)) {
                                 mkdir($dir, 0777, true);
                             }
@@ -688,6 +690,12 @@ class FluidbookTheme extends CubistMagicAbstractModel
                 $settings[$k] = $v;
             }
             $data[] = ['theme_id' => $theme->id, 'signature' => 0, 'nom' => $theme->name, 'proprietaire' => $theme->owner, 'icones' => $theme->iconSet, 'date' => strtotime($theme->creation_date), 'parametres' => json_encode($settings)];
+
+            $dest = $t3dir . '/' . $theme->id . '.jpg';
+            if(!file_exists($dest)) {
+                $preview = storage_path('themes/' . $theme->id . '.jpg');
+                `ln -s $preview $dest`;
+            }
         }
         $t = DB::table('extranet_clean.ws3_theme');
         $t->truncate();
@@ -779,6 +787,7 @@ class FluidbookTheme extends CubistMagicAbstractModel
         if (self::$updateWS2ViewOnChange) {
             $this->updateWS2Table();
         }
+        dispatch(new GenerateThemePreview($this));
     }
 
     public function postDelete()
@@ -860,6 +869,7 @@ class FluidbookTheme extends CubistMagicAbstractModel
         }
     }
 
+
     protected static function _unserialize($str)
     {
         $class = 'stdClass';
diff --git a/resources/js/social_screenshot/social_screenshot.js b/resources/js/social_screenshot/social_screenshot.js
new file mode 100644 (file)
index 0000000..d9e4184
--- /dev/null
@@ -0,0 +1,26 @@
+const puppeteer = require('puppeteer');
+const commandLineArgs = require('command-line-args');
+const optionDefinitions = [
+    {name: 'url', type: String},
+    {name: 'dest', type: String},
+    {name: 'delay', type: Number, defaultOption: 10},
+    {name: 'scale', type: Number, defaultOption: 1},
+    {name: 'width', type: Number, defaultOption: 1920},
+    {name: 'height', type: Number, defaultOption: 1080}
+];
+
+(async () => {
+    const options = commandLineArgs(optionDefinitions);
+    const browser = await puppeteer.launch({headless: true});
+    const page = await browser.newPage();
+    page.on('console', msg => console.log('PAGE LOG:', msg._text));
+    await page.setViewport({
+        width: options.width / options.scale,
+        height: options.height / options.scale,
+        deviceScaleFactor: options.scale,
+    });
+    await page.goto(options.url);
+    await new Promise(r => setTimeout(r, 1000 * options.delay));
+    await page.screenshot({path: options.dest, type: 'jpeg', quality: 95});
+    await browser.close();
+})();
\ No newline at end of file