private function downloadLinks(array $links) { $this->logger->info('Begin downloading ' . count($links) . ' files...'); foreach ($links as $name => $link) { //TODO check if filename exists! $filePath = $this->appConfiguration->getFilesSavePath() . '/' . $name . '_' . md5(rand()) . '.torrent'; $this->logger->debug("Downloading {$name} => {$link} to {$filePath}"); $fp = @fopen($filePath, 'w'); if (!$fp) { $this->logger->error("Downloading of {$link} failed - unable to create file {$filePath}"); continue; } $request = new Request('GET', $link); $job = new FetchJob($this->appConfiguration, $request); $job->setResponseStream($fp); $this->logger->debug("Executing job for {$link}"); try { if (!$job->execute()) { $this->logger->error("Downloading of {$link} failed - unknown error occurred"); continue; } } catch (\Exception $e) { $type = get_class($e); $this->logger->error("Downloading of {$link} failed - {$type}: " . $e->getMessage()); continue; } fclose($fp); $this->logger->info("Downloaded {$link} to {$filePath}"); } }
/** * Prepares cUrl to execute the job * * @throws UnsupportedFeatureException * @throws \RuntimeException */ private function prepareCUrl() { $this->cUrl = curl_init((string) $this->fetchRequest->getUri()); if ($this->cUrl === false) { throw new \RuntimeException('Failed to initialize cURL - internal error'); } $schemeType = substr($this->fetchRequest->getUri()->getScheme(), 0, 4); if ($schemeType === 'http') { $this->prepareHttpCUrl(); } elseif ($schemeType !== 'ftp' && $schemeType !== 'ftps') { throw new \RuntimeException('Your request uses unknown scheme ' . $this->fetchRequest->getUri()->getScheme() . ' (available schemes: http/https/ftp/ftps)'); } $acceptUnsafeCertsFlag = $this->appConfig->isAcceptUnsafeCertificates() ? 0 : 2; //I f*****g love curl... curl_setopt_array($this->cUrl, [CURLOPT_RETURNTRANSFER => 1, CURLOPT_FILE => $this->responseStream, CURLOPT_HEADER => 0, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_SSL_VERIFYPEER => $acceptUnsafeCertsFlag, CURLOPT_SSL_VERIFYHOST => $acceptUnsafeCertsFlag]); $userInfo = $this->fetchRequest->getUri()->getUserInfo(); if (!empty($userInfo)) { curl_setopt_array($this->cUrl, [CURLOPT_HTTPAUTH => CURLAUTH_ANY, CURLOPT_USERPWD => $userInfo]); } }
public function testConfigurationIsConsideredValidAfterSettingFilesSavePath() { $this->assertFalse($this->subjectUnderTest->isValid(), 'Configuration should not be valid for fresh object'); $this->subjectUnderTest->setFilesSavePath(sys_get_temp_dir()); $this->assertTrue($this->subjectUnderTest->isValid(), 'Configuration should be valid after setting files save path'); }