private function handleTorrent($filename, &$retval) { //解析该种子 $torrent = file_get_contents($filename); /** @var TorrentFileTool $obj */ $obj = null; try { $obj = TorrentFileTool::getProcessedObj($torrent); } catch (BEncoderException $ex) { $obj = null; return false; } /** @var Seed|null $seedExists */ $seedExists = Seed::findOne(['info_hash' => $obj->getInfoHash()]); $seed = null; if ($seedExists) { if (!$seedExists->is_valid) { //TODO 参数化配置是否允许删除后重新上传 if (true) { $seed = $seedExists; } else { $seedExists->addError('info_hash', 'seed invalid'); $retval = 'invalid'; return $seedExists; } } else { $seedExists->addError('info_hash', 'seed exists'); $retval = 'exists'; return $seedExists; } } else { $seed = new Seed(); } file_put_contents($this->getTorrentFilePath($obj->getInfoHash()), $obj->getInfoEncoded()); $seed->file_count = $obj->file_count; $seed->file_size = $obj->total_size; $seed->info_hash = $obj->getInfoHash(); $seed->torrent_name = $obj->getTorrentName(); $seed->publisher_user_id = Yii::$app->user->identity->getId(); $seed->detail_info = $this->proc_obj->getDetail(); $seed->full_name = $this->proc_obj->generateSubject(); $seed->is_valid = true; $seed->type_id = $this->type_id; $seed->sub_type_id = $this->sub_type_id; $seed->save(); $retval = 'succeed'; return $seed; }
public function actionDownload($seed_id) { $seed = Seed::findOne($seed_id); if (empty($seed)) { return ['result' => 'failed', 'extra' => 'no such seed']; } $main_tracker = "http://10.254.36.2:8080/announce.php?"; //鉴于announce_list实际上是扩展,经过实验,如果有announce_list, //announce就不再起作用,所以把main_tracker放到list的第一个 $secondary_tracker = [$main_tracker, "http://10.254.36.2:9001/announce.php?", "http://tracker1.bevip.xyz:8081/announce.php?", "http://tracker2.bevip.xyz:8082/announce.php?", "http://tracker3.bevip.xyz:8083/announce.php?", "http://tracker4.bevip.xyz:8084/announce.php?"]; $info_path = getcwd() . "/torrents/" . $seed['info_hash'] . '.info'; $torrent = TorrentFileTool::buildTorrentFile($info_path, $main_tracker, $secondary_tracker); $event = new SeedEvent(); $event->seed_id = $seed_id; $event->user_id = Yii::$app->user->identity->getId(); $event->event_type = 'Downloaded'; $event->insert(); return Yii::$app->response->sendContentAsFile($torrent, '[NGPT]' . $seed['torrent_name'] . '.torrent'); }