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

Commit

Permalink
feat(Torrent): Per-add torrent edit
Browse files Browse the repository at this point in the history
1. Move torrent upload back to route 'torrent/upload'
2.
  • Loading branch information
Rhilip committed Oct 2, 2019
1 parent 02cc251 commit f06b342
Show file tree
Hide file tree
Showing 19 changed files with 586 additions and 186 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<a name="unreleased"></a>
## [Unreleased]

### Docs
- **Sponsor:** Add Sponsor `MeiHeZi` (ae612e0)


<a name="v0.1.6-alpha"></a>
## [v0.1.6-alpha] - 2019-09-20
Expand Down
2 changes: 1 addition & 1 deletion application/Controllers/CaptchaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function actionIndex()
$captcha = new Captcha([
'width' => 150,
'height' => 40,
'fontFile' => RIDPT_ROOT . '/public/static/fonts/Times New Roman.ttf', // FIXME
'fontFile' => RIDPT_ROOT . '/public/static/fonts/Times New Roman.ttf',
'fontSize' => 20,
'wordNumber' => 6,
'angleRand' => [-20, 20],
Expand Down
2 changes: 1 addition & 1 deletion application/Controllers/LinksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function actionApply()
$success = $form->validate();
if ($success) {
$form->flush();
return $this->render('action/success', ['msg' => 'Thanks you to apply links, Our team will check it ASAP.']); // FIXME
return $this->render('action/success', ['msg' => __('form.link_apply.msg_success')]);
} else {
return $this->render('action/fail', ['msg' => $form->getError()]);
}
Expand Down
1 change: 0 additions & 1 deletion application/Controllers/RssController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class RssController extends Controller
{
public function actionIndex()
{
// FIXME add torrent search
$feed = new FeedForm();
if (false === $feed->validate()) {
return $this->render('action/fail', ['msg' => $feed->getError()]);
Expand Down
45 changes: 45 additions & 0 deletions application/Controllers/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,34 @@

use Rid\Http\Controller;

use Exception;

class TorrentController extends Controller
{
public function actionUpload()
{
// TODO Check user upload pos
if (app()->request->isPost()) {
$uploadForm = new Torrent\UploadForm();
$uploadForm->setInput(app()->request->post());
$uploadForm->setFileInput(app()->request->files());
$success = $uploadForm->validate();
if (!$success) {
return $this->render('action/fail', ['title' => 'Upload Failed', 'msg' => $uploadForm->getError()]);
} else {
try {
$uploadForm->flush();
} catch (Exception $e) {
return $this->render('action/fail', ['title' => 'Upload Failed', 'msg' => $e->getMessage()]);
}

return app()->response->redirect('/torrent/details?id=' . $uploadForm->getId());
}
} else {
return $this->render('torrent/upload');
}
}

public function actionDetails()
{
$details = new Torrent\DetailsForm();
Expand All @@ -27,7 +53,26 @@ public function actionDetails()

public function actionEdit() // TODO
{
$edit = new Torrent\EditForm();

if (app()->request->isPost()) {
$edit->setInput(app()->request->get() + app()->request->post());
$success = $edit->validate();
if (!$success) {
return $this->render('action/fail', ['msg' => $edit->getError()]);
} else {
$edit->flush();
return app()->response->redirect('/torrent/details?id=' . $edit->getTorrent()->getId());
}
} else {
$edit->setInput(app()->request->get());
$permission_check = $edit->checkUserPermission();
if ($permission_check === false) {
return $this->render('action/fail', ['msg' => $edit->getError()]);
} else {
return $this->render('torrent/edit', ['edit' => $edit]);
}
}
}

public function actionSnatch()
Expand Down
25 changes: 0 additions & 25 deletions application/Controllers/TorrentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,6 @@ public function actionSearch()
return $this->render('torrents/search', ['search' => $search]);
}

public function actionUpload()
{
// TODO Check user upload pos
if (app()->request->isPost()) {
$uploadForm = new Torrents\UploadForm();
$uploadForm->setInput(app()->request->post());
$uploadForm->setFileInput(app()->request->files());
$success = $uploadForm->validate();
if (!$success) {
return $this->render('action/fail', ['title' => 'Upload Failed', 'msg' => $uploadForm->getError()]);
} else {
try {
$uploadForm->flush();
} catch (\Exception $e) {
return $this->render('action/fail', ['title' => 'Upload Failed', 'msg' => $e->getMessage()]);
}

return app()->response->redirect('/torrent/details?id=' . $uploadForm->getId());
}
} else {
return $this->render('torrents/upload');
}

}

public function actionTags()
{
$pager = new Torrents\TagsForm();
Expand Down
253 changes: 253 additions & 0 deletions application/Models/Form/Torrent/EditForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 10/2/2019
* Time: 2019
*/

namespace App\Models\Form\Torrent;


use App\Libraries\Constant;
use App\Models\Form\Traits\isValidTorrentTrait;
use App\Models\Torrent;

use Rid\Http\UploadFile;
use Rid\Validators\Validator;

class EditForm extends Validator
{
use isValidTorrentTrait;

public $category;
public $title;
public $subtitle = '';
public $links;
public $descr;

/**
* The upload file class of Nfo
*
* @var UploadFile
*/
public $nfo;

public $anonymous = 0; // If user upload this torrent Anonymous
public $hr = 0; // If This torrent require hr check

// Quality
public $audio = 0; /* 0 is default value. */
public $codec = 0;
public $medium = 0;
public $resolution = 0;

public $team = 0;

public $tags;

/**
* @return array
*/
public static function baseTorrentRules(): array
{
$rules = [
'title' => 'required',
'category' => [
['required'], ['Integer'],
['InList', ['list' => array_map(function ($cat) {
return $cat['id'];
}, app()->site->ruleCanUsedCategory()
)]]
],
'descr' => 'required',
];

// Add Quality Valid
foreach (app()->site->getQualityTableList() as $quality => $title) {
$quality_id_list = [0];
// IF enabled this quality field , then load it value list from setting
// Else we just allow the default value 0 to prevent cheating
if (config('torrent_upload.enable_quality_' . $quality)) {
foreach (app()->site->ruleQuality($quality) as $cat) {
$quality_id_list[] = $cat['id'];
}
}

$rules[$quality] = [
['Integer'],
['InList', ['list' => $quality_id_list]]
];
}

// Add Team id Valid
$team_id_list = [0];
if (config('torrent_upload.enable_teams')) {
foreach (app()->site->ruleTeam() as $team) {
if (app()->auth->getCurUser()->getClass() >= $team['class_require']) {
$team_id_list[] = $team['id'];
}
}
}

$rules['team'] = [
['Integer'],
['InList', ['list' => $team_id_list]]
];

// Add Flag Valid
// Notice: we don't valid if user have privilege to use this value,
// Un privilege flag will be rewrite in rewriteFlags() when call flush()
if (config('torrent_upload.enable_anonymous')) {
$rules['uplver'] = [
['InList', ['list' => [0, 1]]]
];
}
if (config('torrent_upload.enable_hr')) {
$rules['hr'] = [
['InList', ['list' => [0, 1]]]
];
}

return $rules;
}

/**
* @return array
*/
public static function inputRules(): array
{
$rules = static::baseTorrentRules();
$rules['id'] = 'Required | Integer';

if (config('torrent_upload.enable_upload_nfo') // Enable nfo upload
&& app()->auth->getCurUser()->isPrivilege('upload_nfo_file') // This user can upload nfo
) {
$rules['nfo_action'] = [
['required'],
['InList', ['list' => ['keep', 'remove', 'update']]]
];

// Nfo file upload
if (app()->request->post('nfo_action', 'keep') == 'update') {
$rules['nfo'] = [
['Upload\Extension', ['allowed' => ['nfo', 'txt']]],
['Upload\Size', ['size' => config('upload.max_nfo_file_size') . 'B']]
];
}
}

if (app()->auth->getCurUser()->isPrivilege('manage_torrents')) {
$rules['status'] = [
['required'],
['InList', ['list' => Torrent::TORRENT_STATUSES]]
];
}

return $rules;
}

public static function callbackRules(): array
{
return ['isExistTorrent', 'checkUserPermission'];
}

public function checkUserPermission()
{
// Get Torrent if not in validate
if ($this->torrent === null) {
$this->isExistTorrent();
if ($this->getError()) { return false;
}
}

if (app()->auth->getCurUser()->getId() != $this->torrent->getOwnerId() // User is torrent owner
|| !app()->auth->getCurUser()->isPrivilege('manage_torrents') // User can manager torrents
) {
$this->buildCallbackFailMsg('owner', 'You can\'t edit torrent which is not belong to you.');
return false;
}

// TODO Check Other Permission and store in class pro

return true;
}

public function flush()
{
$this->rewriteFlags();
$tags = $this->getTags();
app()->pdo->createCommand('
UPDATE `torrents` SET title = :title, subtitle = :subtitle,
category = :category, team = :team, quality_audio = :audio, quality_codec = :codec,
quality_medium = :medium, quality_resolution = :resolution,
descr = :descr, tags = JSON_ARRAY(:tags), nfo=:nfo,uplver = :uplver, hr = :hr
WHERE id = :tid')->bindParams([
'tid' => $this->id,
'title' => $this->title, 'subtitle' => $this->subtitle,
'category' => $this->category, 'team' => $this->team,
'audio' => $this->audio, 'codec' => $this->codec, 'medium' => $this->medium, 'resolution' => $this->resolution,
'descr' => $this->descr, 'tags' => $tags, 'nfo' => $this->getInputNfo(),
'uplver' => $this->anonymous, 'hr' => $this->hr
])->execute();

app()->redis->del(Constant::torrentContent($this->id));
// Delete cache
}

private function getInputNfo()
{
$action = $this->getInput('nfo_action', 'keep');
if ($action == 'remove') {
return '';
} elseif ($action == 'update') {
return $this->nfo->getFileContent();
} else {
return $this->torrent->getNfo(false);
}
}

// Check and rewrite torrent flags based on site config and user's privilege of upload flags
protected function rewriteFlags()
{
foreach (['anonymous', 'hr'] as $flag) {
$config = config('torrent_upload.enable_' . $flag);
if ($config == 2) { // if global config force enabled this flag
$this->$flag = 1;
} elseif ($config == 0) { // if global config disabled this flag
$this->$flag = 0;
} else { // check if user can use this flag
if (!app()->auth->getCurUser()->isPrivilege('upload_flag_' . $flag)) {
$this->$flag = 0;
}
}
}
}

protected function getTags(): array
{
$tags_list = [];
if (config('torrent_upload.enable_tags')) {
$tags = str_replace(',', ' ', $this->tags);
$tags_list = explode(' ', $tags);
$tags_list = array_slice($tags_list, 0, 10); // Get first 10 tags

if (!config('torrent_upload.allow_new_custom_tags')) {
$rule_pinned_tags = array_keys(app()->site->rulePinnedTags());
$tags_list = array_intersect($rule_pinned_tags, $tags_list);
}
}

return $tags_list;
}

protected function updateTagsTable(array $tags)
{
foreach ($tags as $tag) {
app()->pdo->createCommand('INSERT INTO tags (tag) VALUES (:tag) ON DUPLICATE KEY UPDATE `count` = `count` + 1;')->bindParams([
'tag' => $tag
])->execute();
}
}

}
Loading

0 comments on commit f06b342

Please sign in to comment.