Graph API use new code path (#14493)

* Graph API use new code path
Improves error handling significantly

* Actually base64 encode it
This commit is contained in:
Tony Murray 2022-10-22 14:58:51 -05:00 committed by GitHub
parent 04f0574132
commit aefd14d3aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 21 deletions

View File

@ -33,8 +33,8 @@ use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use LibreNMS\Alerting\QueryBuilderParser;
use LibreNMS\Config;
use LibreNMS\Data\Store\Datastore;
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\Util\Graph;
use LibreNMS\Util\IP;
use LibreNMS\Util\IPv4;
use LibreNMS\Util\Number;
@ -87,29 +87,17 @@ function api_not_found()
function api_get_graph(array $vars)
{
global $dur; // Needed for callback within graph code
try {
$graph = Graph::get($vars);
$auth = true;
if ($vars['output'] === 'base64') {
return api_success(['image' => $graph->base64(), 'content-type' => $graph->imageType()], 'image');
}
// prevent ugly error for undefined graphs from being passed to the user
[$type, $subtype] = extract_graph_type($vars['type']);
if (! is_file(base_path("includes/html/graphs/$type/auth.inc.php"))) {
return api_error(400, 'Invalid graph type');
return response($graph->data(), 200, ['Content-Type' => $graph->imageType()]);
} catch (\LibreNMS\Exceptions\RrdGraphException $e) {
return api_error(500, $e->getMessage());
}
ob_start();
include 'includes/html/graphs/graph.inc.php';
Datastore::terminate();
$image = ob_get_contents();
ob_end_clean();
if ($vars['output'] === 'base64') {
return api_success(['image' => $image, 'content-type' => get_image_type(Config::get('webui.graph_type'))], 'image');
}
return response($image, 200, ['Content-Type' => get_image_type(Config::get('webui.graph_type'))]);
}
function check_bill_permission($bill_id, $callback)

View File

@ -6,3 +6,5 @@ if (is_numeric($vars['id']) && ($auth || bill_permitted($vars['id']))) {
$bill = dbFetchRow('SELECT * FROM `bills` WHERE bill_id = ?', [$bill_id]);
$auth = true;
}
global $dur;