a hack to have persistent totals-stats per torrent over client-restart-cycles.
(totals are saved to database for every torrent.)
this again is a piece of quickhack-code even lacking error-handling. ~
the hack is only tested on tf-2.1-b4rt-6 but it should work on any tf 2.1
installation although i am not soo sure where to integrate the snips exactly
because my version is already very different in that parts. (my current version
is already using the client-handler-architecture and has part of this hack
integrated there...)
means the places i describe here are actually never tested, but as this hack is
rather trivial integration should be quite easy.
1. databasechoose the snip for your database and import into your torrentflux database.
1.1 mysql--
-- tf_torrent_totals
--
CREATE TABLE tf_torrent_totals (
tid VARCHAR(40) NOT NULL default '',
uptotal BIGINT(80) NOT NULL default '0',
downtotal BIGINT(80) NOT NULL default '0',
PRIMARY KEY (tid)
) TYPE=MyISAM;
1.2 non-mysql(as always this version is untested ~)
--
-- tf_torrent_totals
--
CREATE TABLE tf_torrent_totals (
tid VARCHAR(40) NOT NULL default '',
uptotal INTEGER(80) NOT NULL default '0',
downtotal INTEGER(80) NOT NULL default '0',
PRIMARY KEY (tid)
) ;
2. new functionsadd where you have your global functions defined.. (eg functions.php)
/**
* gets totals of a torrent
*
* @param $torrent name of the torrent
* @return array with torrent-totals
*/
function getTorrentTotals($torrent) {
global $cfg, $db;
if ( !isset($torrent) || !preg_match('/^[a-zA-Z0-9._]+$/', $torrent) )
return;
$torrentId = getTorrentHash($torrent);
$sql = "select uptotal,downtotal from tf_torrent_totals where tid = '".$torrentId."'";
$retVal = array();
$result = $db->Execute($sql);
$row = $result->FetchRow();
if (!empty($row)) {
$retVal["uptotal"] = $row["uptotal"];
$retVal["downtotal"] = $row["downtotal"];
} else {
$retVal["uptotal"] = 0;
$retVal["downtotal"] = 0;
}
return $retVal;
}
/**
* updates totals of a torrent
*
* @param $torrent name of the torrent
* @param $uptotal uptotalof the torrent
* @param $downtotal downtotal of the torrent
*/
function updateTorrentTotals($torrent, $uptotal, $downtotal) {
global $cfg, $db;
if ( !isset($torrent) || !preg_match('/^[a-zA-Z0-9._]+$/', $torrent) )
return;
$torrentId = getTorrentHash($torrent);
$sql = "select uptotal,downtotal from tf_torrent_totals where tid = '".$torrentId."'";
$result = $db->Execute($sql);
$row = $result->FetchRow();
if (!empty($row)) {
$currentUp = $row["uptotal"];
$currentDown = $row["downtotal"];
$upSum = $currentUp + $uptotal;
$downSum = $currentDown + $downtotal;
$sql = "UPDATE tf_torrent_totals SET uptotal = '".($upSum+0)."', downtotal = '".($downSum+0)."' WHERE tid = '".$torrentId."'";
$db->Execute($sql);
} else {
$sql = "INSERT INTO `tf_torrent_totals` ( `tid` , `uptotal` ,`downtotal` )
VALUES (
'".$torrentId."',
'".$uptotal."',
'".$downtotal."'
)";
$db->Execute($sql);
}
}
/**
* gets hash of a torrent
*
* @param $torrent name of the torrent
* @return var with torrent-hash
*/
function getTorrentHash($torrent) {
global $cfg;
$result = shell_exec("cd " . $cfg["torrent_file_path"]."; " . $cfg["pythonCmd"] . " -OO " . $cfg["btshowmetainfo"]." \"".$torrent."\"");
$resultAry = explode("\n",$result);
$hashAry = explode(":",trim($resultAry[3]));
return trim($hashAry[1]);
}
3. integration3.1 downloaddetails.phpfind :
{
die("fatal error torrent file not specified");
}
add below :
$divi = 1024<<10;
$totalAry = getTorrentTotals(urldecode($torrent));
$upTotalCurrent = $af->uptotal/$divi;
$downTotalCurrent = $af->downtotal/$divi;
$upTotal = $totalAry["uptotal"]/$divi;
$downTotal = $totalAry["downtotal"]/$divi;
$upTotal += $upTotalCurrent;
$downTotal += $downTotalCurrent;
if ($af->running != 1) {
$upTotalCurrent = 0;
$downTotalCurrent = 0;
}
find :
<tr>
<td align="right"><div class="tiny">Down:</div></td>
<td bgcolor="<?php echo $cfg["body_data_bg"] ?>"><div class="tiny"><?php echo "<strong>".formatFreeSpace($af->GetRealDownloadTotal())."</strong>" ?></div></td>
<td align="right"><div class="tiny">Up:</div></td>
<td bgcolor="<?php echo $cfg["body_data_bg"] ?>"><div class="tiny"><?php echo "<strong>".formatFreeSpace($af->uptotal/(1024*1024))."</strong>" ?></div></td>
</tr>
replace with :
<tr>
<td align="right"><div class="tiny">Down:</div></td>
<td bgcolor="<?php echo $cfg["body_data_bg"] ?>"><div class="tiny"><?php echo "<strong>".formatFreeSpace($downTotalCurrent)."</strong>" ?></div></td>
<td align="right"><div class="tiny">Up:</div></td>
<td bgcolor="<?php echo $cfg["body_data_bg"] ?>"><div class="tiny"><?php echo "<strong>".formatFreeSpace($upTotalCurrent)."</strong>" ?></div></td>
</tr>
<tr>
<td align="right"><div class="tiny">Down-Total:</div></td>
<td bgcolor="<?php echo $cfg["body_data_bg"] ?>"><div class="tiny"><?php echo "<strong>".formatFreeSpace($downTotal)."</strong>" ?></div></td>
<td align="right"><div class="tiny">Up-Total:</div></td>
<td bgcolor="<?php echo $cfg["body_data_bg"] ?>"><div class="tiny"><?php echo "<strong>".formatFreeSpace($upTotal)."</strong>" ?></div></td>
</tr>
3.2 where you start your torrents (thats in index.php on 2.1 final)may work on 2.1 final :
find :
@unlink($cfg["torrent_file_path"].$delfile);
add
above :
include_once("AliasFile.php");
$af = new AliasFile($cfg['torrent_file_path'].$alias_file, 0);
// update totals for this torrent
updateTorrentTotals($delfile, $af->uptotal+0, $af->downtotal+0);
3.3 where you delete your torrents (thats in index.php on 2.1 final)may work on 2.1 final :
find :
// create AliasFile object and write out the stat file
$af = new AliasFile($cfg["torrent_file_path"].$alias.".stat", $owner);
if ($cfg["AllowQueing"])
{
if($queue == "1")
{
$af->QueueTorrentFile(); // this only writes out the stat file (does not start torrent)
}
else
{
$af->StartTorrentFile(); // this only writes out the stat file (does not start torrent)
}
}
replace with :
// create AliasFile object and write out the stat file
$af = new AliasFile($cfg["torrent_file_path"].$alias.".stat", $owner);
// update totals for this torrent
updateTorrentTotals($torrent, $af->uptotal+0, $af->downtotal+0);
if ($cfg["AllowQueing"])
{
if($queue == "1")
{
$af->QueueTorrentFile(); // this only writes out the stat file (does not start torrent)
}
else
{
$af->StartTorrentFile(); // this only writes out the stat file (does not start torrent)
}
}
i attached a screenie of the downloaddetails of a test-torrent which was stopped and resumed multiple times.
regards,
b4rt