OPNsense
  • Home
  • Help
  • Search
  • Login
  • Register

  • OPNsense Forum »
  • English Forums »
  • Development and Code Review (Moderator: fabian) »
  • Creating CSV - permissions
« previous next »
  • Print
Pages: [1]

Author Topic: Creating CSV - permissions  (Read 2612 times)

mihak

  • Jr. Member
  • **
  • Posts: 70
  • Karma: 5
    • View Profile
Creating CSV - permissions
« 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.
Logged

fabian

  • Moderator
  • Hero Member
  • *****
  • Posts: 2769
  • Karma: 200
  • OPNsense Contributor (Language, VPN, Proxy, etc.)
    • View Profile
    • Personal Homepage
Re: Creating CSV - permissions
« Reply #1 on: March 09, 2021, 09:53:47 pm »
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.
Logged

mihak

  • Jr. Member
  • **
  • Posts: 70
  • Karma: 5
    • View Profile
Re: Creating CSV - permissions
« Reply #2 on: March 18, 2021, 02:29:45 am »
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();
    }
}
« Last Edit: March 18, 2021, 02:32:01 am by mihak »
Logged

  • Print
Pages: [1]
« previous next »
  • OPNsense Forum »
  • English Forums »
  • Development and Code Review (Moderator: fabian) »
  • Creating CSV - permissions
 

OPNsense is an OSS project © Deciso B.V. 2015 - 2024 All rights reserved
  • SMF 2.0.19 | SMF © 2021, Simple Machines
    Privacy Policy
    | XHTML | RSS | WAP2