Empty file download

#6 · closed · 2 comments

View on GitHub ↗

gianmarcotoso

I've noticed that if you send a file created through the `write` method as the payload of a response, it's returned empty. This is because the gnuplot process is still running and has not yet closed the file descriptor (at least on my configuration, Ubuntu 14.04 running on VirtualBox). I've solved this by moving what's in the `__construct` and `__destruct` methods inside two new methods, and calling them at the end of the `write` function (as well as in the constructor and the destructor). Like this: ``` php public function write($terminal, $file) { $this->sendInit(); $this->sendCommand("set terminal $terminal size {$this->width}{$this->unit}, {$this->height}{$this->unit}"); $this->sendCommand('set output "'.$file.'"'); $this->plot(); // Cleanup and re-init $this->cleanup(); $this->initialize(); } ``` Of course, this means that all the commands need to be sent again after each `write`, so I'm checking in before making a pull request to make sure this is ok. On a sidenote, I've already added line width support for individual lines and I'll be pushing it alongside this :)

Comments

gianmarcotoso

Sorry, I didn't do my homework properly. I found that it can also be fixed by resetting gnuplot's output, like this: ``` php public function write($terminal, $file) { $this->sendInit(); $this->sendCommand("set terminal $terminal size {$this->width}{$this->unit}, {$this->height}{$this->unit}"); $this->sendCommand('set output "'.$file.'"'); $this->plot(); // Flush the output as described here: http://www.gnuplot.info/faq/faq.html#x1-840007.6 $this->sendCommand('set output'); usleep(5000); } ``` This is better because it allows to save, add stuff, save again to another file, etc.

gianmarcotoso

I've posted a pull request, this ticket is no longer relevant.