From 5ccfe0143133774ff3838fbd5c859fd0d221a749 Mon Sep 17 00:00:00 2001 From: filesite Date: Sat, 28 Sep 2024 10:34:12 +0800 Subject: [PATCH] add snapshot for audio --- lib/DirScanner.php | 5 +++ plugins/Html.php | 16 ++++++++ themes/beauty/controller/ListController.php | 15 ++++++- themes/beauty/controller/SiteController.php | 43 +++++++++++++++++---- themes/beauty/views/site/index.php | 12 +++++- themes/beauty/views/site/mp3player.php | 4 +- www/js/beauty.js | 7 +++- 7 files changed, 90 insertions(+), 12 deletions(-) diff --git a/lib/DirScanner.php b/lib/DirScanner.php index 8f19827..157e0d7 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -285,6 +285,7 @@ Class DirScanner { //合并描述文件内容到md文件或者目录数据 //增加视频文件:mp4, mov, m3u8描述文件支持 //增加.url文件支持 + //增加.mp3文件支持 private function mergeDescriptionData($realpath) { $data = array(); $ext = $this->parseDescriptionFiles($realpath); @@ -296,6 +297,8 @@ Class DirScanner { $targetFile_mov = preg_replace('/_?[a-z0-9]+\.txt$/U', '.mov', $realpath); $targetFile_m3u8 = preg_replace('/_?[a-z0-9]+\.txt$/U', '.m3u8', $realpath); $targetFile_url = preg_replace('/_?[a-z0-9]+\.txt$/U', '.url', $realpath); + $targetFile_mp3 = preg_replace('/_?[a-z0-9]+\.txt$/U', '.mp3', $realpath); + if (file_exists($targetFile_md)) { $targetFile = $targetFile_md; }else if (file_exists($targetFile_mp4)) { @@ -306,6 +309,8 @@ Class DirScanner { $targetFile = $targetFile_m3u8; }else if (file_exists($targetFile_url)) { $targetFile = $targetFile_url; + }else if (file_exists($targetFile_mp3)) { + $targetFile = $targetFile_mp3; } if (!empty($targetFile) && $targetFile != $realpath) { diff --git a/plugins/Html.php b/plugins/Html.php index 9aa22bf..b6f3a7c 100644 --- a/plugins/Html.php +++ b/plugins/Html.php @@ -291,4 +291,20 @@ eof; return $sourceType; } + //根据文件名,找出同名的图片文件 + public static function searchImageByFilename($filename, $files, $imgExts = array('jpg', 'jpeg', 'png', 'webp', 'gif', 'svg')) { + $matchedImage = null; + + if (!empty($files)) { + foreach($files as $item) { + if ($item['filename'] == $filename && in_array($item['extension'], $imgExts)) { + $matchedImage = $item; + break; + } + } + } + + return $matchedImage; + } + } diff --git a/themes/beauty/controller/ListController.php b/themes/beauty/controller/ListController.php index 98f5118..734e235 100644 --- a/themes/beauty/controller/ListController.php +++ b/themes/beauty/controller/ListController.php @@ -122,6 +122,7 @@ Class ListController extends Controller { } //图片、视频类型筛选支持 + $allFiles = $scanResults[$cateId]['files']; $showType = $this->get('show', 'all'); if ($showType == 'image' && !empty($scanResults[$cateId]['files'])) { $scanResults[$cateId]['files'] = array_filter($scanResults[$cateId]['files'], function($item) { @@ -247,6 +248,18 @@ Class ListController extends Controller { } if (!empty($item['extension']) && in_array($item['extension'], $audioExts)) { + //print_r($subcate['files']);exit; + //为音乐文件获取封面图 + if (empty($item['snapshot'])) { + $imgExts = !empty(FSC::$app['config']['supportedImageExts']) ? FSC::$app['config']['supportedImageExts'] : array('jpg', 'jpeg', 'png', 'webp', 'gif'); + $matchedImage = Html::searchImageByFilename($item['filename'], $allFiles, $imgExts); + if (!empty($matchedImage)) { + $item['snapshot'] = $matchedImage['path']; + }else { + $item['snapshot'] = '/img/beauty/audio_icon.jpeg?v1'; + } + } + array_push($audios, $item); $index ++; } @@ -263,7 +276,7 @@ Class ListController extends Controller { $viewName = '//site/index'; //共享视图 $params = compact( 'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme', - 'mp3File', 'page', 'pageSize', 'cacheDataId', 'copyright', 'showType', 'isAdminIp' + 'mp3File', 'page', 'pageSize', 'cacheDataId', 'copyright', 'showType', 'isAdminIp', 'allFiles' ); return $this->render($viewName, $params, $pageTitle); } diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index 28fadb0..3fbe5a2 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -155,6 +155,7 @@ Class SiteController extends Controller { //mp3支持 $audioExts = !empty(FSC::$app['config']['supportedAudioExts']) ? FSC::$app['config']['supportedAudioExts'] : array('mp3'); + $allFiles = $scanResults; $showType = $this->get('show', 'all'); if ($showType == 'image') { $scanResults = array_filter($scanResults, function($item) { @@ -174,7 +175,7 @@ Class SiteController extends Controller { } - //dataType支持:[image, video] + //dataType支持:[image, video, audio] $dataType = $this->get('dataType', 'html'); if ($dataType == 'image') { $imgs = array(); @@ -235,6 +236,17 @@ Class SiteController extends Controller { } if (!empty($item['extension']) && in_array($item['extension'], $audioExts)) { + //为音乐文件获取封面图 + if (empty($item['snapshot'])) { + $imgExts = !empty(FSC::$app['config']['supportedImageExts']) ? FSC::$app['config']['supportedImageExts'] : array('jpg', 'jpeg', 'png', 'webp', 'gif'); + $matchedImage = Html::searchImageByFilename($item['filename'], $allFiles, $imgExts); + if (!empty($matchedImage)) { + $item['snapshot'] = $matchedImage['path']; + }else { + $item['snapshot'] = '/img/beauty/audio_icon.jpeg?v1'; + } + } + array_push($audios, $item); $index ++; } @@ -249,7 +261,7 @@ Class SiteController extends Controller { $params = compact( 'page', 'pageSize', 'cacheDataId', 'showType', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'mp3File', 'copyright', - 'alertWarning', 'isAdminIp' + 'alertWarning', 'isAdminIp', 'allFiles' ); return $this->render($viewName, $params, $pageTitle); } @@ -286,7 +298,7 @@ Class SiteController extends Controller { * original - 原图 * small - 缩略图 * vm - 视频封面图(vmeta) - * am - 音乐封面图(ameta) + * am - 音乐封面图 **/ public function actionDirsnap() { $code = 1; @@ -307,8 +319,8 @@ Class SiteController extends Controller { $cacheSubDir = 'dir'; $withCreateTime = true; //返回数据的缓存时间 $cache = Common::getCacheFromFile($cacheKey, $expireSeconds, $cacheSubDir, $withCreateTime); - $cachedData = $cache['data']; - $cachedCtime = $cache['ctime']; + $cachedData = !empty($cache) ? $cache['data'] : null; + $cachedCtime = !empty($cache) ? $cache['ctime'] : 0; $now = time(); //如果关闭缩略图 @@ -318,7 +330,7 @@ Class SiteController extends Controller { } }else if ( !empty($cachedData) && !empty($cachedData['size']) && in_array($cachedData['size'], array('vm', 'am')) ) { //如果是视频、音乐封面图,则缓存 10 分钟 - if ($now - $cachedCtime > 600) { + if ($cachedCtime > 0 && $now - $cachedCtime > 600) { $cachedData = null; } } @@ -635,6 +647,23 @@ Class SiteController extends Controller { $videoExtension = pathinfo($arr['path'], PATHINFO_EXTENSION); $videoSourceType = Html::getMediaSourceType($videoExtension); + //从缓存数据获取封面图 + $poster = '/img/beauty/audio_bg.jpg'; + $cacheSeconds = 86400; + $cachedParentData = Common::getCacheFromFile($cacheParentDataId, $cacheSeconds); + if (!empty($cachedParentData)) { + $mp3 = $cachedParentData[$videoId]; + if (!empty($mp3['snapshot'])) { + $poster = $mp3['snapshot']; + }else { + $imgExts = !empty(FSC::$app['config']['supportedImageExts']) ? FSC::$app['config']['supportedImageExts'] : array('jpg', 'jpeg', 'png', 'webp', 'gif'); + $matchedImage = Html::searchImageByFilename($mp3['filename'], $cachedParentData, $imgExts); + if (!empty($matchedImage)) { + $poster = $matchedImage['path']; + } + } + } + //获取联系方式 $maxScanDeep = 0; //最大扫描目录级数 @@ -656,7 +685,7 @@ Class SiteController extends Controller { $params = compact( 'videoUrl', 'videoId', 'videoFilename', 'cateId', 'cacheParentDataId', 'page', 'pageSize', - 'copyright', 'isAdminIp', 'videoExtension', 'videoSourceType' + 'copyright', 'isAdminIp', 'videoExtension', 'videoSourceType', 'poster' ); return $this->render($viewName, $params, $pageTitle); } diff --git a/themes/beauty/views/site/index.php b/themes/beauty/views/site/index.php index b7a118d..3814ef0 100644 --- a/themes/beauty/views/site/index.php +++ b/themes/beauty/views/site/index.php @@ -407,10 +407,20 @@ eof; $linkUrl .= "&page={$viewData['page']}&limit={$viewData['pageSize']}"; } + $snapshot = '/img/beauty/audio_icon.jpeg'; + if (!empty($file['snapshot'])) { + $snapshot = $file['snapshot']; + }else { //尝试找出同名的图片文件 + $matchedImage = Html::searchImageByFilename($file['filename'], $viewData['allFiles'], $imgExts); + if (!empty($matchedImage)) { + $snapshot = $matchedImage['path']; + } + } + echo << - {$file['filename']} diff --git a/themes/beauty/views/site/mp3player.php b/themes/beauty/views/site/mp3player.php index 29f3680..0d399ea 100644 --- a/themes/beauty/views/site/mp3player.php +++ b/themes/beauty/views/site/mp3player.php @@ -56,7 +56,7 @@ controls playsinline data-setup='{"inactivityTimeout":0}' - poster="/img/beauty/audio_bg.jpg" + poster="" data-src="" data-type="" id="my-player"> @@ -79,7 +79,7 @@