/** * @param CurlWrapper $curl * @param string $dir * * @throws FileNotWritableException * @return CurlWrapper */ public static function prepareDownload(CurlWrapper $curl, $dir) { do { $fileName = md5($curl->getUrl()) . '.' . Strings::random() . '.tmp'; } while (is_file($dir . '/' . $fileName)); if (($fileHandle = @fopen($curl->file = $dir . '/' . $fileName, 'wb')) === FALSE) { throw new FileNotWritableException("File {$curl->file} is not writable."); } if (($headersHandle = @fopen($curl->file . '.headers', 'wb')) === FALSE) { throw new FileNotWritableException("File {$curl->file} is not writable."); } return $curl->setOptions(array('file' => $fileHandle, 'writeHeader' => $headersHandle, 'binaryTransfer' => TRUE)); }
/** * Creates CurlWrapper from given Request object. * * @param Request $request * @param string $requestId * @throws DirectoryNotWritableException * @return CurlWrapper */ protected function initRequest(Request $request, &$requestId) { // combine setup $request->options += $this->options; $request->headers += $this->headers; // cookies if ($request->cookies) { $request->headers['Cookie'] = $request->getCookies(); } // wrap $curl = new CurlWrapper($request->getUrl(), $request->method); $curl->setOptions($request->options); $curl->setHeaders($request->headers); $curl->setPost($request->post, $request->files); // fallback when safe_mode if (!$this->canFollowRedirect()) { $curl->setOption('followLocation', NULL); } // method & prepare download if ($request->isMethod(Request::DOWNLOAD)) { if (!is_dir($this->downloadDir)) { throw new DirectoryNotWritableException("Please provide a writable directory for download."); } FileResponse::prepareDownload($curl, $this->downloadDir); } else { $curl->setOption('header', TRUE); } // logging if ($this->logger) { $requestId = $this->logger->request($request); } return $curl; }