From 44a670165c8dda3ad48a8efb405a52d31699daae Mon Sep 17 00:00:00 2001 From: filesite Date: Sat, 28 Sep 2024 08:24:11 +0800 Subject: [PATCH] improve video and audio dir snap get --- plugins/Common.php | 4 +-- themes/beauty/controller/SiteController.php | 31 +++++++++++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/plugins/Common.php b/plugins/Common.php index cd65d46..82f2b32 100644 --- a/plugins/Common.php +++ b/plugins/Common.php @@ -553,7 +553,7 @@ Class Common { //从文件缓存读取数据 //expireSeconds: 缓存失效时间,默认10分钟 - public static function getCacheFromFile($key, $expireSeconds = 600, $cacheSubDir = '') { + public static function getCacheFromFile($key, $expireSeconds = 600, $cacheSubDir = '', $withCreateTime = false) { $cacheDir = __DIR__ . '/../runtime/cache/'; //子目录支持 if (!empty($cacheSubDir)) { @@ -568,7 +568,7 @@ Class Common { //如果缓存没有失效 $now = time(); if ($now - $data['ctime'] <= $expireSeconds) { - return $data['data']; + return empty($withCreateTime) ? $data['data'] : $data; }else { return null; } diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index 1ce7440..28fadb0 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -281,6 +281,13 @@ Class SiteController extends Controller { } //根据目录id,获取第一张图网址作为封面图返回 + /** + * size可选值: + * original - 原图 + * small - 缩略图 + * vm - 视频封面图(vmeta) + * am - 音乐封面图(ameta) + **/ public function actionDirsnap() { $code = 1; $msg = 'OK'; @@ -298,13 +305,22 @@ Class SiteController extends Controller { $cacheKey = $this->getCacheKey($cateId, 'snap'); $expireSeconds = FSC::$app['config']['screenshot_expire_seconds']; //有效期3650天 $cacheSubDir = 'dir'; - $cachedData = Common::getCacheFromFile($cacheKey, $expireSeconds, $cacheSubDir); + $withCreateTime = true; //返回数据的缓存时间 + $cache = Common::getCacheFromFile($cacheKey, $expireSeconds, $cacheSubDir, $withCreateTime); + $cachedData = $cache['data']; + $cachedCtime = $cache['ctime']; + $now = time(); //如果关闭缩略图 if (empty(FSC::$app['config']['enableSmallImage']) || FSC::$app['config']['enableSmallImage'] === 'false') { if (!empty($cachedData) && !empty($cachedData['size']) && $cachedData['size'] == 'small') { $cachedData = null; } + }else if ( !empty($cachedData) && !empty($cachedData['size']) && in_array($cachedData['size'], array('vm', 'am')) ) { + //如果是视频、音乐封面图,则缓存 10 分钟 + if ($now - $cachedCtime > 600) { + $cachedData = null; + } } //弃用老版本数据格式,抛弃没有size属性的 @@ -339,7 +355,7 @@ Class SiteController extends Controller { if (!empty($cachedData)) { $url = $cachedData['snapshot']; $cacheSubDir = 'dir'; - $size = 'vm'; + $size = 'vm'; //视频封面图 Common::saveCacheToFile($cacheKey, compact('url', 'size'), $cacheSubDir); } }else { @@ -347,12 +363,16 @@ Class SiteController extends Controller { $firstVideo = $scanner->getSnapshotImage($realpath, $audioExts); if (!empty($firstVideo)) { $url = '/img/beauty/audio_icon.jpeg'; + + //TODO: 获取音乐封面图 + //$size = 'am'; //音乐封面图 + } } }else { $url = $imgFile['path']; $img_id = $imgFile['id']; - $size = 'orignal'; + $size = 'orignal'; //原尺寸 //小尺寸图片支持 if (!empty(FSC::$app['config']['enableSmallImage']) && FSC::$app['config']['enableSmallImage'] !== 'false') { @@ -362,7 +382,7 @@ Class SiteController extends Controller { $cachedData = Common::getCacheFromFile($cacheKey_smimg, $expireSeconds, $cacheSubDir); if (!empty($cachedData)) { //已经有缩略图 $url = $cachedData; - $size = 'small'; + $size = 'small'; //缩略图 //当前目录有缩略图的时候才缓存 $cacheSubDir = 'dir'; @@ -419,7 +439,6 @@ Class SiteController extends Controller { $msg = '403 Forbidden,禁止访问'; }else { $cacheKey = $this->getCacheKey($cateId, 'snap'); - $img_id = ''; //为保持数据格式一致,图片id传空 $cacheSubDir = 'dir'; $size = 'orignal'; @@ -427,7 +446,7 @@ Class SiteController extends Controller { $size = 'small'; } - $saved = Common::saveCacheToFile($cacheKey, compact('url', 'img_id', 'size'), $cacheSubDir); + $saved = Common::saveCacheToFile($cacheKey, compact('url', 'size'), $cacheSubDir); if ($saved !== false) { $code = 1;