From 32967c424776fd0ed77db496c8aa28754f12abb7 Mon Sep 17 00:00:00 2001 From: Rhilip Date: Wed, 7 Aug 2019 00:07:46 +0800 Subject: [PATCH] refactor(torrents/upload): check info_hash in valid function but not flush --- apps/models/form/Torrents/UploadForm.php | 35 ++++++++++++------------ migration/ridpt.sql | 8 ++++-- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/apps/models/form/Torrents/UploadForm.php b/apps/models/form/Torrents/UploadForm.php index 72699bd..0d97f61 100644 --- a/apps/models/form/Torrents/UploadForm.php +++ b/apps/models/form/Torrents/UploadForm.php @@ -154,10 +154,10 @@ public static function inputRules() public static function callbackRules() { - return ['isValidTorrent']; + return ['isValidTorrentFile', 'makePrivateTorrent']; } - protected function isValidTorrent() + protected function isValidTorrentFile() { try { $this->torrent_dict = Bencode::load($this->getData('file')->tmpName); @@ -208,16 +208,17 @@ protected function isValidTorrent() $this->torrent_structure = $this->getFileTree(); } - public function makePrivateTorrent() + protected function makePrivateTorrent() { - $this->torrent_dict['announce'] = "https://" . config("base.site_tracker_url") . "/announce"; + $this->torrent_dict['announce'] = 'https://' . config('base.site_tracker_url') . '/announce'; // Remove un-need field in private torrents unset($this->torrent_dict['announce-list']); // remove multi-tracker capability unset($this->torrent_dict['nodes']); // remove cached peers (Bitcomet & Azareus) - // Some other change if you need - //$this->torrent_dict['commit'] = ""; + // Rewrite `commit` and `created by` if enabled this config + if (config('torrent_upload.rewrite_commit_to')) $this->torrent_dict['commit'] = config('torrent_upload.rewrite_commit_to'); + if (config('torrent_upload.rewrite_createdby_to')) $this->torrent_dict['created by'] = config('torrent_upload.rewrite_createdby_to'); /** * The following line requires uploader to re-download torrents after uploading **Since info_hash change** @@ -238,10 +239,18 @@ public function makePrivateTorrent() // Make it private and unique by add our source flag $this->torrent_dict['info']['private'] = 1; // add private tracker flag - $this->torrent_dict['info']['source'] = "Powered by [" . config("base.site_url") . "] " . config("base.site_name"); + $this->torrent_dict['info']['source'] = config('torrent_upload.rewrite_source_to') ?: 'Powered by [' . config('base.site_url') . '] ' . config('base.site_name'); // Get info_hash on new torrent content dict['info'] - $this->info_hash = pack("H*", sha1(Bencode::encode($this->torrent_dict['info']))); + $this->info_hash = pack('H*', sha1(Bencode::encode($this->torrent_dict['info']))); + + // Check if this torrent is exist or not before insert. + $count = app()->pdo->createCommand('SELECT COUNT(*) FROM torrents WHERE info_hash = :info_hash')->bindParams([ + 'info_hash' => $this->info_hash + ])->queryScalar(); + + // TODO redirect user to exist torrent details page when this torrent exist. + if ($count > 0) $this->buildCallbackFailMsg('Torrent','std_torrent_existed'); } /** @@ -249,20 +258,12 @@ public function makePrivateTorrent() */ public function flush() { - $this->makePrivateTorrent(); - $this->rewriteFlags(); - - // Check if this torrent is exist or not before insert. - $count = app()->pdo->createCommand('SELECT COUNT(*) FROM torrents WHERE info_hash = :info_hash')->bindParams([ - 'info_hash' => $this->info_hash - ])->queryScalar(); - if ($count > 0) throw new \Exception('std_torrent_existed'); - $nfo_blob = ''; if (isset($this->nfo)) { // FIXME it seem always be true ??? $nfo_blob = $this->nfo->getFileContent(); } + $this->rewriteFlags(); $this->determineTorrentStatus(); app()->pdo->beginTransaction(); try { diff --git a/migration/ridpt.sql b/migration/ridpt.sql index 2198bfe..111af31 100644 --- a/migration/ridpt.sql +++ b/migration/ridpt.sql @@ -3,7 +3,7 @@ -- https://www.phpmyadmin.net/ -- -- Host: 127.0.0.1 --- Generation Time: Aug 06, 2019 at 03:36 PM +-- Generation Time: Aug 07, 2019 at 12:06 AM -- Server version: 8.0.16 -- PHP Version: 7.3.7 @@ -778,6 +778,9 @@ INSERT INTO `site_config` (`name`, `value`) VALUES ('torrent_upload.enable_tags', '1'), ('torrent_upload.enable_teams', '1'), ('torrent_upload.enable_upload_nfo', '1'), +('torrent_upload.rewrite_commit_to', ''), +('torrent_upload.rewrite_createdby_to', ''), +('torrent_upload.rewrite_source_to', ''), ('tracker.cheater_check', '1'), ('tracker.enable_announce', '1'), ('tracker.enable_maxdlsystem', '1'), @@ -867,6 +870,7 @@ CREATE TABLE IF NOT EXISTS `snatched` ( `user_id` int(11) UNSIGNED NOT NULL, `torrent_id` int(11) UNSIGNED NOT NULL, `agent` varchar(60) NOT NULL, + `ip` varbinary(16) DEFAULT NULL, `port` smallint(5) UNSIGNED NOT NULL DEFAULT '0', `true_uploaded` bigint(20) UNSIGNED NOT NULL DEFAULT '0', `true_downloaded` bigint(20) UNSIGNED NOT NULL DEFAULT '0', @@ -876,7 +880,7 @@ CREATE TABLE IF NOT EXISTS `snatched` ( `seed_time` int(10) UNSIGNED NOT NULL DEFAULT '0', `leech_time` int(10) UNSIGNED NOT NULL DEFAULT '0', `finished` enum('yes','no') NOT NULL DEFAULT 'no', - `finish_ip` varchar(40) DEFAULT NULL, + `finish_ip` varbinary(16) DEFAULT NULL, `create_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `last_action_at` timestamp NULL DEFAULT NULL, `finish_at` timestamp NULL DEFAULT NULL,