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

Commit

Permalink
feat(torrent/snatch): Add view of torrent/snatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Aug 6, 2019
1 parent 1e600a0 commit 259c5f1
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- **Editor:** Support wysibb editor
- **Gravatar:** Add support of gravatar
- **Process:** Add custom Process Support
- **Process:** Clean Components before sleep
- **Redis:** Add mutiDelete() function for Redis
- **Tracker:** Move From Timer to Process
- **Tracker:** Add `retry in` field when failed
Expand Down
6 changes: 3 additions & 3 deletions apps/controllers/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public function actionEdit() // TODO

public function actionSnatch() // TODO
{
//$tid = app()->request->get('id');
//$torrent = new Torrent($tid);
$tid = app()->request->get('id');
$torrent = new Torrent($tid);

//return $this->render('torrent/snatch', ['torrent' => $torrent]);
return $this->render('torrent/snatch', ['torrent' => $torrent]);
}


Expand Down
2 changes: 1 addition & 1 deletion apps/libraries/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function userContent($uid)

public static function torrentContent($tid)
{
return 'Torrent:torrent_' . $tid . 'content:hash';
return 'Torrent:torrent_' . $tid . '_content:hash';
}

// Tracker User
Expand Down
6 changes: 6 additions & 0 deletions apps/models/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,10 @@ public function getLastCommentsDetails()
])->queryAll();
});
}

public function getSnatchDetails() {
return app()->pdo->createCommand('SELECT * FROM `snatched` WHERE `torrent_id` = :tid')->bindParams([
'tid' => $this->id
])->queryAll();
}
}
11 changes: 6 additions & 5 deletions apps/process/TrackerAnnounceProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ private function processAnnounceRequest($timenow, $queries, $seeder, $userInfo,
])->queryScalar();

if ($selfRecordCount == 0) {
app()->pdo->createCommand("INSERT INTO snatched (`user_id`,`torrent_id`,`agent`,`port`,`true_downloaded`,`true_uploaded`,`this_download`,`this_uploaded`,`to_go`,`{$timeKey}`,`create_at`,`last_action_at`)
VALUES (:uid,:tid,:agent,:port,:true_dl,:true_up,:this_dl,:this_up,:to_go,:time,FROM_UNIXTIME(:create_at),FROM_UNIXTIME(:last_action_at))")->bindParams([
app()->pdo->createCommand("INSERT INTO snatched (`user_id`,`torrent_id`,`agent`,`ip`,`port`,`true_downloaded`,`true_uploaded`,`this_download`,`this_uploaded`,`to_go`,`{$timeKey}`,`create_at`,`last_action_at`)
VALUES (:uid,:tid,:agent,INET6_ATON(:ip),:port,:true_dl,:true_up,:this_dl,:this_up,:to_go,:time,FROM_UNIXTIME(:create_at),FROM_UNIXTIME(:last_action_at))")->bindParams([
'uid' => $userInfo['id'], 'tid' => $torrentInfo['id'],
'agent' => $queries['user-agent'], 'port' => $queries['port'],
'agent' => $queries['user-agent'], 'ip'=>$queries['remote_ip'], 'port' => $queries['port'],
'true_up' => 0, 'true_dl' => 0,
'this_up' => 0, 'this_dl' => 0,
'to_go' => $queries['left'], 'time' => 0,
Expand Down Expand Up @@ -147,9 +147,10 @@ private function processAnnounceRequest($timenow, $queries, $seeder, $userInfo,
if (app()->pdo->getRowCount() > 0) { // It means that the delete or update query affected so we can safety update `snatched` table
app()->pdo->createCommand("UPDATE `snatched` SET `true_uploaded` = `true_uploaded` + :true_up,`true_downloaded` = `true_downloaded` + :true_dl,
`this_uploaded` = `this_uploaded` + :this_up, `this_download` = `this_download` + :this_dl, `to_go` = :left, `{$timeKey}`=`{$timeKey}` + :duration,
`agent` = :agent WHERE `torrent_id` = :tid AND `user_id` = :uid")->bindParams([
`ip` = INET6_ATON(:ip),`port` = :port, `agent` = :agent WHERE `torrent_id` = :tid AND `user_id` = :uid")->bindParams([
'true_up' => $trueUploaded, 'true_dl' => $trueDownloaded, 'this_up' => $thisUploaded, 'this_dl' => $thisDownloaded,
'left' => $queries['left'], 'duration' => $duration, 'agent' => $queries['user-agent'],
'left' => $queries['left'], 'duration' => $duration,
'ip'=>$queries['remote_ip'],'port' => $queries['port'], 'agent' => $queries['user-agent'],
'tid' => $torrentInfo['id'], 'uid' => $userInfo['id']
])->execute();
}
Expand Down
81 changes: 81 additions & 0 deletions apps/views/torrent/snatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/3/16
* Time: 17:10
*
* @var League\Plates\Template\Template $this
* @var \apps\models\Torrent $torrent
*/

$timenow = time();
?>

<?= $this->layout('layout/base') ?>

<?php $this->start('title')?>Torrents Snatched Details<?php $this->end();?>

<?php $this->start('container')?>
<div class="text-center torrent-title-block">
<h1 class="torrent-title"><?= $torrent->getTitle() ?></h1>
<small class="torrent-subtitle"><em><?= $torrent->getSubtitle() ?: 'No Subtitle.' ?></em></small>
</div>

<div class="row">
<div class="col-md-12">
<div class="panel" id="torrent_snatched_details_panel">
<div class="panel-heading"><b>Torrent Snatched Details</b></div>
<div class="panel-body" id="torrent_snatched_details_body">
<div id="torrent_snatched_details">
<table class="table table-hover table-condensed">
<thead>
<tr>
<th>Username</th>
<th>Ip Address</th> <!-- FIXME check privilege -->
<th>IP on finished</th>
<th>Uploaded/Downloaded</th>
<th>Ratio</th>
<th>Se. Time</th>
<th>Le. Time</th>
<th>Finished?</th>
<th>Completed At</th>
<th>Last Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($torrent->getSnatchDetails() as $snatchDetail): ?>
<tr>
<td><?= $this->insert('helper/username',['torrent'=>$torrent,'user'=>app()->site->getUser($snatchDetail['user_id'])]) ?></td>
<td><?= inet_ntop($snatchDetail['ip']) ?></td>
<td><?= $snatchDetail['finish_ip'] ? inet_ntop($snatchDetail['finish_ip']) : '?' ?></td>
<td>
<?= $this->e($snatchDetail['this_uploaded'],'format_bytes') ?>@<?= $this->e($snatchDetail['this_uploaded'] > 0 ? ($snatchDetail['this_uploaded'] / ($snatchDetail['seed_time'] + $snatchDetail['leech_time'])) : 0,'format_bytes') ?>/s <br>
<?= $this->e($snatchDetail['this_download'],'format_bytes') ?>@<?= $this->e($snatchDetail['this_download'] > 0 ? ($snatchDetail['this_download'] / $snatchDetail['leech_time']) : 0,'format_bytes') ?>/s <br>
</td>
<td>
<?php if ($snatchDetail['this_download'] > 0): ?>
<?= number_format($snatchDetail['this_uploaded'] / $snatchDetail['this_download'], 3) ?>
<?php elseif ($snatchDetail['this_uploaded'] > 0): ?>
Inf.
<?php else: ?>
---
<?php endif; ?>
</td>
<td><?= $this->e($snatchDetail['seed_time'], 'sec2hms') ?></td>
<td><?= $this->e($snatchDetail['leech_time'], 'sec2hms') ?></td>
<td><?= $snatchDetail['finished'] ?></td>
<td><?= $snatchDetail['finish_at'] ?></td>
<td><?= $snatchDetail['last_action_at'] ?></td>
</tr>


<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<?php $this->end();?>
6 changes: 5 additions & 1 deletion apps/views/torrents/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@
<td class="text-center" data-item="t-added-date"><time class="nowrap" data-timestamp="<?= strtotime($torrent->getAddedAt()) ?>" data-ttl="<?= $time_now - strtotime($torrent->getAddedAt()) ?>"><?= str_replace(' ','<br />',$torrent->getAddedAt()) ?></time></td>
<td class="text-center" data-item="t-seeder" data-seeder="<?= $this->e($torrent->getComplete()) ?>"><?= number_format($torrent->getComplete()) ?></td>
<td class="text-center" data-item="t-leecher" data-leecher="<?= $this->e($torrent->getIncomplete()) ?>"><?= number_format($torrent->getIncomplete()) ?></td>
<td class="text-center" data-item="t-completed" data-completed="<?= $this->e($torrent->getDownloaded()) ?>"><?= number_format($torrent->getDownloaded()) ?></td>
<td class="text-center" data-item="t-completed" data-completed="<?= $this->e($torrent->getDownloaded()) ?>">
<?php if ($torrent->getDownloaded() > 0): ?><a href="/torrent/snatch?id=<?= $torrent->getId() ?>"><?php endif; ?>
<?= number_format($torrent->getDownloaded()) ?>
<?php if ($torrent->getDownloaded() > 0): ?></a><?php endif; ?>
</td>
<td class="text-center" data-item="t-uploader" data-uploader="<?= $this->e($torrent->getUplver() ? 0 : $torrent->getOwnerId()) ?>"><?= $this->insert('helper/username', ['user' => $torrent->getOwner(), 'torrent' => $torrent]) ?></td>
</tr>
<?php endforeach; ?>
Expand Down
22 changes: 21 additions & 1 deletion framework/View/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function register(Engine $engine)
$engine->registerFunction('format_bytes_compact', [$this, 'format_bytes_compact']);
$engine->registerFunction('format_bytes_loose', [$this, 'format_bytes_loose']);
$engine->registerFunction('format_ubbcode', [$this, 'format_ubbcode']);
$engine->registerFunction('sec2hms',[$this,'sec2hms']);
}

public static function setDefault(&$array, $defaults)
Expand All @@ -46,7 +47,7 @@ public function format_bytes($var)
$units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$pow = max(min($pow, count($units) - 1), 0);

// Uncomment one of the following alternatives
$bytes /= pow(1024, $pow);
Expand Down Expand Up @@ -78,4 +79,23 @@ public function format_ubbcode($var)
$code->setStorage(new RedisStorage(app()->redis->getRedis()));
return $code->parse();
}

public function sec2hms($sec, $padHours = false)
{

$hms = '';

$hours = intval(intval($sec) / 3600);
$minutes = intval(($sec / 60) % 60);
$seconds = intval($sec % 60);

$hms .= ($padHours)
? str_pad($hours, 2, '0', STR_PAD_LEFT) . ':'
: $hours . ':';

$hms .= str_pad($minutes, 2, '0', STR_PAD_LEFT) . ':';
$hms .= str_pad($seconds, 2, '0', STR_PAD_LEFT);

return $hms;
}
}

0 comments on commit 259c5f1

Please sign in to comment.