OPNsense Forum
English Forums => Development and Code Review => Topic started by: mihak on March 08, 2021, 12:22:59 am
-
I would like to create and append data to a .CSV from a Python backend script; I'd also like to allow users to download the CSV.
Where should I place the CSV file on OPNSenese so my Python code (under default access rights) can write to it?
Also, how do I modiy controller (and view?) of my plugin so I can generate a downloadable link to CSV?
I achieved what I wanted on my dev instance of OPNSenese by manually chmod-ing write permissions to a folder within /usr/local/opnsense - but that is not a viable secure option. 😊
For the context: an hourly task runs speedtest and writes (appends) timestamp, latency, ul and dl speeds into a csv file. The csv file is then used to calculate aveages on the dashboard and to be downloadable for further analysis.
-
This plugin does the same - it just allows you to download a predefined file:
https://github.com/opnsense/plugins/tree/master/sysutils/api-backup
With that, your file just needs to be readable by the web interface, which is afaik still running as root.
-
to close the thread (and for future reference for budding devs):
- create a new DownloadController.php in mvc/app/controllers/OPNsense/<plugin>/Api
- the downloadable file is then available at /api/<plugin>/download/csv
namespace OPNsense\Speedtest\Api;
use OPNsense\Base\ApiControllerBase;
class DownloadController extends ApiControllerBase
{
const DATA_CSV = '/usr/local/opnsense/scripts/OPNsense/<plugin>/filename.csv';
public function csvAction()
{
$this->response->setStatusCode(200, "OK");
$this->response->setContentType('text/csv', 'UTF-8');
$this->response->setHeader("Content-Disposition", "attachment; filename=\"filename.csv\"");
$data = file_get_contents(self::DATA_CSV);
$this->response->setContent($data);
}
public function afterExecuteRoute($dispatcher)
{
$this->response->send();
}
}