/**
  * Завантажити torrent файл
  * @param Torrent $torrent
  */
 public static function downloadFile(Torrent $torrent)
 {
     header("Content-type: application/torrent");
     header("Expires: 0");
     header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
     header("Pragma: public");
     header("Content-Disposition: attachment; filename=" . $torrent->getInfo()->getName() . '.torrent');
     echo $torrent->getBinaryTorrent();
 }
 /**
  * Кодує дані в torrent файл
  * @param Torrent $torrent
  * @return string
  */
 public static function encode(Torrent $torrent)
 {
     $torrentArray = array();
     $data = $torrent->getData();
     foreach ($data as $itemKey => $item) {
         switch ($itemKey) {
             case 'announce-list':
                 foreach ($item as $announce) {
                     if ($announce) {
                         $torrentArray[$itemKey][][0] = $announce;
                     }
                 }
                 break;
             case 'url-list':
                 foreach ($item as $url) {
                     if ($url) {
                         $torrentArray[$itemKey][] = $url;
                     }
                 }
                 break;
             case 'info':
                 if (empty($item)) {
                     continue;
                 }
                 $torrentArray[$itemKey] = array();
                 foreach ($item as $infoKeyItem => $itemInfo) {
                     switch ($infoKeyItem) {
                         case 'files':
                             $torrentArray[$itemKey][$infoKeyItem] = array();
                             foreach ($itemInfo as $fileItem) {
                                 $newFileItem = array();
                                 foreach ($fileItem as $keyAttrFileItem => $attrFileItem) {
                                     switch ($keyAttrFileItem) {
                                         case 'path':
                                         case 'length':
                                         default:
                                             $newFileItem[$keyAttrFileItem] = $attrFileItem;
                                     }
                                 }
                                 $torrentArray[$itemKey][$infoKeyItem][] = $newFileItem;
                             }
                             break;
                         default:
                             $torrentArray[$itemKey][$infoKeyItem] = $itemInfo;
                     }
                 }
                 break;
             default:
                 if ($item) {
                     $torrentArray[$itemKey] = $item;
                 }
         }
     }
     $data = \Rych\Bencode\Bencode::encode($torrentArray);
     return $data;
 }
 /**
  * Встановити торент для моделі
  * @param Torrent $torrent
  */
 public function setTorrent(Torrent $torrent)
 {
     self::$torrent = $torrent;
     $path = PROJECT_PATH . '/' . self::PATH_SAVE_FILES;
     $this->id = $torrent->getHash();
     FileHelper::saveFile($this->id, $torrent, $path);
     $this->torrentToSelfModel();
 }