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

Commit

Permalink
refactor(File/Download): Seperate client download file function to Fi…
Browse files Browse the repository at this point in the history
…leDownloadTrait
  • Loading branch information
Rhilip committed Aug 7, 2019
1 parent e9b8d8d commit 456ea04
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
3 changes: 1 addition & 2 deletions apps/controllers/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public function actionDownload()
return $this->render('action/action_fail');
}

$downloader->setRespHeaders();
return $downloader->getDownloadDict();
return $downloader->sendFileContentToClient();
}

public function actionComments()
Expand Down
2 changes: 1 addition & 1 deletion apps/models/form/News/EditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions apps/models/form/News/SearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SearchForm extends Pager

public static function defaultData()
{
return parent::defaultData() + [
return [
'query' => 'title',
'search' => ''
];
Expand All @@ -37,11 +37,6 @@ public static function inputRules()
];
}

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

public function getRemoteTotal(): int
{
$search = $this->getData('search');
Expand Down
19 changes: 9 additions & 10 deletions apps/models/form/Torrent/DownloadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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://';
Expand Down
44 changes: 44 additions & 0 deletions apps/models/form/Traits/FileDownloadTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 8/7/2019
* Time: 10:07 PM
*/

namespace apps\models\form\Traits;


trait FileDownloadTrait
{

protected function getSendFileName(): string
{
return static::$SEND_FILE_NAME ?? 'file';
}

protected function getContentType(): string
{
return static::$SEND_FILE_CONTENT_TYPE ?? 'application/octet-stream';
}

final private function setRespHeaders()
{
$filename = $this->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();
}
}

0 comments on commit 456ea04

Please sign in to comment.