Skip to content

Commit

Permalink
remove params, now for working correctly the media_manager.php file m…
Browse files Browse the repository at this point in the history
…ust be changed which shloud ensure correct function of the caching mechanism
  • Loading branch information
AndiLeni committed Aug 5, 2023
1 parent a2f1509 commit 3934c50
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 81 deletions.
37 changes: 0 additions & 37 deletions boot.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,5 @@
<?php

use media_negotiator\Helper;

function negotiateFormat($ep)
{

// rex_media_manager object
$subject = $ep->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);
Expand Down
2 changes: 1 addition & 1 deletion lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Helper

public static function getOutputFormat($requestedTypes): string
{
$possibleFormat = "";
$possibleFormat = "default";

$imagickFormats = [];

Expand Down
43 changes: 0 additions & 43 deletions lib/rex_effect_negotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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,
],
];
}
}
2 changes: 2 additions & 0 deletions package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
85 changes: 85 additions & 0 deletions pages/setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

$func = rex_post("func", "string", null);

if ($func == "replaceMM") {

$originalFilePath = rex_path::addon("media_manager", "lib/media_manager.php");
$originalSource = file($originalFilePath);
$newFunctionCode = rex_file::get(rex_path::addon("media_negotiator", "/tpl/getCacheFilename.php"));


// Create a ReflectionClass instance for the given class name
$reflectionClass = new ReflectionClass("rex_media_manager");
$reflectionMethod = $reflectionClass->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.");
}

?>

<h1>Setup:</h1>

<p>
Die Datei <code>/redaxo/src/addons/media_manager/lib/media_manager.php</code> muss angepasst werden.
</p>
<p>
Dies kann <a href="#manuell">manuell</a> gemacht werden, oder man kann die Datei <a href="#automatisch">automatisch</a> ersetzen lassen.
</p>



<h2 id="manuell">Manuell</h2>
<p>
Bitte manuell die Datei öffnen und die Funktion <code>getCacheFilename()</code> in Folgendes ändern:
</p>
<pre>
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;
}
}
</pre>


<h2 id="automatisch">Automatisch</h2>
<p>
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).
</p>

<form method="post">
<input type="hidden" name="func" value="replaceMM">
<button class="btn btn-primary" type="submit">Ersetzen</button>
</form>

<p>
<?= rex_view::info("Diese Anpassung muss nach jedem Update des Media Managers oder Redaxo Updates erneut gemacht werden, da die Datei in ihren ursprünglichen Zustand versetzt wird beim updaten.") ?>
</p>
20 changes: 20 additions & 0 deletions tpl/getCacheFilename.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 3934c50

Please sign in to comment.