From ce225a7a6773825616e5d7170a0c2cf3f7722e0e Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 21 Jul 2021 14:59:28 +0200 Subject: [PATCH] wip #4210 @2 --- .../API/FluidbookThemeAPIController.php | 2 - app/Jobs/GenerateThemePreview.php | 2 +- app/Jobs/UpdateWS2ThemeTable.php | 177 ++++++++++++++++++ app/Models/FluidbookTheme.php | 149 +-------------- 4 files changed, 188 insertions(+), 142 deletions(-) create mode 100644 app/Jobs/UpdateWS2ThemeTable.php diff --git a/app/Http/Controllers/API/FluidbookThemeAPIController.php b/app/Http/Controllers/API/FluidbookThemeAPIController.php index 42bc4618c..5dfbd4ba4 100644 --- a/app/Http/Controllers/API/FluidbookThemeAPIController.php +++ b/app/Http/Controllers/API/FluidbookThemeAPIController.php @@ -50,8 +50,6 @@ class FluidbookThemeAPIController extends Controller } $theme->setAttribute($k, $v); } - - return $theme; } diff --git a/app/Jobs/GenerateThemePreview.php b/app/Jobs/GenerateThemePreview.php index ff3ec14ef..332b1df38 100644 --- a/app/Jobs/GenerateThemePreview.php +++ b/app/Jobs/GenerateThemePreview.php @@ -49,7 +49,7 @@ class GenerateThemePreview implements ShouldQueue $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('delay', 4); $cl->setArg('scale', 1); $cl->setArg('dest', $dest); $cl->setArg('url', $this->theme->getPreviewURL(['shortLoading'=>1])); diff --git a/app/Jobs/UpdateWS2ThemeTable.php b/app/Jobs/UpdateWS2ThemeTable.php new file mode 100644 index 000000000..9c94a739d --- /dev/null +++ b/app/Jobs/UpdateWS2ThemeTable.php @@ -0,0 +1,177 @@ +getFields() as $field) { + $name = $field->getAttribute('name'); + $allFields[] = $name; + if ($field instanceof Files) { + $fileFields[] = $name; + } else if ($field instanceof Color) { + if ($field->getAttribute('allows_alpha')) { + $colorAlphaFields[] = $name; + } else { + $colorFields[] = $name; + } + } + } + $ignore = ['id', 'name', 'owner', 'created_at', 'deleted_at', 'updated_at', 'slug']; + $t3dir = '/data1/extranet/www/fluidbook/themes3/'; + + $data = []; + foreach (FluidbookTheme::all() as $theme) { + $settings = []; + foreach ($allFields as $k) { + if (in_array($k, $ignore)) { + continue; + } + $v = $theme->$k; + + if (in_array($k, $colorAlphaFields)) { + $v = self::colorAlphaToWS2($v); + } else if (in_array($k, $colorFields)) { + $v = self::colorToWS2($v); + } else if (in_array($k, $fileFields)) { + if (null === $v) { + $v = ''; + } else { + /** @var Media $media */ + $media = $theme->getMedia($v)->get(0); + if (null !== $media) { + $v = $media->getAttribute('file_name'); + $dir = $t3dir . $theme->id; + if (!file_exists($dir)) { + mkdir($dir, 0777, true); + } + $dest = $dir . '/' . $v; + $path = $media->getPath(); + $exists = file_exists($dest); + if (!$exists || realpath($dest) !== realpath($path)) { + if ($exists) { + unlink($dest); + } + `ln -s $path $dest`; + } + } else { + $v = ''; + } + } + } + if (null === $v) { + continue; + } + $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 -sf $preview $dest`; + } + } + $t = DB::table('extranet_clean.ws3_theme'); + $t->truncate(); + $t->insert($data); + + } + + public static function colorAlphaToWS2($data) + { + if ($data === '') { + return null; + } + if (!isset(self::$_colorAlphaToWS2Cache[$data])) { + self::$_colorAlphaToWS2Cache[$data] = self::_colorToWS2($data, true); + } + return self::$_colorAlphaToWS2Cache[$data]; + } + + public static function colorToWS2($data) + { + if ($data === '') { + return ''; + } + if (!isset(self::$_colorToWS2Cache[$data])) { + self::$_colorToWS2Cache[$data] = self::_colorToWS2($data, false); + } + return self::$_colorToWS2Cache[$data]; + } + + protected static function _colorToWS2($data, $alpha): string + { + $data = trim($data, '# '); + if (strlen($data) === 6) { + $res = $data; + } else if ($data === 'transparent') { + $res = '00000000'; + } else if (preg_match('/rgba?\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})(\s*,\s*([0-9.]*))?\)/', $data, $matches)) { + $res = self::dechex($matches[1]) . self::dechex($matches[2]) . self::dechex($matches[3]); + if (isset($matches[5])) { + $res = self::dechex($matches[5] * 255) . $res; + } + } else { + $res = $data; + } + if ($alpha && strlen($res) === 6) { + $res = 'ff' . $res; + } + return $res; + } + + public static function dechex($e) + { + if ($e == 0) { + $res = '00'; + } else { + $res = dechex(round($e)); + if (strlen($res) == 1) { + $res = '0' . $res; + } + } + return $res; + } + +} diff --git a/app/Models/FluidbookTheme.php b/app/Models/FluidbookTheme.php index 7c505551e..2bba683eb 100644 --- a/app/Models/FluidbookTheme.php +++ b/app/Models/FluidbookTheme.php @@ -6,6 +6,7 @@ namespace App\Models; use App\Fields\User; use App\Jobs\GenerateThemePreview; +use App\Jobs\UpdateWS2ThemeTable; use Cubist\Backpack\Magic\Fields\Color; use Cubist\Backpack\Magic\Fields\Files; use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel; @@ -19,9 +20,9 @@ class FluidbookTheme extends CubistMagicAbstractModel 'singular' => 'theme', 'plural' => 'themes']; - protected static $_colorToWS2Cache = []; + protected static $_colorToWS3Cache = []; - protected static $_colorAlphaToWS2Cache = []; + protected $_oldRoot = '/home/extranet/www/fluidbook/'; protected $_enableTrackNonDefaultValues = true; @@ -602,82 +603,6 @@ class FluidbookTheme extends CubistMagicAbstractModel $this->addMediaToField($fieldname, $path, true); } - public function updateWS2Table() - { - $fileFields = []; - $colorFields = []; - $colorAlphaFields = []; - $allFields = []; - foreach ($this->getFields() as $field) { - $name = $field->getAttribute('name'); - $allFields[] = $name; - if ($field instanceof Files) { - $fileFields[] = $name; - } else if ($field instanceof Color) { - if ($field->getAttribute('allows_alpha')) { - $colorAlphaFields[] = $name; - } else { - $colorFields[] = $name; - } - } - } - $ignore = ['id', 'name', 'owner', 'created_at', 'deleted_at', 'updated_at', 'slug']; - $t3dir = '/data1/extranet/www/fluidbook/themes3/'; - - $data = []; - foreach (FluidbookTheme::all() as $theme) { - $settings = []; - foreach ($allFields as $k) { - if (in_array($k, $ignore)) { - continue; - } - $v = $theme->$k; - - if (in_array($k, $colorAlphaFields)) { - $v = self::_colorAlphaToWS2($v); - } else if (in_array($k, $colorFields)) { - $v = self::_colorToWS2($v); - } else if (in_array($k, $fileFields)) { - if (null === $v) { - $v = ''; - } else { - /** @var Media $media */ - $media = $theme->getMedia($v)->get(0); - if (null !== $media) { - $v = $media->getAttribute('file_name'); - $dir = $t3dir . $theme->id; - if (!file_exists($dir)) { - mkdir($dir, 0777, true); - } - $dest = $dir . '/' . $v; - $path = $media->getPath(); - $exists = file_exists($dest); - if (!$exists || realpath($dest) !== realpath($path)) { - if ($exists) { - unlink($dest); - } - `ln -s $path $dest`; - } - } - } - } - if (null === $v) { - continue; - } - $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(); - $t->insert($data); - } public static function _colorToWS3($data) { @@ -701,77 +626,23 @@ class FluidbookTheme extends CubistMagicAbstractModel return self::$_colorToWS3Cache[$data]; } - public static function _colorAlphaToWS2($data) - { - if ($data === '') { - return null; - } - if (!isset(self::$_colorAlphaToWS2Cache[$data])) { - self::$_colorAlphaToWS2Cache[$data] = self::__colorToWS2($data, true); - } - return self::$_colorAlphaToWS2Cache[$data]; - } - - public static function _colorToWS2($data) - { - if ($data === '') { - return ''; - } - if (!isset(self::$_colorToWS2Cache[$data])) { - self::$_colorToWS2Cache[$data] = self::__colorToWS2($data, false); - } - return self::$_colorToWS2Cache[$data]; - } - - protected static function __colorToWS2($data, $alpha) - { - $data = trim($data, '# '); - if (strlen($data) === 6) { - $res = $data; - } else if ($data === 'transparent') { - $res = '00000000'; - } else if (preg_match('/rgba?\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})(\s*,\s*([0-9.]*))?\)/', $data, $matches)) { - $res = self::dechex($matches[1]) . self::dechex($matches[2]) . self::dechex($matches[3]); - if (isset($matches[5])) { - $res = self::dechex($matches[5] * 255) . $res; - } - } else { - $res = $data; - } - if ($alpha && strlen($res) === 6) { - $res = 'ff' . $res; - } - return $res; - } - - public static function dechex($e) - { - if ($e == 0) { - $res = '00'; - } else { - $res = dechex(round($e)); - if (strlen($res) == 1) { - $res = '0' . $res; - } - } - return $res; - } - - public function postSave() { parent::postSave(); - if (self::$updateWS2ViewOnChange) { - $this->updateWS2Table(); - } + self::updateWS2Table(); dispatch(new GenerateThemePreview($this))->onQueue('theme'); } public function postDelete() { parent::postDelete(); + self::updateWS2Table(); + } + + public static function updateWS2Table() + { if (self::$updateWS2ViewOnChange) { - $this->updateWS2Table(); + dispatch(new UpdateWS2ThemeTable())->onConnection('sync'); } } -- 2.39.5