Return GraphImage to include more metadata (#14307)

* Return GraphImage to include more metadata
Allows things like including title.
Implements __toString for backwards compatability
getImageData to allow controlling the output through flags

* Style and Lint
This commit is contained in:
Tony Murray
2022-09-06 07:33:57 -05:00
committed by GitHub
parent 69d1c2022a
commit 9fdc213f25
5 changed files with 191 additions and 64 deletions

View File

@@ -18,25 +18,35 @@ class GraphController extends Controller
public function __invoke(Request $request, string $path = ''): Response
{
$vars = array_merge(Url::parseLegacyPathVars($request->path()), $request->except(['username', 'password']));
$vars['graph_type'] = $vars['graph_type'] ?? Config::get('webui.graph_type');
$output = $vars['graph_type'] ?? Config::get('webui.graph_type', 'default');
if (\Auth::check()) {
// only allow debug for logged in users
Debug::set(! empty($vars['debug']));
}
$headers = [
'Content-type' => Graph::imageType($vars['graph_type']),
];
try {
return response(Graph::get($vars), 200, Debug::isEnabled() ? [] : $headers);
$graph = Graph::get($vars);
if (Debug::isEnabled()) {
return response('<img src="' . $graph->inline() . '" alt="graph" />');
}
$headers = [
'Content-type' => $graph->imageType(),
];
if ($output == 'base64') {
return response($graph, 200, $headers);
}
return response($graph->data(), 200, $headers);
} catch (RrdGraphException $e) {
if (Debug::isEnabled()) {
throw $e;
}
return response($e->generateErrorImage(), 500, $headers);
return response($e->generateErrorImage(), 500, ['Content-type' => Graph::imageType()]);
}
}
}

View File

@@ -81,7 +81,7 @@ class AppServiceProvider extends ServiceProvider
});
Blade::directive('graphImage', function ($vars, $flags = 0) {
return "<?php echo \LibreNMS\Util\Graph::get($vars, $flags); ?>";
return "<?php echo \LibreNMS\Util\Graph::getImageData($vars, $flags); ?>";
});
}