Skip to content

Commit

Permalink
Convert recentTrackers->last_used to an array (#2783)
Browse files Browse the repository at this point in the history
Previously, we were storing a string, and if a user created 20 torrents
in a row, it would add 20 trailing carriage returns (\r) to the	box.

We already do the logic	of stripping empty lines for other purposes,
lets reuse that here.

Add de-duplication of last used trackers (to match the list array).

Also eliminate $announce_list, because it's never used, and `grep -rn announce_list * | grep global`
indicates no other function is using it without it being passed.
  • Loading branch information
anthonyryan1 authored Nov 11, 2024
1 parent fc200ab commit 1d9dfb5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
34 changes: 17 additions & 17 deletions plugins/create/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class recentTrackers
public $hash = "rtrackers.dat";
public $modified = false;
public $list = array();
public $last_used = "";
public $last_used = array();
public $piece_size = 1024;
public $start_seeding = false;
public $private_torrent = false;
Expand All @@ -21,6 +21,14 @@ static public function load()
$cache = new rCache();
$rt = new recentTrackers();
$cache->get($rt);

// TODO: Temporary migration from string to array
// Can remove this after November 2026
if (is_string(($rt->last_used)))
{
$rt->last_used = [];
}

return($rt);
}
public function store()
Expand Down Expand Up @@ -51,6 +59,7 @@ public function get()
public function strip()
{
global $recentTrackersMaxCount;
$this->last_used = array_values( array_unique($this->last_used) );
$this->list = array_values( array_unique($this->list) );
$cnt = count($this->list);
$arr = array_values($this->list);
Expand Down Expand Up @@ -131,38 +140,29 @@ static public function getTrackerDomain($announce)
{
$rt = recentTrackers::load();
$trackers = array();
$announce_list = '';
if(isset($_REQUEST['trackers']))
{
$rt->last_used = $_REQUEST['trackers']; // remember trackers last used
$arr = explode("\r",$_REQUEST['trackers']);
$arr = explode("\r", $_REQUEST['trackers']);
foreach( $arr as $key => $value )
{
$value = trim($value);
if(strlen($value))
if(strlen($value) === 0)
{
$trackers[] = $value;
$rt->list[] = $value;
}
else
{
if(count($trackers)>0)
{
$announce_list .= (' -a '.escapeshellarg(implode(',',$trackers)));
$trackers = array();
}
continue;
}

$trackers[] = $value;
$rt->list[] = $value;
}
}
// remember checkbox and dropdown options
$rt->last_used = $trackers;
$rt->piece_size = $_REQUEST['piece_size'];
$rt->start_seeding = $_REQUEST['start_seeding'];
$rt->private_torrent = $_REQUEST['private'];
$rt->hybrid_torrent = $_REQUEST['hybrid'];
$rt->store();

if(count($trackers)>0)
$announce_list .= (' -a '.escapeshellarg(implode(',',$trackers)));
$piece_size = 262144;
if(isset($_REQUEST['piece_size']))
$piece_size = $_REQUEST['piece_size']*1024;
Expand Down
2 changes: 1 addition & 1 deletion plugins/create/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ plugin.onLangLoaded = function() {
theDialogManager.setHandler("tcreate", "beforeShow", () => {
if (plugin.recentTrackers) {
const recent = plugin.recentTrackers;
$("#trackers").val(recent.last_used);
$("#trackers").val(recent.last_used.join("\r\n"));
$(`#piece_size option[value=${recent.piece_size || 1024}]`).prop("selected", true);
$("#start_seeding").prop("checked", iv(recent.start_seeding));
$("#private").prop("checked", iv(recent.private_torrent));
Expand Down

0 comments on commit 1d9dfb5

Please sign in to comment.