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

Commit

Permalink
feat(torrents): Use Torrent Form Model in TorrentController
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Aug 7, 2019
1 parent 10ccd92 commit e786e9e
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 62 deletions.
31 changes: 25 additions & 6 deletions apps/components/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace apps\components;

use apps\models\User;
use apps\models;
use apps\libraries\Mailer;
use apps\libraries\Constant;

Expand All @@ -23,6 +23,7 @@ class Site extends Component
protected $cur_user;

protected $users = [];
protected $torrents = [];
protected $map_username_to_id = [];

const LOG_LEVEL_NORMAL = 'normal';
Expand All @@ -35,6 +36,7 @@ public function onRequestBefore()
parent::onRequestBefore();
$this->cur_user = null;
$this->users = [];
$this->torrents = [];
$this->map_username_to_id = [];
}

Expand All @@ -43,24 +45,36 @@ protected static function getStaticCacheNameSpace(): string
return 'Cache:site';
}

public function getTorrent($tid)
{
if (array_key_exists($tid, $this->torrents)) {
$torrent = $this->torrents[$tid];
} else {
$torrent = new models\Torrent($tid); // TODO Handing if this user id does not exist
$this->torrents[$tid] = $torrent;
}
return $torrent;
}


/**
* @param $uid
* @return User|bool return False means this user is not exist
* @return models\User|bool return False means this user is not exist
*/
public function getUser($uid)
{
if (array_key_exists($uid, $this->users)) {
$user = $this->users[$uid];
} else {
$user = new User($uid); // TODO Handing if this user id does not exist
$user = new models\User($uid); // TODO Handing if this user id does not exist
$this->users[$uid] = $user;
}
return $user;
}

/**
* @param $username
* @return User|bool
* @return models\User|bool
*/
public function getUserByUserName($username)
{
Expand All @@ -82,7 +96,7 @@ public function getUserByUserName($username)

/**
* @param string $grant
* @return User|bool return False means this user is anonymous
* @return models\User|bool return False means this user is anonymous
*/
public function getCurUser($grant = 'cookies')
{
Expand All @@ -94,7 +108,7 @@ public function getCurUser($grant = 'cookies')

/**
* @param string $grant
* @return User|boolean
* @return models\User|boolean
*/
protected function loadCurUser($grant = 'cookies')
{
Expand Down Expand Up @@ -154,6 +168,11 @@ protected function loadCurUserFromPasskey()
return $user_id;
}

public function getTorrentFileLoc($tid)
{
return app()->getPrivatePath('torrents') . DIRECTORY_SEPARATOR . $tid . '.torrent';
}

public function writeLog($msg, $level = self::LOG_LEVEL_NORMAL)
{
app()->pdo->createCommand('INSERT INTO `site_log`(`create_at`,`msg`, `level`) VALUES (CURRENT_TIMESTAMP, :msg, :level)')->bindParams([
Expand Down
2 changes: 1 addition & 1 deletion apps/controllers/RssController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function actionIndex()
$fetch = app()->pdo->createCommand('SELECT `id` FROM torrents ORDER BY added_at DESC LIMIT 50;')->queryColumn();

$torrents = array_map(function ($id) {
return new Torrent($id);
return app()->site->getTorrent($id);
}, $fetch);

return $this->render('rss_feed', ['torrents' => $torrents]);
Expand Down
35 changes: 21 additions & 14 deletions apps/controllers/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,44 @@

namespace apps\controllers;

use apps\models\Torrent;
use apps\models\form\Torrent as TorrentForm;
use apps\models\form\Torrent;

use Rid\Http\Controller;

class TorrentController extends Controller
{
public function actionDetails()
{
$tid = app()->request->get('id');
$torrent = new Torrent($tid);
$details = new Torrent\DetailsForm();
$details->setData(app()->request->get());
$success = $details->validate();
if (!$success) {
return $this->render('action/action_fail');
}

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

public function actionEdit() // TODO
{

}

public function actionSnatch() // TODO
public function actionSnatch()
{
$pager = new TorrentForm\SnatchForm();
$pager->setData(app()->request->get());
$success = $pager->validate();
$snatch = new Torrent\SnatchForm();
$snatch->setData(app()->request->get());
$success = $snatch->validate();
if (!$success) {
return $this->render('action/action_fail');
}

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

public function actionDownload()
{
$downloader = new TorrentForm\DownloadForm();
$downloader = new Torrent\DownloadForm();
$downloader->setData(app()->request->get());
$success = $downloader->validate();
if (!$success) {
Expand All @@ -60,9 +63,13 @@ public function actionComments()

public function actionStructure()
{
$tid = app()->request->get('id');
$structure = new Torrent\StructureForm();
$structure->setData(app()->request->get());
$success = $structure->validate();
if (!$success) {
return $this->render('action/action_fail');
}

$torrent = new Torrent($tid); // If torrent is not exist or can't visit , a notfound exception will throw out........
return $this->render('torrent/structure', ['torrent' => $torrent]);
return $this->render('torrent/structure', ['structure' => $structure]);
}
}
20 changes: 3 additions & 17 deletions apps/models/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@

use apps\libraries\Constant;

use Rid\Bencode\Bencode;
use Rid\Utils;
use Rid\Exceptions\NotFoundException;
use Rid\Utils\AttributesImportUtils;
use Rid\Utils\ClassValueCacheUtils;

class Torrent
{
use AttributesImportUtils;
use ClassValueCacheUtils;
use Utils\AttributesImportUtils;
use Utils\ClassValueCacheUtils;

private $id = null;

Expand Down Expand Up @@ -78,11 +76,6 @@ public function loadTorrentContentById($id)
$this->importAttributes($self);
}

public static function TorrentFileLoc($id = 0)
{
return app()->getPrivatePath('torrents') . DIRECTORY_SEPARATOR . $id . '.torrent';
}

/**
* @return mixed
*/
Expand Down Expand Up @@ -178,13 +171,6 @@ public function getDescr()
return $this->descr;
}

public function getRawDict()
{
$file = self::TorrentFileLoc($this->id);
$dict = Bencode::load($file);
return $dict;
}

/**
* @param bool $raw
* @return mixed
Expand Down
31 changes: 31 additions & 0 deletions apps/models/form/Torrent/DetailsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 8/7/2019
* Time: 7:22 PM
*/

namespace apps\models\form\Torrent;

use apps\models\form\Traits\isValidTorrentTrait;

use Rid\Validators\Validator;

class DetailsForm extends Validator
{

use isValidTorrentTrait;

public static function inputRules()
{
return [
'id' => 'required | Integer'
];
}

public static function callbackRules()
{
return ['isExistTorrent'];
}
}
14 changes: 2 additions & 12 deletions apps/models/form/Torrent/DownloadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@

namespace apps\models\form\Torrent;


use apps\models\form\Traits\isValidTorrentTrait;
use Rid\Bencode\Bencode;
use Rid\Validators\Validator;

class DownloadForm extends Validator
class DownloadForm extends StructureForm
{
public $https;

use isValidTorrentTrait;

public static function inputRules()
{
return [
Expand All @@ -27,11 +22,6 @@ public static function inputRules()
];
}

public static function callbackRules()
{
return ['isExistTorrent'];
}

public function setRespHeaders() {
$filename = '[' . config('base.site_name') . ']' . $this->torrent->getTorrentName() . '.torrent';

Expand All @@ -44,7 +34,7 @@ public function setRespHeaders() {
}

public function getDownloadDict() {
$dict = $this->torrent->getRawDict();
$dict = $this->getTorrentFileContentDict();

$scheme = 'http://';
if (filter_var($this->https, FILTER_VALIDATE_BOOLEAN))
Expand Down
23 changes: 23 additions & 0 deletions apps/models/form/Torrent/StructureForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 8/7/2019
* Time: 6:58 PM
*/

namespace apps\models\form\Torrent;

use Rid\Bencode\Bencode;

class StructureForm extends DetailsForm
{

public function getTorrentFileContentDict()
{
$file_loc = app()->site->getTorrentFileLoc($this->id);
$content = Bencode::load($file_loc);
return $content;
}

}
2 changes: 1 addition & 1 deletion apps/models/form/Torrents/SearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function getRemoteData()
])->queryColumn();

return array_map(function ($id) {
return new Torrent($id);
return app()->site->getTorrent($id);
}, $fetch);
}
}
4 changes: 2 additions & 2 deletions apps/models/form/Torrents/UploadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public function flush()
$this->setBuff();

// Save this torrent
$dump_status = Bencode::dump(Torrent::TorrentFileLoc($this->id), $this->torrent_dict);
$dump_status = Bencode::dump(app()->site->getTorrentFileLoc($this->id), $this->torrent_dict);
if ($dump_status == false) {
throw new \Exception('std_torrent_cannot_save');
}
Expand All @@ -322,7 +322,7 @@ public function flush()
} catch (\Exception $e) {
// Delete the saved torrent file when torrent save success but still get Exception on other side
if (isset($dump_status) && $dump_status == true) {
unlink(Torrent::TorrentFileLoc($this->id));
unlink(app()->site->getTorrentFileLoc($this->id));
}

app()->pdo->rollback();
Expand Down
3 changes: 2 additions & 1 deletion apps/models/form/Traits/isValidTorrentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function isExistTorrent() {
$this->buildCallbackFailMsg('Torrent', 'The torrent id ('. $tid. ') is not exist in our database');
return;
}
$this->torrent = new Torrent($tid);

$this->torrent = app()->site->getTorrent($tid);
}
}
3 changes: 2 additions & 1 deletion apps/views/torrent/details.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* Time: 18:00
*
* @var League\Plates\Template\Template $this
* @var \apps\models\Torrent $torrent
* @var \apps\models\form\Torrent\DetailsForm $details
*/

$torrent = $details->getTorrent();
?>

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

0 comments on commit e786e9e

Please sign in to comment.