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

Commit

Permalink
feat(torrent/nfo): Show nfo in details page
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Jul 20, 2019
1 parent a423577 commit a1f1d64
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 15 deletions.
21 changes: 21 additions & 0 deletions apps/controllers/api/v1/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,25 @@ public function actionFileList() {
return $this->buildMethodFailMsg('GET');
}
}

public function actionNfoFileContent()
{
if ($this->checkMethod('GET')) {
$filelist = new TorrentsForm();
$filelist->setData(app()->request->get());
$success = $filelist->validate();
if (!$success) {
return [
'success' => false,
'errors' => $filelist->getErrors()
];
} else {
$ret = $filelist->getNfoFileContent();
return array_merge(
['success' => true],
$ret
);
}
}
}
}
16 changes: 16 additions & 0 deletions apps/models/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Torrent
private $category;
private $descr;
private $uplver;
private $hr;

private $nfo;

/** @var array */
private $tags;
Expand Down Expand Up @@ -280,4 +283,17 @@ public function getTags(): array

return $this->tags;
}


public function hasNfo() {
return (boolean) $this->nfo;
}

/**
* @return mixed
*/
public function getNfo()
{
return $this->nfo;
}
}
54 changes: 53 additions & 1 deletion apps/models/api/v1/form/TorrentsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,64 @@ public function getFileList()
{
// Check if cache is exist if exist , just quick return
$filelist = app()->redis->hGet('Torrent:' . $this->tid . ':base_content', 'torrent_structure');
if ($filelist == false) {
if ($filelist === false) {
$filelist = app()->pdo->createCommand('SELECT `torrent_structure` FROM `torrents` WHERE `id`= :tid LIMIT 1')->bindParams([
'tid' => $this->tid
])->queryScalar();
// However, we don't cache it for cache safety reason.
}
return ['msg' => 'Get Filelist success', 'result' => json_decode($filelist, false)];
}

public function getNfoFileContent()
{
$nfo_file = app()->redis->hGet('Torrent:' . $this->tid . ':base_content', 'nfo');
if ($nfo_file === false) {
$nfo_file = app()->pdo->createCommand('SELECT `nfo` FROM `torrents` WHERE `id` = :tid LIMIT 1')->bindParams([
'tid' => $this->tid
])->queryScalar();
}

// Convert nfo
$nfo_file = self::code($nfo_file);

return ['msg' => 'Get Nfo File Content success','result' => $nfo_file];
}


// FIXME Code for Viewing NFO file

// code: Takes a string and does a IBM-437-to-HTML-Unicode-Entities-conversion.
// swedishmagic specifies special behavior for Swedish characters.
// Some Swedish Latin-1 letters collide with popular DOS glyphs. If these
// characters are between ASCII-characters (a-zA-Z and more) they are
// treated like the Swedish letters, otherwise like the DOS glyphs.
protected static function code($ibm_437, $swedishmagic = false)
{
$cf = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 8962, 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197, 201, 230, 198, 244, 246, 242, 251, 249, 255, 214, 220, 162, 163, 165, 8359, 402, 225, 237, 243, 250, 241, 209, 170, 186, 191, 8976, 172, 189, 188, 161, 171, 187, 9617, 9618, 9619, 9474, 9508, 9569, 9570, 9558, 9557, 9571, 9553, 9559, 9565, 9564, 9563, 9488, 9492, 9524, 9516, 9500, 9472, 9532, 9566, 9567, 9562, 9556, 9577, 9574, 9568, 9552, 9580, 9575, 9576, 9572, 9573, 9561, 9560, 9554, 9555, 9579, 9578, 9496, 9484, 9608, 9604, 9612, 9616, 9600, 945, 223, 915, 960, 931, 963, 181, 964, 934, 920, 937, 948, 8734, 966, 949, 8745, 8801, 177, 8805, 8804, 8992, 8993, 247, 8776, 176, 8729, 183, 8730, 8319, 178, 9632, 160);
$s = "";
for ($c = 0; $c < strlen($ibm_437); $c++) { // cyctle through the whole file doing a byte at a time.
$byte = $ibm_437[$c];
$ob = ord($byte);
if ($ob >= 127) { // is it in the normal ascii range
$s .= '&#' . $cf[$ob] . ';';
} else {
$s .= $byte;
}
}
if ($swedishmagic) {
$s = str_replace("\345", "\206", $s);
$s = str_replace("\344", "\204", $s);
$s = str_replace("\366", "\224", $s);

// couldn't get ^ and $ to work, even through I read the man-pages,
// i'm probably too tired and too unfamiliar with posix regexps right now.
$s = preg_replace("/([ -~])\305([ -~])/", "\\1\217\\2", $s);
$s = preg_replace("/([ -~])\304([ -~])/", "\\1\216\\2", $s);
$s = preg_replace("/([ -~])\326([ -~])/", "\\1\231\\2", $s);
$s = str_replace("\311", "\220", $s); //
$s = str_replace("\351", "\202", $s); //
}
return $s;
}
}
1 change: 0 additions & 1 deletion apps/public/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ body{background-color:#f6f6f6}

/* '/torrent/upload' */
#torrent_upload_table > tbody > tr > td:first-child{vertical-align:middle;text-align:right}
#torrent_file{display:inline;margin-right:10px;width:220px}

/* '/torrent/details' */
.torrent-details-block{}
Expand Down
45 changes: 39 additions & 6 deletions apps/public/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ function location_search_replace(new_params) {
}

jQuery(document).ready(function () {
// Cache
const cache_torrent_files = localforage.createInstance({name: 'torrent_files'});
const cache_torrent_nfo = localforage.createInstance({name: 'torrent_nfo'});

// Drop all support of IE 6-11
if ($.zui.browser.ie) {
$.zui.browser.tip();
Expand Down Expand Up @@ -164,7 +168,7 @@ jQuery(document).ready(function () {
// Torrent favour Add/Remove action
$('.torrent-favour').click(function () {
let that = $(this);
let tid = that.attr('data-tid');
let tid = that.data('tid');
let star = that.find(' > i');

$.post(api_point + '/torrent/bookmark', {'tid': tid}, function (res) {
Expand Down Expand Up @@ -201,12 +205,10 @@ jQuery(document).ready(function () {
}

$('.torrent-files').click(function () {
const torrent_files_localforage = localforage.createInstance({name: 'torrent_files'});

let that = $(this);
let tid = that.attr('data-tid');
let tid = that.data('tid');

torrent_files_localforage.getItem(tid, function (err, value) {
cache_torrent_files.getItem(tid, function (err, value) {
function list_worker(tree, par = '') {
let ret = '';
let size = 0;
Expand Down Expand Up @@ -250,13 +252,44 @@ jQuery(document).ready(function () {
build_file_tree(value);
} else {
$.getJSON(api_point + '/torrent/filelist', {'tid': tid}, function (res) {
torrent_files_localforage.setItem(tid, res);
cache_torrent_files.setItem(tid, res);
build_file_tree(res);
});
}
});
});

$('.torrent-nfo').click(function () {
let that = $(this);
let tid = that.data('tid');

cache_torrent_nfo.getItem(tid, function (err, value) {
function build_nfo_modal(res) {
if (res.success) {
(new $.zui.ModalTrigger({
name: 'torrent_nfo_content_model',
showHeader: false,
size: 'lg',
moveable: true,
custom: `<pre>${res.result}</pre>`
})).show();
} else {
create_error_notice(res.errors.join(', '));
}
}

if (value !== null) {
build_nfo_modal(value);
} else {
$.getJSON(api_point + '/torrent/nfofilecontent', {'tid': tid}, function (res) {
cache_torrent_nfo.setItem(tid, res);
build_nfo_modal(res);
});
}
});
});


// For torrents structure page
if ($('#torrent_structure').length) {
$('#torrent_structure div.dictionary,div.list').click(function () {
Expand Down
5 changes: 5 additions & 0 deletions apps/views/torrent/details.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
<div class="torrent-action-item"><!--suppress HtmlUnknownTarget -->
<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 -->
<?php if($torrent->hasNfo()): // TODO add global config key of NFO ?>
<div class="torrent-action-item">
<a class="torrent-nfo" href="javascript:" data-tid="<?= $torrent->getId() ?>"><i class="fas fa-info fa-fw"></i>&nbsp;View Torrent's Nfo file</a>
</div><!-- View Torrent's Nfo -->
<?php endif;?>
<div class="torrent-action-item"><!--suppress HtmlUnknownTarget -->
<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 -->
Expand Down
27 changes: 20 additions & 7 deletions apps/views/torrent/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@
<tr>
<td class="nowrap"><label for="file" class="required">Torrent File</label></td>
<td>
<input id="torrent_file" name="file" class="form-control" type="file"
accept=".torrent" required="required"
data-toggle="tooltip"
data-original-title="可直接上传从其它PT站下载的torrent文件"> <!-- FIXME original-title -->
<span id="torrent_file_name"></span>
<div class="row">
<div class="col-md-3">
<input id="torrent_file" name="file" class="form-control" type="file"
accept=".torrent" required="required"> <!-- FIXME original-title -->
</div>
<div class="col-md-9" id="torrent_file_name"></div>
</div>

</tr>
<tr>
<td class="nowrap"><label for="title" class="required">Title</td>
Expand Down Expand Up @@ -85,11 +88,21 @@
</div>
</td> <!-- FIXME link url -->
</tr>
<tr>
<td class="nowrap"><label for="nfo">NFO File</label></td>
<td>
<div class="row">
<div class="col-md-3">
<input id="torrent_nfo" name="nfo" class="form-control" type="file"
accept=".nfo, .txt"> <!-- FIXME original-title -->
</div>
</div>
</tr>
<tr>
<td class="nowrap"><label for="descr" class="required">Description</label></td>
<td>
<textarea id="descr" name="descr" class="form-control" style="width: 99%"
cols="100" rows="10" required="required"></textarea>
<textarea id="descr" name="descr" class="form-control" style="width: 99%"
cols="100" rows="10" required="required"></textarea>
</td>
</tr>
<tr>
Expand Down

0 comments on commit a1f1d64

Please sign in to comment.