3

Within my Symfony application I need to do several operation with files: list of files from a directory, decrypt them using gpg, parse the output with an external software and encrypt again.

My first question is: is this the right approach for this problem? On another scenario, I'd have written bash/python scripts to do this, but since info (user ids, passphrases, etc) is read from a Symfony API I though it was quite convenient to embed the calls into the application.

My second question is more specific: is there any way to efficiently handle the command line outputs and errors? For instance, when I call 'ls' how can easily convert the output into an array of file names?

private function decryptAction()
{
    $user_data_source = '/Users/myuser/datafiles/';

    // Scan directory and get a list of all files
    $process = new Process('ls ' . $user_data_source);

    try {
        $process->mustRun();
        $files = explode(' ', $process->getOutput());

        return $files;
    } catch (ProcessFailedException $e) {
        return $e->getMessage();
    }
}
luchaninov
  • 6,792
  • 6
  • 60
  • 75
MarcSitges
  • 282
  • 3
  • 11

2 Answers2

3

Found the answer for my second question, but I am still very interested in your thoughts about the entire approach.

// Scan directory and get a list of all files
        $process = new Process('ls -1 ' . $user_data_source);

        try {
            $process->mustRun();
            $files = array_filter( explode("\n", $process->getOutput()), 'strlen');
            return $files;
        } catch (ProcessFailedException $e) {
            return $e->getMessage();
        }
MarcSitges
  • 282
  • 3
  • 11
0

Unless you really need an immediate response from the call, this kind of tasks are better left to a background process.

So what I would do is write one or more Symfony commands that perform the described processes (read, decrypt, and so on).

Those processes can be executed via crontab, or "daemonized" via another scheduler like Supervisord.

Then, the API call only creates some kind of "semaphore" file that triggers the actual execution, or even better you can use some kind of queue system.

Francesco Abeni
  • 4,190
  • 1
  • 19
  • 30