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

Commit

Permalink
feat(Subtitle): Add Base Subtitle Page
Browse files Browse the repository at this point in the history
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
Rhilip committed Aug 9, 2019
1 parent 5555dbb commit 4fe52e5
Show file tree
Hide file tree
Showing 11 changed files with 464 additions and 5 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@

### Feat
- **Category:** Add Categories Manage Pane
- **Category:** Add Image and class_name support
- **Category:** Add Categories Support when upload torrent
- **Category:** Add Image and class_name support
- **Category:** Add Default sprite image of category
- **Crontab:** Move From Timer to Process
- **Editor:** Support wysibb editor
- **Gravatar:** Add support of gravatar
- **Pager:** Add Pager Support
- **Pager:** Torrents/{SearchForm,TagsForm}
- **Pager:** Add Pager Support
- **Process:** Add custom Process Support
- **Process:** Clean Components before sleep
- **RateLimit:** Add actionRateLimitCheckTrait
- **Redis:** Add mutiDelete() function for Redis
- **Tracker:** Move From Timer to Process
- **Tracker:** Add `retry in` field when failed
- **Tracker:** Move From Timer to Process
- **User:** Add Bonus And Unread Messsage count
- **UserInfo:** Add Cache Lock of user access_{time,ip} update
- **Validator:** Add autoload from requests function
Expand Down
1 change: 1 addition & 0 deletions apps/config/httpd.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
'max_request' => 3000, // 进程的最大任务数
'max_wait_time' => 60, // 退出等待时间
'package_max_length' => 6242880, // 最大上传包大小,单位 Bytes
'buffer_output_size' => 33554432, // 发送缓存区大小,影响向用户发送文件的最大大小,单位 Bytes
'reload_async' => true, // 异步安全重启
/* 'user' => 'www', // 子进程运行用户 */
],
Expand Down
64 changes: 64 additions & 0 deletions apps/controllers/SubtitlesController.php
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
}
}
}
17 changes: 17 additions & 0 deletions apps/models/form/Subtitles/DeleteForm.php
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
}
49 changes: 49 additions & 0 deletions apps/models/form/Subtitles/DownloadForm.php
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);
}
}
35 changes: 35 additions & 0 deletions apps/models/form/Subtitles/SearchForm.php
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;
}

}
105 changes: 105 additions & 0 deletions apps/models/form/Subtitles/UploadForm.php
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');
}
}
42 changes: 42 additions & 0 deletions apps/models/form/Traits/isValidSubtitleTrait.php
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');
}
}
}
10 changes: 8 additions & 2 deletions apps/models/form/Traits/isValidTorrentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@

trait isValidTorrentTrait
{
public $id;
public $torrent_id;
public $tid;
public $id;

/** @var Torrent */
protected $torrent;

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

/**
* @return Torrent
*/
Expand All @@ -28,7 +34,7 @@ public function getTorrent(): Torrent
}

protected function isExistTorrent() {
$tid = $this->getData('tid') ?? $this->getData('id');
$tid = $this->getData('torrent_id') ?? $this->getData('tid') ?? $this->getData('id');
$torrent_exist = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `torrents` WHERE `id` = :tid')->bindParams([
'tid' => $tid
])->queryScalar();
Expand Down
Loading

0 comments on commit 4fe52e5

Please sign in to comment.