From 3934c50584f41a22c090e028296221ce28d70d56 Mon Sep 17 00:00:00 2001 From: Andreas Lenhardt Date: Sat, 5 Aug 2023 13:35:01 +0200 Subject: [PATCH] remove params, now for working correctly the media_manager.php file must be changed which shloud ensure correct function of the caching mechanism --- boot.php | 37 --------------- lib/Helper.php | 2 +- lib/rex_effect_negotiator.php | 43 ------------------ package.yml | 2 + pages/setup.php | 85 +++++++++++++++++++++++++++++++++++ tpl/getCacheFilename.php | 20 +++++++++ 6 files changed, 108 insertions(+), 81 deletions(-) create mode 100644 pages/setup.php create mode 100644 tpl/getCacheFilename.php diff --git a/boot.php b/boot.php index ec47982..bce4ff2 100644 --- a/boot.php +++ b/boot.php @@ -1,42 +1,5 @@ getSubject(); - - // check if the requested image has the 'negotiator' effect - $set_effects = $subject->effectsFromType($subject->getMediaType()); - $set_effects = array_column($set_effects, 'effect'); - - // if not, skip - if (!in_array('negotiator', $set_effects)) { - return $subject; - } else { - // if yes, set cache path - $possible_types = rex_server('HTTP_ACCEPT', 'string', ''); - $types = explode(',', $possible_types); - - // check which output type is technically possible - $possibleFormat = Helper::getOutputFormat($types); - - if ($possibleFormat === "avif") { - $subject->setCachePath($subject->getCachePath() . 'avif-'); - } elseif ($possibleFormat === "webp") { - $subject->setCachePath($subject->getCachePath() . 'webp-'); - } else { - $subject->setCachePath($subject->getCachePath() . 'default-'); - } - } - - return $subject; -} - - -rex_extension::register('MEDIA_MANAGER_BEFORE_SEND', "negotiateFormat"); if (rex_addon::get('media_manager')->isAvailable()) { rex_media_manager::addEffect(rex_effect_negotiator::class); diff --git a/lib/Helper.php b/lib/Helper.php index 160b1f8..fb0d433 100644 --- a/lib/Helper.php +++ b/lib/Helper.php @@ -12,7 +12,7 @@ class Helper public static function getOutputFormat($requestedTypes): string { - $possibleFormat = ""; + $possibleFormat = "default"; $imagickFormats = []; diff --git a/lib/rex_effect_negotiator.php b/lib/rex_effect_negotiator.php index c5fcbea..aa90682 100644 --- a/lib/rex_effect_negotiator.php +++ b/lib/rex_effect_negotiator.php @@ -22,11 +22,6 @@ public function execute() if ($possibleFormat === "avif") { - // check if file is cached, if yes return - if ($this->isCached($possibleFormat)) { - return; - } - // check if force_imagick enabled or imageavif not available, else use GD if (rex_config::get("media_negotiator", "force_imagick", false) || !function_exists('imageavif')) { // use Imagick @@ -46,10 +41,6 @@ public function execute() } } elseif ($possibleFormat === "webp") { - if ($this->isCached($possibleFormat)) { - return; - } - if (rex_config::get("media_negotiator", "force_imagick", false) || !function_exists('imagewebp')) { // use Imagick $img = $this->media->getSource(); @@ -67,38 +58,4 @@ public function execute() // do not change format and deliver original file } } - - private function isCached($targetFormat) - { - $cachePath = rex_path::addonCache('media_manager'); - $type = $this->params["mediatyp"]; - $originalFilename = $this->media->getMediaFilename(); - - $cacheFile = "{$cachePath}{$targetFormat}-{$type}/{$originalFilename}"; - - if (is_file($cacheFile)) { - return true; - } else { - return false; - } - } - - - public function getParams() - { - $effectId = rex_get("type_id", "int", 0); - $sql = rex_sql::factory(); - $res = $sql->setQuery("select name from " . rex::getTable("media_manager_type") . " where id = ?", [$effectId]); - $typName = $res->getValue("name"); - - return [ - [ - 'label' => "Name dieses Effekts", - 'name' => 'mediatyp', - 'type' => 'string', - 'notice' => "Hier bitte den Namen des Mediatyps eintragen. (Sollte korrekt vorausgefüllt sein)", - 'default' => $typName, - ], - ]; - } } diff --git a/package.yml b/package.yml index 18cb56a..594cd43 100644 --- a/package.yml +++ b/package.yml @@ -8,6 +8,8 @@ page: title: 'Media Negotiator' perm: media_negotiator[] subpages: + setup: + { title: 'Setup', icon: rex-icon } config: { title: 'Einstellungen', icon: rex-icon fa-cog } diff --git a/pages/setup.php b/pages/setup.php new file mode 100644 index 0000000..f8f8619 --- /dev/null +++ b/pages/setup.php @@ -0,0 +1,85 @@ +getMethod('getCacheFilename'); + + // get start and end lines of getCacheFilename() function + $startLine = $reflectionMethod->getStartLine(); + $endLine = $reflectionMethod->getEndLine(); + + $beforeFunc = implode("", array_slice($originalSource, 0, $startLine - 1)); + $afterFunc = implode("", array_slice($originalSource, $endLine)); + + + $newSource = $beforeFunc . $newFunctionCode . "\r\n" . $afterFunc; + + rex_file::put($originalFilePath, $newSource); + + + echo rex_view::success("Datei erfolgreich geändert."); +} + +?> + +

Setup:

+ +

+ Die Datei /redaxo/src/addons/media_manager/lib/media_manager.php muss angepasst werden. +

+

+ Dies kann manuell gemacht werden, oder man kann die Datei automatisch ersetzen lassen. +

+ + + +

Manuell

+

+ Bitte manuell die Datei öffnen und die Funktion getCacheFilename() in Folgendes ändern: +

+
+public function getCacheFilename()
+{
+    assert(null !== $this->cachePath);
+    assert(null !== $this->type);
+
+    $set_effects = $this->effectsFromType($this->getMediaType());
+    $set_effects = array_column($set_effects, 'effect');
+
+    if (!in_array('negotiator', $set_effects)) {
+        // do not change cache path
+        return $this->cachePath . $this->type . '/' . $this->originalFilename;
+    } else {
+        // change cache path for negotiator
+        $possible_types = rex_server('HTTP_ACCEPT', 'string', '');
+        $types = explode(',', $possible_types);
+        $possibleFormat = media_negotiator\Helper::getOutputFormat($types);
+
+        return $this->cachePath . $possibleFormat . "-" . $this->type . '/' . $this->originalFilename;
+    }
+}
+
+ + +

Automatisch

+

+ Alternativ kann dies auch mit einem Klick auf den folgenden Button gemacht werden (dies ersetzt die Funktion getCacheFilename() in der media_manager.php Datei durch eine Version mit der benötigten Anpassung). +

+ +
+ + +
+ +

+ +

\ No newline at end of file diff --git a/tpl/getCacheFilename.php b/tpl/getCacheFilename.php new file mode 100644 index 0000000..f83c98d --- /dev/null +++ b/tpl/getCacheFilename.php @@ -0,0 +1,20 @@ + public function getCacheFilename() + { + assert(null !== $this->cachePath); + assert(null !== $this->type); + + $set_effects = $this->effectsFromType($this->getMediaType()); + $set_effects = array_column($set_effects, 'effect'); + + if (!in_array('negotiator', $set_effects)) { + // do not change cache path + return $this->cachePath . $this->type . '/' . $this->originalFilename; + } else { + // change cache path for negotiator + $possible_types = rex_server('HTTP_ACCEPT', 'string', ''); + $types = explode(',', $possible_types); + $possibleFormat = media_negotiator\Helper::getOutputFormat($types); + + return $this->cachePath . $possibleFormat . "-" . $this->type . '/' . $this->originalFilename; + } + } \ No newline at end of file