Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat(web): 详情开场先挂本地封面海报位

longmeidao2aa974d07e0d
diff --git a/src/peach/web_catalog.py b/src/peach/web_catalog.py
index 3a2acd551..7384a30e1 100644
--- a/src/peach/web_catalog.py
+++ b/src/peach/web_catalog.py
@@ -809,6 +809,9 @@ def q_item(contract: WebContract, aid):
d["cost"] = COST.get(d["location"], "metered")
d["has_thumb"] = contract.has_snapshot(d["snapshot_path"])
d['has_local_poster'] = (contract.poster_root / f"{d['id']}_4.jpg").is_file()
+ # 详情开场把本地封面挂到播放器海报位,选哪张跟着「JAV 默认封面」设置走:
+ # 不知道封套在不在盘上,这条详情就只能永远落在预览图那一档。
+ d["has_cover"] = contract.has_cover(d.get("code"))
# 身份格的厂牌位和顶栏小圆片同一条判据:没装标识就不输出 `<img>`。规范厂牌走
# `entity_refs`,非规范的那条只有扁平 `studio` 字段,两边都要有标志,否则
# 后者会从「本来能取到图」退化成永远首字母。
diff --git a/tests/test_rm_web.py b/tests/test_rm_web.py
index 9f6986210..ef0b3a082 100644
--- a/tests/test_rm_web.py
+++ b/tests/test_rm_web.py
@@ -2128,6 +2128,18 @@ class JavModeAndCoverTests(unittest.TestCase):
self.assertTrue(rows[1]["has_cover"])
self.assertFalse(rows[2]["has_cover"])
+ def test_item_detail_also_reports_whether_a_cover_is_on_disk(self):
+ """详情开场也要能照「JAV 默认封面」设置选海报位。
+
+ 单条详情漏发这一格,播放器就只能永远落在预览图那一档:同一部作品在卡片
+ 上是封套、进详情却换了一张,两处各自一份判据迟早分叉。
+ """
+ (self.covers / "ABW-232.jpg").write_bytes(b"x")
+ self.assertTrue(rm_web.q_item(self.contract, 1)["has_cover"])
+ self.contract.cache_bust()
+ (self.covers / "ABW-232.jpg").unlink()
+ self.assertFalse(rm_web.q_item(self.contract, 1)["has_cover"])
+
def test_missing_cover_resolves_to_none_not_a_broken_path(self):
self.assertIsNone(self.contract.cover_path("ABW-232"))
self.assertIsNone(self.contract.cover_path(None))
diff --git a/tests/test_web_ui.py b/tests/test_web_ui.py
index 8a21f21c7..0e9108671 100644
--- a/tests/test_web_ui.py
+++ b/tests/test_web_ui.py
@@ -3690,6 +3690,41 @@ class WebUiSourceTests(unittest.TestCase):
self.assertPageContains('/vendor/videojs/8.24.0/video.min.js')
self.assertPageContains('/vendor/videojs/8.24.0/video-js.min.css')
+ def test_detail_opens_with_the_local_cover_before_the_video_loads(self):
+ """详情开场先把本地封面挂上海报位。
+
+ video.js 按需加载,流源又是一趟解析往返:这中间画面不该是黑的。移除
+ 交给播放器自己:挂载前是 `<video>` 的原生 poster,挂载后是 video.js 的
+ 海报层,开播即收,详情侧不另写一套收尾逻辑。
+ """
+ self.assertPageContains("function detailPosterUrl(it){")
+ self.assertPageContains("const vv=$('#vid'),poster=detailPosterUrl(it);")
+ self.assertPageContains("poster:options.poster||detailPosterUrl(it)")
+
+ def test_detail_poster_follows_the_jav_image_preference(self):
+ body = self.app_js.split('function detailPosterUrl(it){', 1)[1].split('\n}', 1)[0]
+ self.assertIn("javImageKind(it,appSettings.javImage)==='cover'", body)
+ self.assertIn("`/cover?code=${encodeURIComponent(it.code||'')}`", body)
+ self.assertIn("/poster?id=${it.id}&c=4", body)
+
+ def test_changing_the_jav_image_preference_repaints_the_open_detail(self):
+ cover_body = self.app_js.split("wireIconSwitch(mount,'data-jav-image-choice',choice=>{", 1)[1].split('});', 1)[0]
+ self.assertIn("repaintDetailPoster();", cover_body)
+ repaint = self.app_js.split('function repaintDetailPoster(){', 1)[1].split('\n}', 1)[0]
+ self.assertIn("detailPlayer.poster(poster)", repaint)
+ self.assertIn("$('#vid')?.setAttribute('poster',poster)", repaint)
+ # 海报层属于详情播放器,函数就排在它前面;开场判据里 current 是详情独有的状态。
+ self.assertLess(self.app_js.index('function repaintDetailPoster(){'),
+ self.app_js.index('async function mountDetailPlayer('))
+
+ def test_follow_detail_poster_survives_the_player_mount(self):
+ """video.js 只认 options 里的海报,不读元素上的 poster 属性。
+
+ 关注视频的缩略图已经写在元素上,挂载那一刻却被丢掉:开播前又剩黑场。
+ 同一条 poster 递进 options,挂载前后看到的才是同一张图。
+ """
+ self.assertPageContains("poster:item.thumb_url")
+
def test_the_player_script_is_fetched_on_demand_instead_of_in_the_first_paint(self):
"""video.js 676KB,只有开始看片才用得上,和 Swiper 同一口径不进首屏。
diff --git a/web/app.js b/web/app.js
index 394ad0b80..e0d922ee0 100644
--- a/web/app.js
+++ b/web/app.js
@@ -548,6 +548,7 @@ function renderJavImageSetting(){
appSettings.javImage=normalizeJavImage(choice);saveSettings();
syncJavImages(document,appSettings.javImage);
document.querySelectorAll('img[data-jav-image].cover').forEach(coverAnchor);
+ repaintDetailPoster();
});
const size=$('#javSizeSetting');
size.innerHTML=iconSwitchHtml('jav-size','JAV 封面默认大小',
@@ -2177,6 +2178,15 @@ function mountPlayerSubtitles(player,assetId){
});
}).catch(()=>{});
}
+/* 换「JAV 默认封面」时,开着的详情把海报位跟同一张图一起换:挂载了走 player 的
+ 海报层,脚本还在路上时改元素上的原生 poster,否则下一次开播前看到的还是旧
+ 那张。详情没开(current 为空)或这条没有可用的本地图时不动。 */
+function repaintDetailPoster(){
+ const poster=current?detailPosterUrl(current):'';
+ if(!poster)return;
+ if(detailPlayer&&!detailPlayer.isDisposed())detailPlayer.poster(poster);
+ else $('#vid')?.setAttribute('poster',poster);
+}
async function mountDetailPlayer(it,video,autoplay,options={}){
if(detailPlayer)return detailPlayer;
/* 从小窗展开回来或带 `?t=` 深链进来时从记下的时刻接着放;展开时如果正在放,回来也接着放。 */
@@ -2193,6 +2203,9 @@ async function mountDetailPlayer(it,video,autoplay,options={}){
}
detailPlayer=globalThis.videojs(video,{
controls:true,preload:'metadata',language:'zh-CN',responsive:true,
+ /* video.js 只认 options 里的海报,不读 video 元素上的 poster 属性;不传,
+ 开播前那层本地封面就在挂载那一刻被丢掉。 */
+ poster:options.poster||detailPosterUrl(it),
controlBar:{
pictureInPictureToggle:true,currentTimeDisplay:true,timeDivider:true,
durationDisplay:true,remainingTimeDisplay:false
@@ -2828,6 +2841,16 @@ function javArtwork(it,layout,eager=false){
:`<img class="poster" src="${thumb}" alt="" loading="${eager?'eager':'lazy'}"${frame}>`;
return image.replace('<img ',`<img data-jav-image="${it.id}" data-jav-cover="${esc(cover)}" data-jav-thumb="${esc(thumb)}" data-jav-image-layout="${layout}" `);
}
+/* 详情开场给播放器的海报位:video.js 的脚本还在下载、流源还没接上时,画面先给本地
+ 封面,不留一块黑。选哪张与卡片同一份判据,番号作品跟随「JAV 默认封面」设置——
+ 官方封套或预览图;其它媒体退到本地预览格。返回空串表示这条没有可用的本地图,
+ 播放器照旧从黑场开始。 */
+function detailPosterUrl(it){
+ const thumb=(it.has_thumb||it.has_local_poster)?`/poster?id=${it.id}&c=4`:'';
+ if(!it.is_jav)return thumb;
+ return javImageKind(it,appSettings.javImage)==='cover'
+ ?`/cover?code=${encodeURIComponent(it.code||'')}`:thumb;
+}
/* 卡片署名。版次队列要和「接着看」长得一样,就必须用同一份身份推导——各算各的
迟早会在同名 creator/performer 那 35 组上分叉,同一条作品在两处指向两个实体。
`linked=false` 给队列用:整行本身就是一个 <button>,里面再嵌 <button> 会被
@@ -5796,6 +5819,7 @@ async function openFollowDetail(id,push=true,mediaIndex=null,preserveReturn=fals
source:{src,type:selectedMedia?.media_type||item.media_type||'video/mp4'},
checkSourceStatus:false,
size:selectedMedia?.size,
+ poster:item.thumb_url,
mediaPromise
});
const stopFollowAmbient=mountPlayerAmbient(followVideo);
@@ -10198,7 +10222,8 @@ async function openItem(id,push=true,queueContext=null,anchor=null){
};
$('#likeBtn').onclick=()=>savePreference({liked:$('#likeBtn').getAttribute('aria-pressed')!=='true'});
$('#savePreference').onclick=savePreference;
- const vv=$('#vid');
+ const vv=$('#vid'),poster=detailPosterUrl(it);
+ if(poster)vv.poster=poster;
vv.addEventListener('play',()=>{if(!$('#stage').dataset.c){$('#stage').dataset.c='1';
api('/api/play',{method:'POST',body:JSON.stringify({id:it.id})})}});
if(it.play_seconds&&realDuration(it.duration)){