This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Subtitle): Add Base Subtitle Page
1. Add subtitle search, upload, download support 2. change the http server config about `buffer_output_size` , which default is 2M, may let server can't sent file bigger than 2M. So change it to 32M.
- Loading branch information
Showing
11 changed files
with
464 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 8/7/2019 | ||
* Time: 9:57 PM | ||
*/ | ||
|
||
namespace apps\controllers; | ||
|
||
use apps\models\form\Subtitles; | ||
|
||
use Rid\Http\Controller; | ||
|
||
class SubtitlesController extends Controller | ||
{ | ||
public function actionIndex() | ||
{ | ||
return $this->actionSearch(); | ||
} | ||
|
||
public function actionSearch() | ||
{ | ||
$search = new Subtitles\SearchForm(); | ||
if (false === $success = $search->validate()) { | ||
return $this->render('action/action_fail'); | ||
} | ||
return $this->render('subtitles/search',['search' => $search]); | ||
} | ||
|
||
public function actionUpload() | ||
{ | ||
if (app()->request->isPost()) { | ||
$upload = new Subtitles\UploadForm(); | ||
if (false === $success = $upload->validate()) { | ||
return $this->render('action/action_fail',['msg' => $upload->getError()]); // TODO add redirect | ||
} else { | ||
$upload->flush(); | ||
return $this->render('action/action_success'); // TODO add redirect | ||
} | ||
} | ||
return $this->actionSearch(); | ||
} | ||
|
||
public function actionDownload() | ||
{ | ||
$download = new Subtitles\DownloadForm(); | ||
if (false === $success = $download->validate()) { | ||
return $this->render('action/action_fail'); | ||
} | ||
|
||
return $download->sendFileContentToClient(); | ||
} | ||
|
||
public function actionDelete() | ||
{ | ||
$delete = new Subtitles\DeleteForm(); | ||
if (false === $success = $delete->validate()) { | ||
return $this->render('action/action_fail'); // TODO add redirect | ||
} else { | ||
return $this->render('action/action_success'); // TODO add redirect | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 8/7/2019 | ||
* Time: 11:22 PM | ||
*/ | ||
|
||
namespace apps\models\form\Subtitles; | ||
|
||
|
||
use Rid\Validators\Validator; | ||
|
||
class DeleteForm extends Validator | ||
{ | ||
// TODO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 8/7/2019 | ||
* Time: 10:03 PM | ||
*/ | ||
|
||
namespace apps\models\form\Subtitles; | ||
|
||
use apps\models\form\Traits; | ||
use Rid\Validators\Validator; | ||
|
||
class DownloadForm extends Validator | ||
{ | ||
use Traits\FileSentTrait, Traits\isValidSubtitleTrait; | ||
|
||
protected $_autoload_data = true; | ||
protected $_autoload_data_from = ['get']; | ||
|
||
private function addDownloadHit() | ||
{ | ||
app()->pdo->createCommand('UPDATE `subtitles` SET `hits` = `hits` + 1 WHERE id = :sid')->bindParams([ | ||
'sid' => $this->id | ||
])->execute(); | ||
} | ||
|
||
protected function getSendFileName(): string | ||
{ | ||
return $this->subtitle['filename']; | ||
} | ||
|
||
protected function getSendFileContentLength(): int | ||
{ | ||
return (int)$this->subtitle['size']; | ||
} | ||
|
||
protected function hookFileContentSend() | ||
{ | ||
$this->addDownloadHit(); | ||
} | ||
|
||
protected function getSendFileContent() | ||
{ | ||
$filename = $this->id . '.' . $this->subtitle['ext']; | ||
$file_loc = app()->getPrivatePath('subs') . DIRECTORY_SEPARATOR . $filename; | ||
return file_get_contents($file_loc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** TODO | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 8/7/2019 | ||
* Time: 10:03 PM | ||
*/ | ||
|
||
namespace apps\models\form\Subtitles; | ||
|
||
use Rid\Validators\Pager; | ||
|
||
class SearchForm extends Pager | ||
{ | ||
|
||
protected function getRemoteTotal(): int | ||
{ | ||
return app()->pdo->createCommand('SELECT COUNT(`id`) FROM `subtitles`')->queryScalar(); // TODO | ||
} | ||
|
||
protected function getRemoteData(): array | ||
{ | ||
return app()->pdo->createCommand('SELECT * FROM `subtitles` ORDER BY id DESC')->queryAll(); // TODO | ||
} | ||
|
||
public function getSubsSizeSum() | ||
{ | ||
if (false === $size = app()->redis->get('Site:subtitle_sub_size:string')) { | ||
$size = app()->pdo->createCommand('SELECT SUM(`size`) FROM `subtitles`')->queryScalar(); | ||
app()->redis->set('Site:subtitle_sub_size:string', $size); | ||
} | ||
return $size; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 8/7/2019 | ||
* Time: 10:02 PM | ||
*/ | ||
|
||
namespace apps\models\form\Subtitles; | ||
|
||
|
||
use apps\models\form\Traits\isValidTorrentTrait; | ||
use Rid\Http\UploadFile; | ||
use Rid\Validators\Validator; | ||
|
||
class UploadForm extends Validator | ||
{ | ||
use isValidTorrentTrait; | ||
|
||
/** @var UploadFile $file */ | ||
public $file; | ||
|
||
public $title; | ||
public $torrent_id; | ||
// public $lang_id; //TODO support | ||
public $anonymous; | ||
|
||
private $hashs; | ||
|
||
protected $_autoload_data = true; | ||
protected $_autoload_data_from = ['post', 'files']; | ||
|
||
public static function defaultData() | ||
{ | ||
return [ | ||
'anonymous' => 0 | ||
]; | ||
} | ||
|
||
public static function inputRules() | ||
{ | ||
return [ | ||
'torrent_id' => 'Required | Integer', | ||
'file' => [ | ||
['Required'], | ||
['Upload\Required'], | ||
['Upload\Extension', ['allowed' => ['sub', 'srt', 'zip', 'rar', 'ace', 'txt', 'ssa', 'ass', 'cue']]], | ||
['Upload\Size', ['size' => config('upload.max_subtitle_file_size') . 'B']] | ||
], | ||
'anonymous' => [ | ||
['InList', ['list' => [0, 1]]] | ||
] | ||
]; | ||
} | ||
|
||
public static function callbackRules() | ||
{ | ||
return ['isExistTorrent', 'checkSubtitleUniqueByHash']; | ||
} | ||
|
||
protected function checkSubtitleUniqueByHash() | ||
{ | ||
/** @var UploadFile $file */ | ||
$file = $this->getData('file'); | ||
$this->hashs = $file_md5 = md5_file($file->tmpName); | ||
|
||
$exist_id = app()->pdo->createCommand('SELECT id FROM `subtitles` WHERE `hashs` = :hashs LIMIT 1;')->bindParams([ | ||
'hashs' => $file_md5 | ||
])->queryOne(); | ||
|
||
if ($exist_id !== false) { | ||
$this->buildCallbackFailMsg('file', 'This Subtitle has been upload before.'); | ||
} | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function flush() | ||
{ | ||
$title = $this->title ?: $this->file->getFileName(); | ||
|
||
app()->pdo->beginTransaction(); | ||
try { | ||
|
||
$ext = $this->file->getExtension(); | ||
app()->pdo->createCommand('INSERT INTO `subtitles`(`torrent_id`, `hashs` ,`title`, `filename`, `added_at`, `size`, `uppd_by`, `anonymous`, `ext`) | ||
VALUES (:tid, :hashs, :title, :filename, NOW(), :size, :upper, :anonymous, :ext)')->bindParams([ | ||
'tid' => $this->torrent_id, 'hashs' => $this->hashs, | ||
'title' => $title, 'filename' => $this->file->getBaseName(), | ||
'size' => $this->file->size, 'upper' => app()->site->getCurUser()->getId(), | ||
'anonymous' => $this->anonymous, 'ext' => $ext | ||
])->execute(); | ||
$id = app()->pdo->getLastInsertId(); | ||
$file_loc = app()->getPrivatePath('subs') . DIRECTORY_SEPARATOR . $id . '.' . $ext; | ||
$this->file->saveAs($file_loc); | ||
app()->pdo->commit(); | ||
} catch (\Exception $e) { | ||
if (isset($file_loc)) unlink($file_loc); | ||
app()->pdo->rollback(); | ||
throw $e; | ||
} | ||
app()->redis->del('Site:subtitle_sub_size:string'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 8/8/2019 | ||
* Time: 10:09 AM | ||
*/ | ||
|
||
namespace apps\models\form\Traits; | ||
|
||
|
||
trait isValidSubtitleTrait | ||
{ | ||
public $id; | ||
|
||
protected $subtitle; | ||
|
||
public static function inputRules() | ||
{ | ||
return [ | ||
'id' => 'required | Integer' | ||
]; | ||
} | ||
|
||
public static function callbackRules() | ||
{ | ||
return ['isValidSubtitle']; | ||
} | ||
|
||
protected function isValidSubtitle() | ||
{ | ||
$sub_id = $this->getData('id'); | ||
|
||
$this->subtitle = app()->pdo->createCommand('SELECT * FROM `subtitles` WHERE id = :sid LIMIT 1;')->bindParams([ | ||
'sid' => $sub_id | ||
])->queryOne(); | ||
|
||
if ($this->subtitle === false) { | ||
$this->buildCallbackFailMsg('file', 'File not found'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.