From 383fa7f9b6cb9b861a12df1c266361798adaf29e Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Sun, 1 Sep 2024 00:25:38 +0700 Subject: [PATCH] Getting ffprobe data from TorrServer API (#1628) * Minor improvements * Update posting_tpl.tpl * Updated * Update TorrServerAPI.php * Update TorrServerAPI.php * Updated * Updated * Updated * Update playback_m3u.php * Update init_bb.php * Updated * Update playback_m3u.php * Update displaying_torrent.php * Updated * Update playback_m3u.tpl * Update TorrServerAPI.php * Update TorrServerAPI.php * Updated * Update playback_m3u.php * Update playback_m3u.php * Update playback_m3u.php * Updated * Updated * Update main.php * Update main.php * Update main.php * Update main.php * Update playback_m3u.php * Updated * Updated * Update playback_m3u.php * Update playback_m3u.tpl * Update playback_m3u.php * Updated * Update playback_m3u.php * Updated * Update playback_m3u.php * Update playback_m3u.tpl * Updated * Update playback_m3u.php * Update playback_m3u.tpl * Updated * Updated * Update playback_m3u.php * Update playback_m3u.php * Update playback_m3u.php * Update playback_m3u.php * Update playback_m3u.php * Update playback_m3u.php * Updated * Updated * Update TorrServerAPI.php * Revert "Update TorrServerAPI.php" This reverts commit 9726d0d019db2ecc2209a564f8da85d2bbb41876. * Update Ajax.php * Updated * Update playback_m3u.tpl * Update playback_m3u.tpl * Update playback_m3u.tpl * Update playback_m3u.php * Updated * Updated * Updated * Updated * Update playback_m3u.tpl * Updated * Updated * Revert "Updated" This reverts commit 5431f1fa517e01a40584bedd6dc743f27feef415. * Reapply "Updated" This reverts commit 61c49dba628a46911a85ef452f2c688a0bf85570. * Updated * Update playback_m3u.tpl * Update TorrServerAPI.php * Updated * Update playback_m3u.tpl * Updated * Update playback_m3u.php * Update playback_m3u.tpl * Update playback_m3u.tpl * Update playback_m3u.php * Updated * Updated * Update CHANGELOG.md * Update CHANGELOG.md * Update Ajax.php * Update ffprobe_info.php * Update ffprobe_info.php * Update ffprobe_info.php * Update TorrServerAPI.php * Update CHANGELOG.md --- CHANGELOG.md | 4 +- filelist.php | 14 +-- library/ajax/ffprobe_info.php | 106 ++++++++++++++++++++++ library/attach_mod/displaying_torrent.php | 2 +- library/includes/functions.php | 23 +++++ library/includes/init_bb.php | 2 +- library/language/source/main.php | 15 ++- playback_m3u.php | 33 +++++-- src/Ajax.php | 12 ++- src/TorrServerAPI.php | 52 ++++++++++- styles/templates/default/playback_m3u.tpl | 37 +++++++- styles/templates/posting_tpl.tpl | 2 +- 12 files changed, 279 insertions(+), 23 deletions(-) create mode 100644 library/ajax/ffprobe_info.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d3c24584..2fa495c0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - [CLI] TorrentPier installer ☕️ [\#1576](https://github.com/torrentpier/torrentpier/pull/1576), [\#1582](https://github.com/torrentpier/torrentpier/pull/1582), [\#1585](https://github.com/torrentpier/torrentpier/pull/1585), [\#1591](https://github.com/torrentpier/torrentpier/pull/1591) ([belomaxorka](https://github.com/belomaxorka)) - Added some new HTML meta-tags [\#1562](https://github.com/torrentpier/torrentpier/pull/1562) ([belomaxorka](https://github.com/belomaxorka)) - Added robots meta-tag support 🤖 [\#1587](https://github.com/torrentpier/torrentpier/pull/1587) ([belomaxorka](https://github.com/belomaxorka)) -- Added [TorrServer](https://github.com/YouROK/TorrServer) instance support! 🎞 [\#1603](https://github.com/torrentpier/torrentpier/pull/1603), [\#1623](https://github.com/torrentpier/torrentpier/pull/1623), [\#1624](https://github.com/torrentpier/torrentpier/pull/1624) ([belomaxorka](https://github.com/belomaxorka)) +- Added [TorrServer](https://github.com/YouROK/TorrServer) instance support! 🎞 [\#1603](https://github.com/torrentpier/torrentpier/pull/1603), [\#1623](https://github.com/torrentpier/torrentpier/pull/1623), [\#1624](https://github.com/torrentpier/torrentpier/pull/1624), [\#1628](https://github.com/torrentpier/torrentpier/pull/1628) ([belomaxorka](https://github.com/belomaxorka)) - Newtopic: Added configuring robots indexing [\#1599](https://github.com/torrentpier/torrentpier/pull/1599) ([belomaxorka](https://github.com/belomaxorka)) - Added showing releaser stats in profile [\#1568](https://github.com/torrentpier/torrentpier/pull/1568) ([belomaxorka](https://github.com/belomaxorka)) - Improved `filelist.php` [\#1586](https://github.com/torrentpier/torrentpier/pull/1586) ([belomaxorka](https://github.com/belomaxorka)) @@ -20,7 +20,7 @@ - Increased `USEREMAIL_MAX_LENGTH` [\#1566](https://github.com/torrentpier/torrentpier/pull/1566) ([belomaxorka](https://github.com/belomaxorka)) - Minor improvements [\#1570](https://github.com/torrentpier/torrentpier/pull/1570), [\#1571](https://github.com/torrentpier/torrentpier/pull/1571), [\#1575](https://github.com/torrentpier/torrentpier/pull/1575), [\#1589](https://github.com/torrentpier/torrentpier/pull/1589), [\#1592](https://github.com/torrentpier/torrentpier/pull/1592), [\#1605](https://github.com/torrentpier/torrentpier/pull/1605), [\#1611](https://github.com/torrentpier/torrentpier/pull/1611), [\#1612](https://github.com/torrentpier/torrentpier/pull/1612), [\#1615](https://github.com/torrentpier/torrentpier/pull/1615), [\#1627](https://github.com/torrentpier/torrentpier/pull/1627) ([belomaxorka](https://github.com/belomaxorka)) - Updated deps [\#1563](https://github.com/torrentpier/torrentpier/pull/1563), [\#1564](https://github.com/torrentpier/torrentpier/pull/1564), [\#1608](https://github.com/torrentpier/torrentpier/pull/1608), [\#1609](https://github.com/torrentpier/torrentpier/pull/1609), [\#1610](https://github.com/torrentpier/torrentpier/pull/1610) ([belomaxorka](https://github.com/belomaxorka)) -- New Crowdin updates [\#1569](https://github.com/torrentpier/torrentpier/pull/1569), [\#1572](https://github.com/torrentpier/torrentpier/pull/1572), [\#1573](https://github.com/torrentpier/torrentpier/pull/1573), [\#1574](https://github.com/torrentpier/torrentpier/pull/1574), [\#1588](https://github.com/torrentpier/torrentpier/pull/1588), [\#1590](https://github.com/torrentpier/torrentpier/pull/1590), [\#1600](https://github.com/torrentpier/torrentpier/pull/1600), [\#1601](https://github.com/torrentpier/torrentpier/pull/1601), [\#1606](https://github.com/torrentpier/torrentpier/pull/1606), [\#1607](https://github.com/torrentpier/torrentpier/pull/1607), [\#1625](https://github.com/torrentpier/torrentpier/pull/1625), [\#1626](https://github.com/torrentpier/torrentpier/pull/1626), [\#1629](https://github.com/torrentpier/torrentpier/pull/1629) ([Exileum](https://github.com/Exileum)) +- New Crowdin updates [\#1569](https://github.com/torrentpier/torrentpier/pull/1569), [\#1572](https://github.com/torrentpier/torrentpier/pull/1572), [\#1573](https://github.com/torrentpier/torrentpier/pull/1573), [\#1574](https://github.com/torrentpier/torrentpier/pull/1574), [\#1588](https://github.com/torrentpier/torrentpier/pull/1588), [\#1590](https://github.com/torrentpier/torrentpier/pull/1590), [\#1600](https://github.com/torrentpier/torrentpier/pull/1600), [\#1601](https://github.com/torrentpier/torrentpier/pull/1601), [\#1606](https://github.com/torrentpier/torrentpier/pull/1606), [\#1607](https://github.com/torrentpier/torrentpier/pull/1607), [\#1625](https://github.com/torrentpier/torrentpier/pull/1625), [\#1626](https://github.com/torrentpier/torrentpier/pull/1626), [\#1629](https://github.com/torrentpier/torrentpier/pull/1629), [\#1630](https://github.com/torrentpier/torrentpier/pull/1630) ([Exileum](https://github.com/Exileum)) ## [v2.4.4](https://github.com/torrentpier/torrentpier/tree/v2.4.4) (2024-07-22) [Full Changelog](https://github.com/torrentpier/torrentpier/compare/v2.4.3...v2.4.4) diff --git a/filelist.php b/filelist.php index 3b4412928..8c87d76d2 100644 --- a/filelist.php +++ b/filelist.php @@ -15,12 +15,12 @@ require __DIR__ . '/common.php'; $user->session_start(); if ($bb_cfg['bt_disable_dht'] && IS_GUEST) { - bb_simple_die($lang['BT_PRIVATE_TRACKER'], 403); + bb_die($lang['BT_PRIVATE_TRACKER'], 403); } $topic_id = isset($_GET[POST_TOPIC_URL]) ? (int)$_GET[POST_TOPIC_URL] : 0; if (!$topic_id) { - bb_simple_die($lang['INVALID_TOPIC_ID'], 404); + bb_die($lang['INVALID_TOPIC_ID'], 404); } $sql = 'SELECT t.attach_id, t.info_hash, t.info_hash_v2, t.size, ad.physical_filename @@ -31,7 +31,7 @@ $sql = 'SELECT t.attach_id, t.info_hash, t.info_hash_v2, t.size, ad.physical_fil LIMIT 1'; if (!$row = DB()->fetch_row($sql)) { - bb_simple_die($lang['INVALID_TOPIC_ID_DB'], 404); + bb_die($lang['INVALID_TOPIC_ID_DB'], 404); } // Protocol meta @@ -45,7 +45,7 @@ $t_hash_field = $meta_v2 ? 'piecesRoot' : 'sha1'; $file_path = get_attachments_dir() . '/' . $row['physical_filename']; if (!is_file($file_path)) { - bb_simple_die($lang['TOR_NOT_FOUND'], 410); + bb_die($lang['TOR_NOT_FOUND'], 410); } $file_contents = file_get_contents($file_path); @@ -60,18 +60,18 @@ if ($bb_cfg['flist_max_files']) { } if ($file_count > $bb_cfg['flist_max_files']) { - bb_simple_die(sprintf($lang['BT_FLIST_LIMIT'], $bb_cfg['flist_max_files'], $file_count), 410); + bb_die(sprintf($lang['BT_FLIST_LIMIT'], $bb_cfg['flist_max_files'], $file_count), 410); } } try { $torrent = \Arokettu\Torrent\TorrentFile::loadFromString($file_contents); } catch (\Exception $e) { - bb_simple_die(htmlCHR("{$lang['TORFILE_INVALID']}: {$e->getMessage()}"), 410); + bb_die(htmlCHR("{$lang['TORFILE_INVALID']}: {$e->getMessage()}"), 410); } if (IS_GUEST && $torrent->isPrivate()) { - bb_simple_die($lang['BT_PRIVATE_TORRENT'], 403); + bb_die($lang['BT_PRIVATE_TORRENT'], 403); } $files = $torrent->$t_version_field()->$t_files_field(); diff --git a/library/ajax/ffprobe_info.php b/library/ajax/ffprobe_info.php new file mode 100644 index 000000000..755d558ab --- /dev/null +++ b/library/ajax/ffprobe_info.php @@ -0,0 +1,106 @@ +ajax_die($lang['MODULE_OFF']); +} + +if (!$attach_id = (int)$this->request['attach_id'] or !is_numeric($attach_id)) { + $this->ajax_die($lang['INVALID_ATTACH_ID']); +} + +if (!$file_index = (int)$this->request['file_index'] or !is_numeric($file_index)) { + $this->ajax_die("Invalid file index: $file_index"); +} + +if (!$info_hash = (string)$this->request['info_hash'] or !ctype_xdigit($info_hash)) { + $this->ajax_die("Invalid info_hash: $info_hash"); +} + +$isAudio = !empty($this->request['is_audio']); + +// Get ffprobe info from TorrServer +$ffpInfo = (new \TorrentPier\TorrServerAPI())->getFfpInfo($info_hash, $file_index, $attach_id); +$ffpInfo = $ffpInfo->{$file_index}; +if (isset($ffpInfo->streams)) { + // Video codec information + $videoCodecIndex = array_search('video', array_column($ffpInfo->streams, 'codec_type')); + if (is_int($videoCodecIndex)) { + $videoCodecInfo = $ffpInfo->streams[$videoCodecIndex]; + } + // Audio codec information + $audioTracks = array_filter($ffpInfo->streams, function ($e) { + return $e->codec_type === 'audio'; + }); + // Audio tracks information + $audioDub = array_map(function ($stream) { + global $lang; + + $result = '' . sprintf($lang['AUDIO_TRACK'], (!isset($stream->index) || $stream->index === 0) ? 1 : $stream->index) . '
'; + if (isset($stream->tags->language)) { + if (isset($stream->tags->title)) { + $result .= '' . mb_strtoupper($stream->tags->language, 'UTF-8') . ' (' . $stream->tags->title . ')' . ''; + } else { + $result .= '' . mb_strtoupper($stream->tags->language, 'UTF-8') . ''; + } + $result .= '
'; + } + + if (!empty($stream->codec_name)) { + $result .= sprintf($lang['AUDIO_CODEC'], $stream->codec_long_name, mb_strtoupper($stream->codec_name, 'UTF-8')) . '
'; + } + if (!empty($stream->bit_rate)) { + $result .= sprintf($lang['BITRATE'], humn_bitrate($stream->bit_rate)) . '
'; + } + if (!empty($stream->sample_rate)) { + $result .= sprintf($lang['SAMPLE_RATE'], $stream->sample_rate) . '
'; + } + if (!empty($stream->channels)) { + $result .= sprintf($lang['CHANNELS'], $stream->channels) . '
'; + } + if (!empty($stream->channel_layout)) { + $result .= sprintf($lang['CHANNELS_LAYOUT'], $stream->channel_layout); + } + + return $result; + }, $audioTracks); + + // Generate output data + $data = [ + 'filesize' => sprintf($lang['FILESIZE'] . ': %s', humn_size($ffpInfo->format->size)), + 'resolution' => (!$isAudio && isset($videoCodecInfo)) ? sprintf($lang['RESOLUTION'], $videoCodecInfo->width . 'x' . $videoCodecInfo->height) : '', + 'video_codec' => (!$isAudio && isset($videoCodecInfo->codec_name)) ? sprintf($lang['VIDEO_CODEC'], $videoCodecInfo->codec_long_name, mb_strtoupper($videoCodecInfo->codec_name, 'UTF-8')) : '', + 'audio_dub' => implode('
', $audioDub) + ]; + + // Validate output data + $result = '
'; + if (!empty($data['resolution'])) { + $result .= $data['resolution'] . '
'; + } + if (!empty($data['filesize'])) { + $result .= $data['filesize'] . '
'; + } + if (!empty($data['video_codec'])) { + $result .= $data['video_codec']; + } + if (!empty($data['audio_dub'])) { + $result .= '
' . $data['audio_dub']; + } + + $this->response['ffprobe_data'] = $result; +} + +$this->response['file_index'] = $file_index; diff --git a/library/attach_mod/displaying_torrent.php b/library/attach_mod/displaying_torrent.php index adb1bdcf6..2257a0954 100644 --- a/library/attach_mod/displaying_torrent.php +++ b/library/attach_mod/displaying_torrent.php @@ -226,7 +226,7 @@ if ($tor_reged && $tor_info) { // TorrServer integration if ($bb_cfg['torr_server']['enabled'] && (!IS_GUEST || !$bb_cfg['torr_server']['disable_for_guest']) && (new \TorrentPier\TorrServerAPI())->getM3UPath($attach_id)) { $template->assign_block_vars('postrow.attach.tor_reged.tor_server', [ - 'TORR_SERVER_M3U_LINK' => PLAYBACK_M3U_URL . $attach_id, + 'TORR_SERVER_M3U_LINK' => PLAYBACK_M3U_URL . $bt_topic_id, 'TORR_SERVER_M3U_ICON' => $images['icon_tor_m3u_icon'], ]); } diff --git a/library/includes/functions.php b/library/includes/functions.php index aeb9e04d2..b04ce7a92 100644 --- a/library/includes/functions.php +++ b/library/includes/functions.php @@ -595,6 +595,29 @@ function humn_size($size, $rounder = null, $min = null, $space = ' ') return round($size, $rounder) . $space . $ext; } +/** + * Bitrate to human-readable format + * + * @param int $bitrate + * @param string $space + * @return string + */ +function humn_bitrate(int $bitrate, string $space = ' '): string +{ + if ($bitrate >= 1000000) { + $unit = 'Mbps'; + $bitrate /= 1000000; + } elseif ($bitrate >= 1000) { + $unit = 'kbps'; + $bitrate /= 1000; + } else { + $unit = 'bps'; + } + + $formattedBitrate = number_format($bitrate, 2); + return $formattedBitrate . $space . $unit; +} + function bt_show_ip($ip, $port = '') { global $bb_cfg; diff --git a/library/includes/init_bb.php b/library/includes/init_bb.php index 09b42488d..6ee61734d 100644 --- a/library/includes/init_bb.php +++ b/library/includes/init_bb.php @@ -302,7 +302,7 @@ define('PROFILE_URL', 'profile.php?mode=viewprofile&' . POST_USERS_URL . '=' define('BONUS_URL', 'profile.php?mode=bonus'); define('TOPIC_URL', 'viewtopic.php?' . POST_TOPIC_URL . '='); define('FILELIST_URL', 'filelist.php?' . POST_TOPIC_URL . '='); -define('PLAYBACK_M3U_URL', 'playback_m3u.php?attach_id='); +define('PLAYBACK_M3U_URL', 'playback_m3u.php?' . POST_TOPIC_URL . '='); define('USER_AGENT', strtolower($_SERVER['HTTP_USER_AGENT'])); diff --git a/library/language/source/main.php b/library/language/source/main.php index 53cce8538..e2f2ab599 100644 --- a/library/language/source/main.php +++ b/library/language/source/main.php @@ -64,6 +64,8 @@ $lang['CLEAR'] = 'Clear'; $lang['MOVE_TO_TOP'] = 'Move to top'; $lang['UNKNOWN'] = 'Unknown'; $lang['COPY_TO_CLIPBOARD'] = 'Copy to clipboard'; +$lang['NO_ITEMS'] = 'There seems to be no data here...'; +$lang['PLEASE_TRY_AGAIN'] = 'Please try again after few seconds...'; $lang['NEXT_PAGE'] = 'Next'; $lang['PREVIOUS_PAGE'] = 'Previous'; @@ -1268,9 +1270,20 @@ $lang['DOWNLOAD'] = 'Download'; // this Language Variable is defined in admin.ph $lang['FILESIZE'] = 'Filesize'; $lang['VIEWED'] = 'Viewed'; $lang['EXTENSION_DISABLED_AFTER_POSTING'] = 'The Extension \'%s\' was deactivated by an board admin, therefore this Attachment is not displayed.'; // used in Posts and PM's, replace %s with mime type + +// Viewtopic -> Display of Attachments -> TorrServer integration +$lang['STREAM'] = 'Stream'; +$lang['RESOLUTION'] = 'Resolution: %s'; +$lang['CHANNELS'] = 'Channels: %s'; +$lang['CHANNELS_LAYOUT'] = 'Channels layout: %s'; +$lang['BITRATE'] = 'Bitrate: %s'; +$lang['SAMPLE_RATE'] = 'Sample rate: %s'; +$lang['AUDIO_TRACK'] = 'Audio track information (%d):'; +$lang['AUDIO_CODEC'] = 'Audio codec: %s'; +$lang['VIDEO_CODEC'] = 'Video codec: %s'; +$lang['SHOW_MORE_INFORMATION_FILE'] = 'Show more information about file'; $lang['DOWNLOAD_M3U_FILE'] = 'Download .m3u file'; $lang['PLAYBACK_M3U'] = 'Playback .m3u file'; -$lang['STREAM'] = 'Stream'; $lang['COPY_STREAM_LINK'] = 'Copy stream link to clipboard'; $lang['M3U_NOT_SUPPORTED'] = 'This file cannot be played in the browser...'; $lang['M3U_NOTICE'] = 'Some browsers do not support playback of certain video formats. In such a case, you can download the .m3u file and play it using a third-party player'; diff --git a/playback_m3u.php b/playback_m3u.php index ce52e7cf3..7cea73c4c 100644 --- a/playback_m3u.php +++ b/playback_m3u.php @@ -18,7 +18,7 @@ if (!$bb_cfg['torr_server']['enabled']) { // Valid file formats $validFormats = [ 'audio' => ['mp3', 'flac', 'wav'], - 'video' => ['mp4', 'mkv', 'avi'] + 'video' => ['mp4', 'mkv', 'avi', 'm4v'] ]; // Start session management @@ -27,13 +27,24 @@ $user->session_start(['req_login' => $bb_cfg['torr_server']['disable_for_guest'] // Disable robots indexing $page_cfg['allow_robots'] = false; -// Check attach_id -if (!$attach_id = request_var('attach_id', 0)) { - bb_die($lang['INVALID_ATTACH_ID']); +// Check topic_id +$topic_id = isset($_GET[POST_TOPIC_URL]) ? (int)$_GET[POST_TOPIC_URL] : 0; +if (!$topic_id) { + bb_die($lang['INVALID_TOPIC_ID'], 404); +} + +// Getting torrent info from database +$sql = 'SELECT attach_id, info_hash, info_hash_v2 + FROM ' . BB_BT_TORRENTS . ' + WHERE topic_id = ' . $topic_id . ' + LIMIT 1'; + +if (!$row = DB()->fetch_row($sql)) { + bb_die($lang['INVALID_TOPIC_ID_DB'], 404); } // Check m3u file exist -if (!$m3uFile = (new \TorrentPier\TorrServerAPI())->getM3UPath($attach_id)) { +if (!$m3uFile = (new \TorrentPier\TorrServerAPI())->getM3UPath($row['attach_id'])) { bb_die($lang['ERROR_NO_ATTACHMENT']); } @@ -49,6 +60,7 @@ foreach ($m3uData as $entry) { if (!filter_var($streamLink, FILTER_VALIDATE_URL)) { continue; } + parse_str(parse_url($streamLink, PHP_URL_QUERY), $urlParams); // Parse tags foreach ($entry->getExtTags() as $extTag) { @@ -65,11 +77,16 @@ foreach ($m3uData as $entry) { // Validate file extension $getExtension = pathinfo($title, PATHINFO_EXTENSION); + if ($getExtension === 'm3u') { + // Skip m3u files + continue; + } $filesCount++; $rowClass = ($filesCount % 2) ? 'row1' : 'row2'; $template->assign_block_vars('m3ulist', [ 'ROW_NUMBER' => $filesCount, + 'FILE_INDEX' => $urlParams['index'], 'ROW_CLASS' => $rowClass, 'IS_VALID' => in_array($getExtension, array_merge($validFormats['audio'], $validFormats['video'])), 'IS_AUDIO' => in_array($getExtension, $validFormats['audio']), @@ -81,8 +98,12 @@ foreach ($m3uData as $entry) { // Generate output $template->assign_vars([ + 'HAS_ITEMS' => $filesCount > 0, 'PAGE_TITLE' => $lang['PLAYBACK_M3U'], - 'FILES_COUNT' => sprintf($lang['BT_FLIST_FILE_PATH'], declension($filesCount, 'files')), + 'ATTACH_ID' => $row['attach_id'], + 'INFO_HASH' => bin2hex($row['info_hash'] ?? $row['info_hash_v2']), + 'FILES_COUNT_TITLE' => sprintf($lang['BT_FLIST_FILE_PATH'], declension($filesCount, 'files')), + 'U_TOPIC' => TOPIC_URL . $topic_id, ]); print_page('playback_m3u.tpl'); diff --git a/src/Ajax.php b/src/Ajax.php index 22584b8c0..771449b0e 100644 --- a/src/Ajax.php +++ b/src/Ajax.php @@ -41,6 +41,7 @@ class Ajax 'manage_group' => ['user'], 'callseed' => ['user'], + 'ffprobe_info' => ['guest'], 'thx' => ['guest'], 'view_post' => ['guest'], 'view_torrent' => ['guest'], @@ -526,9 +527,18 @@ class Ajax * * @return void */ - public function thx() { require AJAX_DIR . '/thanks.php'; } + + /** + * Get info from ffprobe (TorrServer API) + * + * @return void + */ + public function ffprobe_info() + { + require AJAX_DIR . '/ffprobe_info.php'; + } } diff --git a/src/TorrServerAPI.php b/src/TorrServerAPI.php index 85dd242df..bc5ccf008 100644 --- a/src/TorrServerAPI.php +++ b/src/TorrServerAPI.php @@ -12,6 +12,8 @@ namespace TorrentPier; use Curl\Curl; use CURLFile; +use stdClass; + /** * Class TorrServerAPI * @package TorrentPier @@ -33,7 +35,8 @@ class TorrServerAPI private array $endpoints = [ 'playlist' => 'playlist', 'upload' => 'torrent/upload', - 'stream' => 'stream' + 'stream' => 'stream', + 'ffprobe' => 'ffp' ]; /** @@ -175,6 +178,10 @@ class TorrServerAPI */ public function removeM3U(string|int $attach_id): bool { + // Remove ffprobe data from cache + CACHE('tr_cache')->rm("ffprobe_m3u_$attach_id"); + + // Unlink .m3u file $m3uFile = get_attachments_dir() . '/' . self::M3U['prefix'] . $attach_id . self::M3U['extension']; if (is_file($m3uFile)) { if (unlink($m3uFile)) { @@ -187,6 +194,49 @@ class TorrServerAPI return false; } + /** + * Returns info from TorrServer in-build ffprobe + * + * @param string $hash + * @param int $index + * @param int|string $attach_id + * @return mixed + */ + public function getFfpInfo(string $hash, int $index, int|string $attach_id): mixed + { + global $bb_cfg; + + if (!$response = CACHE('tr_cache')->get("ffprobe_m3u_$attach_id")) { + $response = new stdClass(); + } + + if (!isset($response->{$index})) { + // Make stream call to store torrent in memory + for ($i = 0, $max_try = 3; $i <= $max_try; $i++) { + if ($this->getStream($hash)) { + break; + } elseif ($i == $max_try) { + return false; + } + } + + $curl = new Curl(); + $curl->setTimeout($bb_cfg['torr_server']['timeout']); + + $curl->setHeader('Accept', 'application/json'); + $curl->get($this->url . $this->endpoints['ffprobe'] . '/' . $hash . '/' . $index); + $response->{$index} = $curl->response; + if ($curl->httpStatusCode === 200 && !empty($response->{$index})) { + CACHE('tr_cache')->set("ffprobe_m3u_$attach_id", $response, 3600); + } else { + bb_log("TorrServer (ERROR) [$this->url]: Response code: {$curl->httpStatusCode}\n", $this->logFile); + } + $curl->close(); + } + + return $response; + } + /** * Up stream * diff --git a/styles/templates/default/playback_m3u.tpl b/styles/templates/default/playback_m3u.tpl index f6dc29b00..82bd5effd 100644 --- a/styles/templates/default/playback_m3u.tpl +++ b/styles/templates/default/playback_m3u.tpl @@ -1,18 +1,43 @@

{PAGE_TITLE}

+ + - + + - + + + + + + + + @@ -36,6 +68,7 @@
+ {L_TOPIC_RETURN}

{CURRENT_TIME}

{S_TIMEZONE}

diff --git a/styles/templates/posting_tpl.tpl b/styles/templates/posting_tpl.tpl index 987fbfe44..324e5d51c 100644 --- a/styles/templates/posting_tpl.tpl +++ b/styles/templates/posting_tpl.tpl @@ -4722,7 +4722,7 @@ $(function(){       - Нет щаблонов для релизов + Нет шаблонов для релизов

#{FILES_COUNT}{FILES_COUNT_TITLE} {L_STREAM}
{m3ulist.ROW_NUMBER}{m3ulist.TITLE}{m3ulist.TITLE} + + {L_COPY_STREAM_LINK} · {L_DOWNLOAD_M3U_FILE} @@ -26,6 +51,13 @@
{L_NO_ITEMS}
{L_M3U_NOTICE}