From 752b23dc874999d77c9e78adc68c8c03f20e6a2a Mon Sep 17 00:00:00 2001 From: filesite Date: Fri, 8 Jul 2022 23:42:37 +0800 Subject: [PATCH] improve list controller and view for videos --- lib/DirScanner.php | 18 +++++- .../videoblog/controller/ListController.php | 17 +++--- .../videoblog/controller/ViewController.php | 60 +++++++++++++++++++ themes/videoblog/views/site/index.php | 38 +++++++++--- 4 files changed, 114 insertions(+), 19 deletions(-) create mode 100644 themes/videoblog/controller/ViewController.php diff --git a/lib/DirScanner.php b/lib/DirScanner.php index 30102c2..389319d 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -249,13 +249,25 @@ Class DirScanner { } //合并描述文件内容到md文件或者目录数据 + //增加视频文件:mp4, m3u8描述文件支持 private function mergeDescriptionData($realpath) { $data = []; $ext = $this->parseDescriptionFiles($realpath); //try to find the md file - $targetFile = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.md', $realpath); - if (file_exists($targetFile)) { + $targetFile = ''; + $targetFile_md = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.md', $realpath); + $targetFile_mp4 = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.mp4', $realpath); + $targetFile_m3u8 = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.m3u8', $realpath); + if (file_exists($targetFile_md)) { + $targetFile = $targetFile_md; + }else if (file_exists($targetFile_mp4)) { + $targetFile = $targetFile_mp4; + }else if (file_exists($targetFile_m3u8)) { + $targetFile = $targetFile_m3u8; + } + + if (!empty($targetFile)) { $fileId = $this->getId($targetFile); if (empty($this->scanResults[$fileId])) { $ext['id'] = $fileId; @@ -508,7 +520,7 @@ Class DirScanner { if (is_dir($realpath)) { $files = []; if ($nextLevels >= 0) { - $files = $this->scan($realpath, $nextLevels); + $files = $this->scan($realpath, $levels); if (!empty($files)) { foreach($files as $file) { $this->scanResults[$file['id']] = $file; diff --git a/themes/videoblog/controller/ListController.php b/themes/videoblog/controller/ListController.php index 8819e16..f890f71 100644 --- a/themes/videoblog/controller/ListController.php +++ b/themes/videoblog/controller/ListController.php @@ -11,12 +11,13 @@ Class ListController extends Controller { //获取数据 $scanner = new DirScanner(); $scanner->setWebRoot(FSC::$app['config']['content_directory']); - $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4); + $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3); $scanResults = $scanner->getScanResults(); //获取目录 $menus = $scanner->getMenus(); $cateId = $this->get('id', $menus[0]['id']); + //print_r($menus);exit; $titles = []; $htmlReadme = ''; @@ -32,7 +33,7 @@ Class ListController extends Controller { //获取目录面包屑 $subcate = $scanResults[$cateId]; - $breadcrumbs = $this->getBreadcrumbs($menus, $subcate); + $breadcrumbs = $this->getBreadcrumbs($scanResults, $subcate); //获取当前目录下的readme $htmlCateReadme = ''; @@ -66,13 +67,15 @@ Class ListController extends Controller { 'url' => $subcate['path'], ]); - $foundKey = array_search($subcate['pid'], array_column($menus, 'id')); - if ($foundKey !== false) { + $parent = !empty($menus[$subcate['pid']]) ? $menus[$subcate['pid']] : null; + while($parent) { array_unshift($breads, [ - 'id' => $menus[$foundKey]['id'], - 'name' => $menus[$foundKey]['directory'], - 'url' => $menus[$foundKey]['path'], + 'id' => $parent['id'], + 'name' => $parent['directory'], + 'url' => $parent['path'], ]); + + $parent = !empty($menus[$parent['pid']]) ? $menus[$parent['pid']] : null; } return $breads; diff --git a/themes/videoblog/controller/ViewController.php b/themes/videoblog/controller/ViewController.php new file mode 100644 index 0000000..f9198e2 --- /dev/null +++ b/themes/videoblog/controller/ViewController.php @@ -0,0 +1,60 @@ +setWebRoot(FSC::$app['config']['content_directory']); + $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4); + $scanResults = $scanner->getScanResults(); + + //获取目录 + $menus = $scanner->getMenus(); + $cateId = $this->get('id', $menus[0]['id']); + + $titles = []; + $htmlReadme = ''; + $readmeFile = $scanner->getDefaultReadme(); + if (!empty($readmeFile)) { + $titles = $scanner->getMDTitles($readmeFile['id']); + + $Parsedown = new Parsedown(); + $content = file_get_contents($readmeFile['realpath']); + $htmlReadme = $Parsedown->text($content); + $htmlReadme = $scanner->fixMDUrls($readmeFile['realpath'], $htmlReadme); + } + + //获取目录面包屑 + $subcate = $scanResults[$cateId]; + $breadcrumbs = $this->getBreadcrumbs($menus, $subcate); + + //获取当前目录下的readme + $htmlCateReadme = ''; + $cateReadmeFile = $scanner->getDefaultReadme($cateId); + if (!empty($cateReadmeFile)) { + $Parsedown = new Parsedown(); + $content = file_get_contents($cateReadmeFile['realpath']); + $htmlCateReadme = $Parsedown->text($content); + $htmlCateReadme = $scanner->fixMDUrls($cateReadmeFile['realpath'], $htmlCateReadme); + } + + $pageTitle = !empty($titles) ? $titles[0]['name'] : "FileSite.io - 无数据库、基于文件和目录的Markdown文档、网址导航、图书、图片、视频网站PHP开源系统"; + if (!empty($subcate)) { + $pageTitle = "{$subcate['directory']}的照片,来自{$pageTitle}"; + if (!empty($subcate['title'])) { + $pageTitle = $subcate['title']; + } + } + $viewName = '//site/index'; //共享视图 + $params = compact('cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme'); + return $this->render($viewName, $params, $pageTitle); + } + +} diff --git a/themes/videoblog/views/site/index.php b/themes/videoblog/views/site/index.php index 0e2b97d..20497ac 100644 --- a/themes/videoblog/views/site/index.php +++ b/themes/videoblog/views/site/index.php @@ -43,6 +43,7 @@ eof;
+ + Play Video + +eof; + } + + foreach($category['directories'] as $dir) { echo << - + eof; if (!empty($dir['snapshot'])) { @@ -96,10 +111,7 @@ eof; $title = !empty($dir['title']) ? $dir['title'] : $dir['directory']; echo << {$title} @@ -107,17 +119,25 @@ eof; } } - if (!empty($category['files'])) { //一级目录支持 + if (!empty($category['files'])) { //一级目录支持,目录下直接存放视频文件 + $first_img = ''; foreach($category['files'] as $file) { - if (!in_array($file['extension'], $imgExts)) {continue;} + if (!in_array($file['extension'], $videoExts)) { + //如果是最后一层视频目录,取第一张图片做封面 + if (empty($first_img) && empty($category['snapshot']) && in_array($file['extension'], $imgExts)) { + $first_img = $file; + } + continue; + } $duration = !empty($category['duration']) ? $category['duration'] : ''; + $snapshot = !empty($file['snapshot']) ? $file['snapshot'] : (!empty($category['snapshot']) ? $category['snapshot'] : $first_img['path']); - $title = !empty($file['title']) ? $file['title'] : $file['filename']; + $title = !empty($category['title']) ? $category['title'] : $file['filename']; echo << - {$file['filename']} + snapshot of {$title} {$duration}