/** * Processing one URL, opens pair of read-write streams and piping them * * @param AMQPClosure $closure A closure of message with another URL * @param EventLoop\LoopInterface $loop A loop, worker is run on * * @return bool Success flag * * @throws \Exception */ private function processURL(AMQPClosure $closure, EventLoop\LoopInterface $loop) { $readStream = @fopen($closure->get(), 'r'); if (!$readStream) { return $this->error($closure); } $tmpdir = \Bot::config('tmpdir'); if (!file_exists($tmpdir)) { if (!mkdir($tmpdir, 0777, true)) { throw new \Exception('Cannot create downloading dirname ' . $tmpdir); } } $fWritePath = $tmpdir . "/" . md5($closure->get()); $writeStream = fopen($fWritePath, 'w'); if (!$writeStream) { throw new \Exception('Cannot open downloading file ' . $fWritePath); } $this->files[$fWritePath] = true; $read = new Stream($readStream, $loop); $write = new Stream($writeStream, $loop); $write->on('end', function () use($closure, $fWritePath) { unset($this->files[$fWritePath]); $this->success($closure); }); $read->on('error', function () use($closure, $fWritePath) { unset($this->files[$fWritePath]); $this->error($closure); }); $read->pipe($write); }