Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
refactor(torrents/upload): check info_hash in valid function but not …
Browse files Browse the repository at this point in the history
…flush
  • Loading branch information
Rhilip committed Aug 6, 2019
1 parent 4b617ca commit 32967c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
35 changes: 18 additions & 17 deletions apps/models/form/Torrents/UploadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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**
Expand All @@ -238,31 +239,31 @@ 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');
}

/**
* @throws \Exception
*/
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 {
Expand Down
8 changes: 6 additions & 2 deletions migration/ridpt.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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',
Expand All @@ -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,
Expand Down

0 comments on commit 32967c4

Please sign in to comment.