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

Commit

Permalink
feat(Torrent): Finish Torrent's Structure part
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Mar 16, 2019
1 parent 17be3b6 commit b95112b
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 33 deletions.
84 changes: 84 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,94 @@
<a name="unreleased"></a>
## [Unreleased]

### Build
- **Compatible:** Remove Compatible Model
- **Environment:** Upgrade Dependency of Mysql and PHP

### Chore
- **Git:** Fix gitkeep file lost
- **Server:** Change the server printing
- **Static File:** Move fonts to public path

### Docs
- **Debug:** End Error Handle Debug
- **Demo:** Change demo site link
- **Readme:** Add Demo Site Information
- **Readme:** Update Readme.md

### Feat
- **Favour:** Add full favour support
- **Framework:** Add Record of execute sql and redis key hit
- **Front:** Add lib `notice.js`
- **Helper:** Add Simple String crypt helper
- **RSS:** Start Build rss feed
- **Torrent:** Save Torrent File Structure in Table `torrents`
- **Torrent:** Clean temp upload torrent file after success upload
- **Torrent:** Add Torrent Content Cache
- **Torrent:** Add Torrent FileList View
- **Torrent Upload:** Add utf-8 path support for torrent upload
- **UBB:** Add ubb converter
- **User:** Add User Confirm Support
- **User:** Add user load by passkey
- **User:** Add User Active Seed/Leech Count
- **View:** Add ram use status in footer
- **i18n:** Add i18n Support

### Fix
- **Auth:** Update description texts in `/auth/login`
- **Auth:** Fix 'errors/action_fail' view should not be touch in AuthController
- **Auth:** Fix layout fo Auth Point
- **Bencode:** Add full parse check
- **Compatible:** Fix Compatible Model not work
- **Database:** Change Foreign Key behaviour
- **Nameplace:** Fix error nameplace of compatible
- **Namespace:** Namespace miss change
- **Upload:** Fix Upload Torrent can't save in database
- **UserTrackerStatus:** Fix redis cache key miss
- **View:** Fix cost time miss in render
- **View:** Fix View Don't change after service reload
- **View:** Fix `admin/redis_key` of wrong array echo

### Perf
- **Admin:** Call redis key info only when pattern give
- **Bencode:** Remove Bencode Library `sandfoxme/bencode`
- **Config:** Create \Swoole\Table as Dynamic Config Provider in Master Process
- **Error:** Fix Error Page
- **Mailer:** Make Mailer Provider extends BaseObject but not Component
- **Middleware:** Fix Middleware behaviour
- **Psr:** Use Psr\Log to simple Rid\Log Component
- **Session:** Not resend set-cookies header for session
- **User:** Quickly get user peer status
- **Validator:** load base rules from call parent method
- **i18n:** Quick load

### Refactor
- **DynamicConfig:** Remove DynamicConfig provider by Redis
- **Framework:** Resort Component folder
- **Framework:** Rename Mix to Rid
- **Framework:** Refactor framework from upstream
- **Helpers:** Remove JsonHelper
- **Output:** Remove \Rid\Console\Output
- **Validator:** Change Validator Provider
- **View:** Change template system
- **XML:** Remove XML support

### Revert
- **CSS_TAG:** re-add debug for css_tag
- **Config:** Add back Dynamic Config provider by Redis
- **UploadFile:** Add back some attributes of UploadFile

### Style
- **CSS:** Add css tag
- **Git:** Fix git style in private path
- **Validator:** Separate File and Captcha Validate to Trait object
- **View:** Change View Layout


<a name="v0.1.2-alpha"></a>
## [v0.1.2-alpha] - 2019-02-01
### Docs
- **Release:** Release v0.1.2-alpha
- **changelog:** Use `git-chglog` to generate CHANGELOG.md

### Feat
Expand Down
5 changes: 3 additions & 2 deletions apps/controllers/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ public function actionDownload()
return $torrent->getDownloadDict(true);
}

public function actionStructure() {
public function actionStructure()
{
$tid = app()->request->get('id');

$torrent = new Torrent($tid); // If torrent is not exist or can't visit , a notfound exception will throw out........
return $this->render('torrent/structure',['torrent'=> $torrent]);
return $this->render('torrent/structure', ['torrent' => $torrent]);
}
}
12 changes: 12 additions & 0 deletions apps/public/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,15 @@ nav#menu .layui-nav-item{line-height:45px}

.torrent-filelist-modal{width:700px}
ul#torrent-filelist ul{margin-left:1.25em}

#torrent-structure ul{margin-left:15px}
#torrent-structure ul,li{list-style-type:none;color:#000}
#torrent-structure li div.string{padding:3px}
#torrent-structure li div.integer{padding:3px}
#torrent-structure li div.dictionary{padding:3px}
#torrent-structure li div.list{padding:3px}
#torrent-structure li div.string span.icon{color:#090;padding:2px}
#torrent-structure li div.integer span.icon{color:#990;padding:2px}
#torrent-structure li div.dictionary span.icon{color:#909;padding:2px}
#torrent-structure li div.list span.icon{color:#009;padding:2px}
#torrent-structure li span.title{font-weight:bold}
41 changes: 24 additions & 17 deletions apps/public/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ layui.use(['layer', 'form', 'element', 'laypage', 'jquery'], function () {
let layer = layui.layer;
let api_point = '/api/v1';

// Convert ubbcode blcok text to html
$(".ubbcode-block").html(function (index, oldhtml) {
/*
Because:
1. Cloudflare or other CDN may clean the newline characters like `\n`
2. our backend use nl2br() to Inserts HTML line breaks before all newlines in a string,
It's needed to to remove the exist `\n` (if case 1 not happened), and change the html tag `<br />` to `\n`,
then feed to our XBBCODE converter for safety output.
*/
oldhtml = oldhtml.trim()
.replace(/\n/ig, '')
.replace(/<br ?\/?>/ig, '\n');
return XBBCODE.process({text: oldhtml}).html;
});
// TODO Add [hide] support

// Add/Remove favour action
$('.torrent-favour').click(function () {
let that = $(this);
Expand Down Expand Up @@ -72,7 +88,7 @@ layui.use(['layer', 'form', 'element', 'laypage', 'jquery'], function () {
btn: [],
anim: 5,
shadeClose: true, //开启遮罩关闭
area: '700px',
area: ['700px','500px'],
content: "<ul id='torrent-filelist'>" + list_worker(file_list) + "</ul>",
success: function (layero, index) {
$('#torrent-filelist a').click(function () {
Expand All @@ -94,21 +110,12 @@ layui.use(['layer', 'form', 'element', 'laypage', 'jquery'], function () {
layer.alert(res.errors.join(', '), {icon: 2});
}
})

})

// Convert ubbcode blcok text to html
$(".ubbcode-block").html(function (index, oldhtml) {
/*
Because:
1. Cloudflare or other CDN may clean the newline characters like `\n`
2. our backend use nl2br() to Inserts HTML line breaks before all newlines in a string,
It's needed to to remove the exist `\n` (if case 1 not happened), and change the html tag `<br />` to `\n`,
then feed to our XBBCODE converter for safety output.
*/
oldhtml = oldhtml.trim()
.replace(/\n/ig, '')
.replace(/<br ?\/?>/ig, '\n');
return XBBCODE.process({text: oldhtml}).html;
});

// For torrents structure page
if ($('#torrent-structure').length) {
$('#torrent-structure div.dictionary,div.list').click(function () {
$(this).next('ul').toggle();
});
}
});
22 changes: 12 additions & 10 deletions apps/views/layout/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<title><?= app()->config->get('base.site_name') ?> :: <?= $this->e($this->section('title') ?? '') ?> -- Powered by <?= app()->config->get('base.site_generator') ?></title>

<link rel="stylesheet" href="/lib/layui/src/css/layui.css"> <?php /** https://www.layui.com/doc/ */ ?>
<link rel="stylesheet" href="/lib/notice.js/dist/noticejs.css"> <?php /** https://github.com/alihesari/notice.js */ ?>
<link rel="stylesheet" href="/lib/fontAwesome/css/all.css"> <?php /** https://fontawesome.com/icons?d=gallery */ ?>

<!-- Custom styles for this template -->
Expand Down Expand Up @@ -49,7 +48,7 @@
<li class="layui-nav-item<?= /** @noinspection PhpUndefinedMethodInspection */ $this->uri('/forums', ' layui-this'); ?>"><!--suppress HtmlUnknownTarget --><a href="/forums"><?= __('nav_forums') ?></a></li>
<li class="layui-nav-item<?= /** @noinspection PhpUndefinedMethodInspection */ $this->uri('/torrents', ' layui-this'); ?>"><!--suppress HtmlUnknownTarget --><a href="/torrents"><?= __('nav_torrents') ?></a></li>
<li class="layui-nav-item<?= /** @noinspection PhpUndefinedMethodInspection */ $this->uri('/torrents/request', ' layui-this'); ?>"><!--suppress HtmlUnknownTarget --><a href="/torrents/request"><?= __('nav_requests') ?></a></li>
<li class="layui-nav-item<?= /** @noinspection PhpUndefinedMethodInspection */ $this->uri('/torrent/upload', ' layui-this'); ?>"><!--suppress HtmlUnknownTarget --><a href="/torrents/upload"><?= __('nav_upload') ?></a></li>
<li class="layui-nav-item<?= /** @noinspection PhpUndefinedMethodInspection */ $this->uri('/torrent/upload', ' layui-this'); ?>"><!--suppress HtmlUnknownTarget --><a href="/torrent/upload"><?= __('nav_upload') ?></a></li>
<li class="layui-nav-item<?= /** @noinspection PhpUndefinedMethodInspection */ $this->uri('/subtitles', ' layui-this'); ?>"><!--suppress HtmlUnknownTarget --><a href="/subtitles"><?= __('nav_subtitles') ?></a></li>
<li class="layui-nav-item<?= /** @noinspection PhpUndefinedMethodInspection */ $this->uri('/site/topten', ' layui-this'); ?>"><!--suppress HtmlUnknownTarget --><a href="/site/topten"><?= __('nav_topten') ?></a></li>
<li class="layui-nav-item<?= /** @noinspection PhpUndefinedMethodInspection */ $this->uri('/site/about', ' layui-this'); ?>"><!--suppress HtmlUnknownTarget --><a href="/site/about"><?= __('nav_faq') ?></a></li>
Expand All @@ -58,17 +57,21 @@
</div>
<div class="layui-row header-info">
<div class="pull-left">
Welcome Back, <a href="/user"><?= app()->user->getUsername() ?></a>&nbsp;
<span><!--suppress HtmlUnknownTarget --><a href="/auth/logout">[Logout]</a></span>&nbsp;
Welcome Back, <a href="/user" data-user-id="<?= app()->user->getId() ?>"><?= app()->user->getUsername() ?></a>&nbsp;
<span data-item="logout"><!--suppress HtmlUnknownTarget --><a href="/auth/logout">[Logout]</a></span>&nbsp;
<?php if (app()->user->getClass(true) > \Rid\User\UserInterface::ROLE_FORUM_MODERATOR): ?>
<span><!--suppress HtmlUnknownTarget --><a href="/admin">[Admin Panel]</a></span>&nbsp;
<?php endif; ?>
<span><!--suppress HtmlUnknownTarget --><a href="/torrents/favour">[Favour]</a></span>&nbsp;
<span data-item="favour"><!--suppress HtmlUnknownTarget --><a href="/torrents/favour">[Favour]</a></span>&nbsp;
<br>
<span>Ratio: <?= app()->user->getRatio() ?></span>&nbsp;
<span>Uploaded: <?= $this->e(app()->user->getUploaded(), 'format_bytes') ?></span>&nbsp;
<span>Downloaded: <?= $this->e(app()->user->getDownloaded(), 'format_bytes') ?></span>&nbsp;
<span>BT Activity:
<span data-item="ratio" data-ratio="<?= $this->e(app()->user->getRatio()) ?>">
Ratio: <?= app()->user->getRatio() ?></span>&nbsp;
<span data-item="uploaded" data-uploaded="<?= $this->e(app()->user->getUploaded()) ?>">
Uploaded: <?= $this->e(app()->user->getUploaded(), 'format_bytes') ?></span>&nbsp;
<span data-item="download" data-downloaded="<?= $this->e(app()->user->getDownloaded()) ?>">
Downloaded: <?= $this->e(app()->user->getDownloaded(), 'format_bytes') ?></span>&nbsp;
<span data-item="bt_activity" data-seeding="<?= app()->user->getActiveSeed() ?>" data-leeching="<?= app()->user->getActiveLeech() ?>">
BT Activity:
<span class="fas fa-arrow-up icon-seeding"></span>&nbsp;<?= app()->user->getActiveSeed() ?>&nbsp;
<span class="fas fa-arrow-down icon-leeching"></span>&nbsp;<?= app()->user->getActiveLeech() ?>&nbsp;
</span>&nbsp;
Expand Down Expand Up @@ -96,7 +99,6 @@
</footer>

<script src="/lib/layui/src/layui.js"></script>
<script src="/lib/notice.js/dist/notice.js"></script>
<script src="/static/js/bbcodeParser.js"></script>
<script src="/static/js/main.js?<?= $css_tag ?>"></script>
<?= $this->section('script') ?> <!-- Other temp script field -->
Expand Down
5 changes: 2 additions & 3 deletions apps/views/torrent/details.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<?php $this->start('title')?><?= $torrent->getTitle() ?><?php $this->end();?>

<?php $this->start('container')?>

<div class="text-center torrent-title-block">
<h1 class="torrent-title"><?= $torrent->getTitle() ?></h1>
<small class="torrent-subtitle"><em><?= $torrent->getSubtitle() ?: 'No Subtitle.' ?></em></small>
Expand Down Expand Up @@ -66,8 +65,8 @@
<a class="torrent-files" href="javascript:" data-tid="<?= $torrent->getId() ?>"><i class="fas fa-file fa-fw"></i>&nbsp;View Torrent's Files</a>
</div><!-- View Torrent's Files -->
<div class="torrent-action-item"><!--suppress HtmlUnknownTarget -->
<a class="torrent-structure" href="javascript:" data-tid="<?= $torrent->getId() ?>"><i class="fas fa-folder-open fa-fw"></i>&nbsp;View Torrent's Structure</a>
</div><!-- TODO View TorrentController's Structure -->
<a class="torrent-structure" href="/torrent/structure?id=<?= $torrent->getId() ?>"><i class="fas fa-folder-open fa-fw"></i>&nbsp;View Torrent's Structure</a>
</div><!-- View Torrent's Structure -->
</div>
</div>
<div class="layui-card" id="torrent-info-card">
Expand Down
55 changes: 55 additions & 0 deletions apps/views/torrent/structure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/3/16
* Time: 17:10
*
* @var League\Plates\Template\Template $this
* @var \apps\models\Torrent $torrent
*/

use Rid\Bencode\Bencode;

function torrent_structure_builder($array, $parent = "")
{
$ret = '';
foreach ($array as $item => $value) {
$value_length = strlen(Bencode::encode($value));
if (is_iterable($value)) { // It may `dictionary` or `list`
$type = is_indexed_array($value) ? 'list' : 'dictionary';
$ret .= "<li><div align='left' class='".$type ."'><a href='javascript:'> + <span class=title>[" . $item . "]</span> <span class='icon'>(" . ucfirst($type) . ")</span> <span class=length>[". $value_length . "]</span></a></div>";
$ret .= "<ul style='display:none'>" . torrent_structure_builder($value,$item) . "</ul></li>";
} else { // It may `interger` or `string`
$type = is_integer($value) ? 'integer' : 'string';
$value = ($parent == 'info' && $item == 'pieces') ? "0x".bin2hex(substr($value, 0, 25))."..." : $value; // Cut the info pieces....
$ret .="<li><div align=left class=". $type ."> - <span class=title>[". $item . "]</span> <span class=icon>(". ucfirst($type).")</span> <span class=length>[". $value_length ."]</span>: <span class=value>" . $value ."</span></div></li>";
}
}
return $ret;
}
?>

<?= $this->layout('layout/base') ?>

<?php $this->start('title')?>Torrents Structure<?php $this->end();?>

<?php $this->start('container')?>
<div class="text-center torrent-title-block">
<h1 class="torrent-title"><?= $torrent->getTitle() ?></h1>
<small class="torrent-subtitle"><em><?= $torrent->getSubtitle() ?: 'No Subtitle.' ?></em></small>
</div>

<div class="layui-row">
<div class="layui-col-md8 layui-col-md-offset2">
<div class="layui-card" id="torrent-structure-card">
<div class="layui-card-header"><b>Torrent Structure</b></div>
<div class="layui-card-body" id="torrent-structure-body">
<ul id="torrent-structure">
<?= torrent_structure_builder(['root' => $torrent->getRawDict()]); ?>
</ul>
</div>
</div>
</div>
</div>
<?php $this->end();?>
71 changes: 70 additions & 1 deletion framework/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ function __($string, $avg = null, $lang = null)
}

if (!function_exists('tgo')) {

/** 创建一个带异常捕获的协程
* @param $closure
*/
Expand Down Expand Up @@ -63,3 +62,73 @@ function println($expression)
echo $expression . PHP_EOL;
}
}

if (!function_exists('is_indexed_array')) {
/** 索引数组:所有键名都为数值型,注意字符串类型的数字键名会被转换为数值型。
* 判断数组是否为索引数组
* @param array $arr
* @return bool
*/
function is_indexed_array(array $arr): bool
{
if (is_array($arr)) {
return count(array_filter(array_keys($arr), 'is_string')) === 0;
}
return false;
}
}

if (!function_exists('is_continuous_indexed_array')) {
/** 连续索引数组:键名是连续性的数字。
* 判断数组是否为连续的索引数组
* 以下这种索引数组为非连续索引数组
* [
* 0 => 'a',
* 2 => 'b',
* 3 => 'c',
* 5 => 'd',
* ]
* @param array $arr
* @return bool
*/
function is_continuous_indexed_array(array $arr): bool
{
if (is_array($arr)) {
$keys = array_keys($arr);
return $keys == array_keys($keys);
}
return false;
}
}

if (!function_exists('is_assoc_array')) {
/** 关联数组:所有键名都为字符串型,注意字符串类型的数字键名会被转换为数值型。
* 判断数组是否为关联数组
* @param array $arr
* @return bool
*/
function is_assoc_array(array $arr): bool
{
if (is_array($arr)) {
// return !is_indexed_array($arr);
return count(array_filter(array_keys($arr), 'is_string')) === count($arr);
}
return false;
}
}

if (!function_exists('is_assoc_array')) {
/** 混合数组:键名既有数值型也有字符串型。
* 判断数组是否为混合数组
* @param array $arr
* @return bool
*/
function is_mixed_array(array $arr): bool
{
if (is_array($arr)) {
$count = count(array_filter(array_keys($arr), 'is_string'));
return $count !== 0 && $count !== count($arr);
}
return false;
}
}

0 comments on commit b95112b

Please sign in to comment.