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

Commit

Permalink
feat(Subtitle/Delete): Add Subtitle Delete support
Browse files Browse the repository at this point in the history
1. Add Subtitle Search support with params '&tid=\d+' ('&torrent_id=\d+'),
   '&letter=[A-Za-z]' , '&search=[^&]+?'
2. Add Part Subtitle Delete support
3. Add link refs to subtitle in torrent/details page
  • Loading branch information
Rhilip committed Aug 9, 2019
1 parent e366938 commit e3a0b18
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 38 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
- **gitignore:** Add ignore of `/backup` folder

### Feat
- **Category:** Add Default sprite image of category
- **Category:** Add Image and class_name support
- **Category:** Add Categories Manage Pane
- **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
Expand All @@ -21,8 +21,9 @@
- **Process:** Clean Components before sleep
- **RateLimit:** Add actionRateLimitCheckTrait
- **Redis:** Add mutiDelete() function for Redis
- **Tracker:** Add `retry in` field when failed
- **Subtitle:** Add Base Subtitle Page
- **Tracker:** Move From Timer to Process
- **Tracker:** Add `retry in` field when failed
- **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
18 changes: 10 additions & 8 deletions apps/controllers/SubtitlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,35 @@ public function actionIndex()
return $this->actionSearch();
}

public function actionSearch()
public function actionSearch($upload = null)
{
$search = new Subtitles\SearchForm();
if (false === $success = $search->validate()) {
return $this->render('action/action_fail');
return $this->render('action/action_fail', ['msg' => $search->getError()]);
}
return $this->render('subtitles/search',['search' => $search]);
return $this->render('subtitles/search', ['search' => $search, 'upload_mode' => $upload]);
}

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
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();

return $this->actionSearch(true);
}

public function actionDownload()
{
$download = new Subtitles\DownloadForm();
if (false === $success = $download->validate()) {
return $this->render('action/action_fail');
return $this->render('action/action_fail', ['msg' => $download->getError()]);
}

return $download->sendFileContentToClient();
Expand All @@ -56,9 +57,10 @@ public function actionDelete()
{
$delete = new Subtitles\DeleteForm();
if (false === $success = $delete->validate()) {
return $this->render('action/action_fail'); // TODO add redirect
return $this->render('action/action_fail', ['msg' => $delete->getError()]); // TODO add redirect
} else {
return $this->render('action/action_success'); // TODO add redirect
$delete->flush();
return $this->render('action/action_success',['redirect' => '/subtitles']); // TODO add redirect
}
}
}
3 changes: 3 additions & 0 deletions apps/libraries/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Constant
const trackerToDealQueue = 'Tracker:to_deal_queue:list';
const trackerBackupQueue = 'Tracker:backup_queue:list';

// Site Status
const siteSubtitleSize = 'Site:subtitle_size:string';

public static function userContent($uid)
{
return 'User:user_' . $uid . '_content:hash';
Expand Down
54 changes: 53 additions & 1 deletion apps/models/form/Subtitles/DeleteForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,62 @@

namespace apps\models\form\Subtitles;

use apps\libraries\Constant;
use apps\models\form\Traits\isValidSubtitleTrait;

use Rid\Validators\Validator;

class DeleteForm extends Validator
{
// TODO

use isValidSubtitleTrait;

public $reason;

protected $_autoload_data = true;
protected $_autoload_data_from = ['post'];

public static function inputRules()
{
return [
'id' => 'required | Integer',
'reason' => 'required'
];
}

public static function callbackRules()
{
return ['isValidSubtitle', 'canCurUserManagerSubs'];
}

protected function canCurUserManagerSubs()
{
$curuser = app()->site->getCurUser();
if (!$curuser->isPrivilege('manage_subtitles') || // not submanage_class
$this->subtitle['uppd_by'] != $curuser->getId() // not Subtitle 'owner'
) {
$this->buildCallbackFailMsg('Privilege', 'You can\'t Modify Subtitle.');
}
}

public function flush()
{
$filename = $this->id . '.' . $this->subtitle['ext'];
$file_loc = app()->getPrivatePath('subs') . DIRECTORY_SEPARATOR . $filename;

app()->pdo->createCommand('DELETE FROM subtitles WHERE id = :sid')->bindParams([
'sid' => $this->subtitle['id']
])->execute();
unlink($file_loc);

// TODO Delete uploader bonus

if ($this->subtitle['uppd_by'] != app()->site->getCurUser()->getId()) {
app()->site->sendPM(0,$this->subtitle['uppd_by'],'msg_your_sub_deleted','msg_deleted_your_sub');
}

// TODO add user detail
app()->site->writeLog('Subtitle \'' . $this->subtitle['title'] . '\'(' . $this->subtitle['id'] .') was deleted by ' . app()->site->getCurUser()->getUsername());
app()->redis->del(Constant::siteSubtitleSize);
}
}
45 changes: 41 additions & 4 deletions apps/models/form/Subtitles/SearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,63 @@

namespace apps\models\form\Subtitles;

use apps\libraries\Constant;

use Rid\Validators\Pager;

class SearchForm extends Pager
{

public $search;
public $letter;

public $torrent_id;
public $tid;

protected $_autoload_data = true;
protected $_autoload_data_from = ['get'];

public static function inputRules()
{
return [
'tid' => 'Integer',
'letter' => 'Alpha | MaxLength(max=1)'
];
}

protected function getRemoteTotal(): int
{
return app()->pdo->createCommand('SELECT COUNT(`id`) FROM `subtitles`')->queryScalar(); // TODO
$search = $this->getData('search');
$letter = $this->getData('letter');
$tid = $this->getData('torrent_id') ?? $this->getData('tid');
return app()->pdo->createCommand([
['SELECT COUNT(`id`) FROM `subtitles` WHERE 1=1 '],
['AND torrent_id = :tid ', 'if' => !is_null($tid), 'params' => ['tid' => $tid]],
['AND title LIKE :search ', 'if' => !is_null($search) , 'params' => ['search' => "%$search%"]],
['AND title LIKE :letter ', 'if' => is_null($search) && !is_null($letter) , 'params' => ['letter' => "$letter%"]],
])->queryScalar(); // TODO
}

protected function getRemoteData(): array
{
return app()->pdo->createCommand('SELECT * FROM `subtitles` ORDER BY id DESC')->queryAll(); // TODO
$search = $this->search;
$letter = $this->letter;
$tid = $this->torrent_id ?? $this->tid;
return app()->pdo->createCommand([
['SELECT * FROM `subtitles` WHERE 1=1 '],
['AND torrent_id = :tid ', 'if' => !is_null($tid), 'params' => ['tid' => $tid]],
['AND title LIKE :search ', 'if' => !is_null($search) , 'params' => ['search' => "%$search%"]],
['AND title LIKE :letter ', 'if' => is_null($search) && !is_null($letter) , 'params' => ['letter' => "$letter%"]],
['ORDER BY id DESC '],
['LIMIT :offset, :rows', 'params' => ['offset' => $this->offset, 'rows' => $this->limit]],
])->queryAll();
}

public function getSubsSizeSum()
{
if (false === $size = app()->redis->get('Site:subtitle_sub_size:string')) {
if (false === $size = app()->redis->get(Constant::siteSubtitleSize)) {
$size = app()->pdo->createCommand('SELECT SUM(`size`) FROM `subtitles`')->queryScalar();
app()->redis->set('Site:subtitle_sub_size:string', $size);
app()->redis->set(Constant::siteSubtitleSize, $size);
}
return $size;
}
Expand Down
8 changes: 5 additions & 3 deletions apps/models/form/Subtitles/UploadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace apps\models\form\Subtitles;


use apps\libraries\Constant;
use apps\models\form\Traits\isValidTorrentTrait;
use Rid\Http\UploadFile;
use Rid\Validators\Validator;
Expand All @@ -30,6 +31,8 @@ class UploadForm extends Validator
protected $_autoload_data = true;
protected $_autoload_data_from = ['post', 'files'];

const SubtitleExtension = ['sub', 'srt', 'zip', 'rar', 'ace', 'txt', 'ssa', 'ass', 'cue'];

public static function defaultData()
{
return [
Expand All @@ -44,7 +47,7 @@ public static function inputRules()
'file' => [
['Required'],
['Upload\Required'],
['Upload\Extension', ['allowed' => ['sub', 'srt', 'zip', 'rar', 'ace', 'txt', 'ssa', 'ass', 'cue']]],
['Upload\Extension', ['allowed' => static::SubtitleExtension]],
['Upload\Size', ['size' => config('upload.max_subtitle_file_size') . 'B']]
],
'anonymous' => [
Expand Down Expand Up @@ -82,7 +85,6 @@ public function flush()

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([
Expand All @@ -100,6 +102,6 @@ public function flush()
app()->pdo->rollback();
throw $e;
}
app()->redis->del('Site:subtitle_sub_size:string');
app()->redis->del(Constant::siteSubtitleSize);
}
}
1 change: 1 addition & 0 deletions apps/public/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,4 @@ body{background-color:#f6f6f6}
#torrent_structure li div.list span.icon{color:#009;padding:2px}
#torrent_structure li span.title{font-weight:bold}

#subs_search {margin-bottom: 20px}
14 changes: 14 additions & 0 deletions apps/public/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,18 @@ jQuery(document).ready(function () {
cat_remove_form.submit();
}
});

$('.subs_delete').click(function () {
let that = $(this), subs_delete_form = $('#subs_delete_form');
let sub_id = that.data('id');
let reason = prompt("What's delete reason?");

if (reason.length > 0 && subs_delete_form) {
subs_delete_form.find("input[name='id']").val(sub_id);
subs_delete_form.find("input[name='reason']").val(reason);
subs_delete_form.submit();
} else {
alert('Empty delete reason or no subtitle delete form exist.')
}
})
});
12 changes: 6 additions & 6 deletions apps/views/links/manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
<table class="table" id="links_manager_table">
<thead>
<tr>
<td>Id</td>
<td>Link</td>
<td>Status</td>
<td>Contact</td>
<td>Reason</td>
<td>Modify</td>
<th>Id</th>
<th>Link</th>
<th>Status</th>
<th>Contact</th>
<th>Reason</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
Expand Down
Loading

0 comments on commit e3a0b18

Please sign in to comment.