Пример #1
0
 public static function downloadFile(File $file)
 {
     $path = $file->getFilePath();
     #Create the directory
     $basepath = self::$telegram->getDownloadPath();
     $loc_path = $basepath . '/' . $path;
     $dirname = dirname($loc_path);
     if (!is_dir($dirname)) {
         if (!mkdir($dirname, 0755, true)) {
             throw new TelegramException('Directory ' . $dirname . ' cant be created');
         }
     }
     // open file to write
     $fp = fopen($loc_path, 'w+');
     if ($fp === false) {
         throw new TelegramException('File cant be created');
     }
     $ch = curl_init();
     if ($ch === false) {
         throw new TelegramException('Curl failed to initialize');
     }
     $curlConfig = array(CURLOPT_URL => 'https://api.telegram.org/file/bot' . self::$telegram->getApiKey() . '/' . $path, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => 0, CURLOPT_BINARYTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_FILE => $fp);
     curl_setopt_array($ch, $curlConfig);
     $result = curl_exec($ch);
     if ($result === false) {
         throw new TelegramException(curl_error($ch), curl_errno($ch));
     }
     // close curl
     curl_close($ch);
     // close local file
     fclose($fp);
     if (filesize($loc_path) > 0) {
         return true;
     } else {
         return false;
     }
 }
Пример #2
0
 /**
  * Download file
  *
  * @param \Longman\TelegramBot\Entities\File $file
  *
  * @return boolean
  * @throws \Longman\TelegramBot\Exception\TelegramException
  */
 public static function downloadFile(File $file)
 {
     $tg_file_path = $file->getFilePath();
     $file_path = self::$telegram->getDownloadPath() . '/' . $tg_file_path;
     $file_dir = dirname($file_path);
     //For safety reasons, first try to create the directory, then check that it exists.
     //This is in case some other process has created the folder in the meantime.
     if (!@mkdir($file_dir, 0755, true) && !is_dir($file_dir)) {
         throw new TelegramException('Directory ' . $file_dir . ' can\'t be created');
     }
     $debug_handle = TelegramLog::getDebugLogTempStream();
     try {
         self::$client->get('/file/bot' . self::$telegram->getApiKey() . '/' . $tg_file_path, ['debug' => $debug_handle, 'sink' => $file_path]);
         return filesize($file_path) > 0;
     } catch (RequestException $e) {
         return (string) $e->getResponse()->getBody();
     } finally {
         //Logging verbose debug output
         TelegramLog::endDebugLogTempStream("Verbose HTTP File Download Request output:\n%s\n");
     }
 }
Пример #3
0
 /**
  * Download file
  *
  * @param Entities\File $file
  *
  * @return boolean
  */
 public static function downloadFile(File $file)
 {
     $path = $file->getFilePath();
     //Create the directory
     $loc_path = self::$telegram->getDownloadPath() . '/' . $path;
     $dirname = dirname($loc_path);
     if (!is_dir($dirname) && !mkdir($dirname, 0755, true)) {
         throw new TelegramException('Directory ' . $dirname . ' can\'t be created');
     }
     $debug_handle = TelegramLog::getDebugLogTempStream();
     try {
         $response = self::$client->get('/file/bot' . self::$telegram->getApiKey() . '/' . $path, ['debug' => $debug_handle, 'sink' => $loc_path]);
     } catch (RequestException $e) {
         throw new TelegramException($e->getMessage());
     } finally {
         //Logging verbose debug output
         TelegramLog::endDebugLogTempStream("Verbose HTTP File Download Request output:\n%s\n");
     }
     return filesize($loc_path) > 0;
 }
Пример #4
0
 /**
  *
  * Testing getFilePath without data
  *
  */
 public function testGetFilePathWithoutData()
 {
     unset($this->data['file_path']);
     $file = new File($this->data);
     $path = $file->getFilePath();
     $this->assertNull($path);
 }