From c2282ec3a42ab7f1a0b613f40edbdcbb3dd7f206 Mon Sep 17 00:00:00 2001 From: Rhilip Date: Sat, 16 Mar 2019 17:09:38 +0800 Subject: [PATCH] refactor(Route): Fix Route of Torrent --- apps/controllers/TorrentController.php | 76 ++++++++++++++++++++ apps/controllers/TorrentsController.php | 54 -------------- apps/views/{torrents => common}/helper.php | 5 +- apps/views/layout/base.php | 2 +- apps/views/rss_feed.php | 6 +- apps/views/{torrents => torrent}/details.php | 17 ++--- apps/views/{torrents => torrent}/upload.php | 0 apps/views/torrents/list.php | 7 +- 8 files changed, 97 insertions(+), 70 deletions(-) create mode 100644 apps/controllers/TorrentController.php rename apps/views/{torrents => common}/helper.php (92%) rename apps/views/{torrents => torrent}/details.php (87%) rename apps/views/{torrents => torrent}/upload.php (100%) diff --git a/apps/controllers/TorrentController.php b/apps/controllers/TorrentController.php new file mode 100644 index 0000000..0ef0244 --- /dev/null +++ b/apps/controllers/TorrentController.php @@ -0,0 +1,76 @@ +request->get('id'); + $torrent = new Torrent($tid); + + return $this->render('torrent/details', ['torrent' => $torrent]); + } + + public function actionUpload() + { + // TODO Check user upload pos + if (app()->request->isPost()) { + $torrent = new TorrentUploadForm(); + $torrent->setData(app()->request->post()); + $torrent->setFileData(app()->request->files()); + $success = $torrent->validate(); + if (!$success) { + return $this->render('errors/action_fail', ['title' => 'Upload Failed', 'msg' => $torrent->getError()]); + } else { + try { + $torrent->flush(); + } catch (\Exception $e) { + return $this->render('errors/action_fail', ['title' => 'Upload Failed', 'msg' => $e->getMessage()]); + } + + return app()->response->redirect('/torrent/details?id=' . $torrent->id); + } + + } else { + // TODO Check user can upload + return $this->render('torrent/upload'); + } + + } + + public function actionDownload() + { + $tid = app()->request->get('id'); + + $torrent = new Torrent($tid); // If torrent is not exist or can't visit , a notfound exception will throw out........ + $filename = '[' . app()->config->get('base.site_name') . ']' . $torrent->getTorrentName() . '.torrent'; + + app()->response->setHeader('Content-Type', 'application/x-bittorrent'); + if (strpos(app()->request->header('user-agent'), 'IE')) { + app()->response->setHeader('Content-Disposition', 'attachment; filename=' . str_replace('+', '%20', rawurlencode($filename))); + } else { + app()->response->setHeader('Content-Disposition', "attachment; filename=\"$filename\" ; charset=utf-8"); + } + + return $torrent->getDownloadDict(true); + } + + public function actionStructure() { + $tid = app()->request->get('id'); + + $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]); + } +} diff --git a/apps/controllers/TorrentsController.php b/apps/controllers/TorrentsController.php index e3fe1a1..98e14e2 100644 --- a/apps/controllers/TorrentsController.php +++ b/apps/controllers/TorrentsController.php @@ -11,8 +11,6 @@ use Rid\Http\Controller; use apps\models\Torrent; -use apps\models\form\TorrentUploadForm; - class TorrentsController extends Controller { @@ -32,60 +30,8 @@ public function actionIndex() } - public function actionUpload() - { - // TODO Check user upload pos - if (app()->request->isPost()) { - $torrent = new TorrentUploadForm(); - $torrent->setData(app()->request->post()); - $torrent->setFileData(app()->request->files()); - $success = $torrent->validate(); - if (!$success) { - return $this->render('errors/action_fail', ['title' => 'Upload Failed', 'msg' => $torrent->getError()]); - } else { - try { - $torrent->flush(); - } catch (\Exception $e) { - return $this->render('errors/action_fail', ['title' => 'Upload Failed', 'msg' => $e->getMessage()]); - } - - return app()->response->redirect('/torrents/details?id=' . $torrent->id); - } - - } else { - // TODO Check user can upload - return $this->render('torrents/upload'); - } - - } - public function actionSearch() { } - - public function actionDetails() - { - $tid = app()->request->get('id'); - $torrent = new Torrent($tid); - - return $this->render('torrents/details', ['torrent' => $torrent]); - } - - public function actionDownload() - { - $tid = app()->request->get('id'); - - $torrent = new Torrent($tid); // If torrent is not exist or can't visit , a notfound exception will throw out........ - $filename = '[' . app()->config->get('base.site_name') . ']' . $torrent->getTorrentName() . '.torrent'; - - app()->response->setHeader('Content-Type', 'application/x-bittorrent'); - if (strpos(app()->request->header('user-agent'), 'IE')) { - app()->response->setHeader('Content-Disposition', 'attachment; filename=' . str_replace('+', '%20', rawurlencode($filename))); - } else { - app()->response->setHeader('Content-Disposition', "attachment; filename=\"$filename\" ; charset=utf-8"); - } - - return $torrent->getDownloadDict(true); - } } diff --git a/apps/views/torrents/helper.php b/apps/views/common/helper.php similarity index 92% rename from apps/views/torrents/helper.php rename to apps/views/common/helper.php index 6059633..5fbaacb 100644 --- a/apps/views/torrents/helper.php +++ b/apps/views/common/helper.php @@ -14,7 +14,6 @@ * @param \apps\models\Torrent $torrent * @return string */ - function get_torrent_uploader_id(\apps\models\Torrent $torrent) { if ($torrent->getUplver() == 'yes' and app()->user->getClass(true) < app()->config->get('authority.see_anonymous_uploader')) { @@ -24,6 +23,10 @@ function get_torrent_uploader_id(\apps\models\Torrent $torrent) } } +/** + * @param \apps\models\Torrent $torrent + * @return string + */ function get_torrent_uploader(\apps\models\Torrent $torrent) { $owner_id = get_torrent_uploader_id($torrent); diff --git a/apps/views/layout/base.php b/apps/views/layout/base.php index f73a241..7461fe9 100644 --- a/apps/views/layout/base.php +++ b/apps/views/layout/base.php @@ -49,7 +49,7 @@
  • -
  • +
  • diff --git a/apps/views/rss_feed.php b/apps/views/rss_feed.php index b20880b..54b47d8 100644 --- a/apps/views/rss_feed.php +++ b/apps/views/rss_feed.php @@ -43,12 +43,12 @@ <![CDATA[<?= $torrent->getTitle() ?>]]> - getId() ?> + getId() ?> getDescr() ?> getUplver() == 'yes' ? 'Anonymous' : $torrent->getOwner()->getUsername()) . '@' . $site_name ?> Movie - getId() . '&cmtpage=0#startcomments' ?>]]> - + getId() . '&cmtpage=0#startcomments' ?>]]> + getInfoHash() ?> getAddedAt())) ?> diff --git a/apps/views/torrents/details.php b/apps/views/torrent/details.php similarity index 87% rename from apps/views/torrents/details.php rename to apps/views/torrent/details.php index 2979b65..22a5d3f 100644 --- a/apps/views/torrents/details.php +++ b/apps/views/torrent/details.php @@ -9,9 +9,10 @@ * @var \apps\models\Torrent $torrent */ -include 'helper.php'; ?> +insert('common/helper') ?> + layout('layout/base') ?> start('title')?>getTitle() ?>end();?> @@ -45,8 +46,8 @@
    Torrent Action
    +  Download Torrent +
     Add to Favour
    @@ -55,18 +56,18 @@
    -  Edit/Remove this Torrent -
    +  Edit/Remove this Torrent +
     Report this Torrent -
    +
     View Torrent's Files -
    +
     View Torrent's Structure -
    +
    diff --git a/apps/views/torrents/upload.php b/apps/views/torrent/upload.php similarity index 100% rename from apps/views/torrents/upload.php rename to apps/views/torrent/upload.php diff --git a/apps/views/torrents/list.php b/apps/views/torrents/list.php index 59927b7..f6d9f23 100644 --- a/apps/views/torrents/list.php +++ b/apps/views/torrents/list.php @@ -10,10 +10,11 @@ * @var \apps\models\Torrent $torrent */ -include 'helper.php'; $time_now = time(); ?> +insert('common/helper') ?> + layout('layout/base') ?> start('title')?>Torrents Listend();?> @@ -40,12 +41,12 @@
    - +