diff --git a/apps/controllers/TorrentController.php b/apps/controllers/TorrentController.php index 7bcbce8..edc6e04 100644 --- a/apps/controllers/TorrentController.php +++ b/apps/controllers/TorrentController.php @@ -49,8 +49,7 @@ public function actionDownload() return $this->render('action/action_fail'); } - $downloader->setRespHeaders(); - return $downloader->getDownloadDict(); + return $downloader->sendFileContentToClient(); } public function actionComments() diff --git a/apps/models/form/News/EditForm.php b/apps/models/form/News/EditForm.php index 238de16..c650b31 100644 --- a/apps/models/form/News/EditForm.php +++ b/apps/models/form/News/EditForm.php @@ -37,7 +37,7 @@ public static function inputRules() public function flush() { $userid = app()->site->getCurUser()->getId(); - if ($this->id == 0) { // This is new news + if ((int) $this->id == 0) { // This is new news app()->pdo->createCommand('INSERT INTO news (user_id,create_at,title,body,notify,force_read) VALUES (:uid,CURRENT_TIMESTAMP,:title,:body,:notify,:fread);')->bindParams([ 'uid' => $userid, 'title' => $this->title, 'body' => $this->body, 'notify' => $this->notify, 'fread' => $this->force_read diff --git a/apps/models/form/News/SearchForm.php b/apps/models/form/News/SearchForm.php index 24a65f6..7c97574 100644 --- a/apps/models/form/News/SearchForm.php +++ b/apps/models/form/News/SearchForm.php @@ -19,7 +19,7 @@ class SearchForm extends Pager public static function defaultData() { - return parent::defaultData() + [ + return [ 'query' => 'title', 'search' => '' ]; @@ -37,11 +37,6 @@ public static function inputRules() ]; } - public static function callbackRules() - { - return ['checkPager']; - } - public function getRemoteTotal(): int { $search = $this->getData('search'); diff --git a/apps/models/form/Torrent/DownloadForm.php b/apps/models/form/Torrent/DownloadForm.php index e6d90a5..1ffd442 100644 --- a/apps/models/form/Torrent/DownloadForm.php +++ b/apps/models/form/Torrent/DownloadForm.php @@ -8,12 +8,17 @@ namespace apps\models\form\Torrent; +use apps\models\form\Traits\FileDownloadTrait; use Rid\Bencode\Bencode; class DownloadForm extends StructureForm { + use FileDownloadTrait; + public $https; + protected static $SEND_FILE_CONTENT_TYPE = 'application/x-bittorrent'; + public static function inputRules() { return [ @@ -22,18 +27,12 @@ public static function inputRules() ]; } - public function setRespHeaders() { - $filename = '[' . config('base.site_name') . ']' . $this->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"); - } + protected function getSendFileName(): string + { + return '[' . config('base.site_name') . ']' . $this->torrent->getTorrentName() . '.torrent'; } - public function getDownloadDict() { + public function getSendFileContent() { $dict = $this->getTorrentFileContentDict(); $scheme = 'http://'; diff --git a/apps/models/form/Traits/FileDownloadTrait.php b/apps/models/form/Traits/FileDownloadTrait.php new file mode 100644 index 0000000..6b8768f --- /dev/null +++ b/apps/models/form/Traits/FileDownloadTrait.php @@ -0,0 +1,44 @@ +getSendFileName(); + app()->response->setHeader('Content-Type', $this->getContentType()); + + 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"); + } + } + + abstract protected function getSendFileContent(); + + public function sendFileContentToClient() + { + $this->setRespHeaders(); + return $this->getSendFileContent(); + } +}