/** * @param $data * @return array|null */ protected function prepareData($data) { if (empty($data)) { return null; } try { $data = Bencode::decode($data); } catch (RuntimeException $e) { return null; } if (!(isset($data['files']) && is_array($data['files']))) { return null; } $prepared = ['files' => []]; foreach ($data['files'] as $key => $value) { if (ctype_print($key)) { $prepared['files'][$key] = $value; } else { $prepared['files'][bin2hex($key)] = $value; } } return $prepared; }
public function getDownload($id) { $torrent = Torrent::find($id); if (!$torrent) { App::abort(404); } $torrent->downloads += 1; $torrent->save(); $decoded = Bencode::decode($torrent->file); $decoded['announce'] = Config::get('app.url') . '/announce/' . $torrent->id . '/' . Auth::user()->id; unset($decoded['announce-list']); $torrent->file = Bencode::encode($decoded); $response = Response::make($torrent->file, 200); $response->header('Content-Type', 'application/x-bittorrent'); // http://x-bittorrent.mime-application.com/ $response->header('Content-Disposition', 'attachment; filename=' . $torrent->name . '.torrent'); // http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http return $response; }
/** * Декодує дані з torrent файла * @param TorrentFile $torrentFile * @param $data */ public static function decode(TorrentFile $torrentFile, $data) { $torrent = $torrentFile->getTorrent(); $data = \Rych\Bencode\Bencode::decode($data); foreach ($data as $itemKey => $item) { switch ($itemKey) { case 'announce-list': foreach ($item as $announceItem) { $announce = new Announce(); $announce->setAnnounce($announceItem[0]); $torrent->addAnnounce($announce); } break; case 'comment': $torrent->setComment($item); break; case 'created by': $torrent->setCreated_by($item); break; case 'creation date': $torrent->setCreation_date($item); break; case 'encoding': $torrent->setEncoding($item); break; case 'rss': $torrent->setRss($item); break; case 'similar_torrents': $torrent->setSimilar_torrents($item); break; case 'url-list': foreach ($item as $urlItem) { $url = new Url(); $url->setUrl($urlItem); $torrent->addUrl($url); } break; case 'website': $torrent->setWebsite($item); break; case 'info': foreach ($item as $infoKeyItem => $itemInfo) { switch ($infoKeyItem) { case 'files': foreach ($itemInfo as $fileItem) { $file = new File(); $file->setLength($fileItem['length']); $file->setPath(implode('/', $fileItem['path'])); $torrent->getInfo()->addFile($file); } break; case 'name': $torrent->getInfo()->setName($itemInfo); break; case 'piece length': $torrent->getInfo()->setPiece_length($itemInfo); break; case 'pieces': $torrent->getInfo()->setPieces($itemInfo); break; case 'private': $torrent->getInfo()->setPrivate($itemInfo); break; case 'length': $torrent->getInfo()->setPrivate($itemInfo); break; default: $torrent->getInfo()->addOtherItem($infoKeyItem, $itemInfo); } } break; default: $torrent->addOtherItem($itemKey, $item); } } }