Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(media): 非媒体响应禁止浏览器缓存

longmeidaob70031730883
diff --git a/docs/STATUS.md b/docs/STATUS.md
index e93c24415..dd9aeaac8 100644
--- a/docs/STATUS.md
+++ b/docs/STATUS.md
@@ -24,7 +24,7 @@
- 密码未开,局域网匿名可读;首启可跳过密码,配置页可改可关,登录可记住设备。
- macOS 是 reader,代码与 `peach-data` 在内置盘;`peach.local` 经 8900/8443 和 pf 提供 80/443,GET 正常、写入回 409。
- 两端用本机 CA,私钥与凭据不跨机;代码用 Git、账本单写者复制、图片用 Syncthing;坐标见 `<数据根>/config.toml`。
-- Windows ledger `0045`,76,563 行;字幕 195 行(孤立 19),175 部带字幕轨;R 盘暂缓。
+- Windows ledger `0045`,78,103 行;字幕 195 行(孤立 19),175 部带字幕轨;R 盘暂缓。
- Mac ledger 使用已授权共享副本,`in-sync`;`sources` 在内置盘,`archive`、`tools` 可在外置盘。
- Windows doctor、诊断页通过 CA;本地目录缺失,115/PikPak 可读;密码未开、历史失败为警告。
- Python 最低与默认 3.14;Windows FFmpeg/ffprobe 在 `peach-data/tools/ffmpeg`,macOS 走 PATH。
@@ -32,7 +32,7 @@
- 发行名 `peach`、目录名 `peach-app`。macOS 按待办第 26 条处理后重启菜单栏;无口令的 `peach serve --host 0.0.0.0` 拒绝启动。
- 扫描、采集 120 秒无进展预警;资料 90 秒、封面 240 秒超时可重试;见 `state/library-processing-<job_id>.issues.jsonl`。
- 口味、复核、关注等聚合按账本与文件版本缓存;女优资料后继每轮存量至多 16 条。
-- 下载后处理:16 条旧失败任务保留,部分资产已移动;既有 `task_run` 外键异常 104 条,未清理。
+- 下载后处理:16 条旧失败任务保留,部分资产已移动;既有 `task_run` 外键异常 110 条,未清理。
- [标签归一](reference-snapshots/tag-vocabulary-audit.md)
- 实体种子(ADR-0075)由扫描结算后继 `seed-import` 导入,只填空、换旧种子行;冲突与重复身份在 `generated/seed-landing.csv`。
- 推送发现开:本地 watch 加 CloudDrive 云端前缀 `/115open→B:`、`/Pikpak→A:`;关注每 60 分钟轮询;自动更新关闭。
diff --git a/src/peach/remote_media_cache.py b/src/peach/remote_media_cache.py
index 021a0e06b..01b7996b6 100644
--- a/src/peach/remote_media_cache.py
+++ b/src/peach/remote_media_cache.py
@@ -89,10 +89,6 @@ class RemoteMediaCache:
upstream = fetch()
forwarded = {name: upstream.headers[name] for name in MEDIA_HEADERS if name in upstream.headers}
forwarded.update({"cache-control": "private, max-age=31536000", **(headers or {})})
- if request.method == "HEAD":
- response = Response(status_code=upstream.status_code, headers=forwarded)
- close()
- return response
kind = upstream.headers.get("content-type", "").lower()
accepted = (upstream.status_code in (200, 206) and
(kind.startswith(("image/", "video/", "audio/")) or
@@ -107,6 +103,12 @@ class RemoteMediaCache:
byte_range = ""
elif upstream.status_code == 206 and not byte_range:
accepted = False
+ if not accepted:
+ forwarded["cache-control"] = "no-store"
+ if request.method == "HEAD":
+ response = Response(status_code=upstream.status_code, headers=forwarded)
+ close()
+ return response
slot = self._slot(key, byte_range)
def body():
try:
diff --git a/tests/test_follow_media_cache.py b/tests/test_follow_media_cache.py
index 9e084b1b8..f516dd4cc 100644
--- a/tests/test_follow_media_cache.py
+++ b/tests/test_follow_media_cache.py
@@ -85,13 +85,27 @@ class RemoteMediaCacheTests(unittest.IsolatedAsyncioTestCase):
async def test_html_and_failed_status_are_not_installed(self):
self.headers = {"content-type": "text/html", "content-length": "5"}
self.payload = b"error"
- await self.client.get("/media")
+ response = await self.client.get("/media")
+ self.assertEqual(response.headers['cache-control'], 'no-store')
self.assertIsNone(self.cache.stored("follow:1:media:0"))
self.headers["content-type"] = "video/mp4"
self.status = 403
- await self.client.get("/media")
+ response = await self.client.get("/media")
+ self.assertEqual(response.headers['cache-control'], 'no-store')
self.assertIsNone(self.cache.stored("follow:1:media:0"))
+ async def test_invalid_media_head_is_not_cacheable_and_valid_retry_is_persistent(self):
+ self.headers['content-type'] = 'text/html'
+ response = await self.client.head('/media')
+ self.assertEqual(response.headers['cache-control'], 'no-store')
+ self.assertIsNone(self.cache.stored('follow:1:media:0'))
+ self.headers['content-type'] = 'video/mp4'
+ response = await self.client.get('/media')
+ self.assertEqual(response.content, self.payload)
+ self.assertIn('max-age=31536000', response.headers['cache-control'])
+ await self.client.get('/media')
+ self.assertEqual(self.calls, 2)
+
async def test_truncated_response_leaves_no_cache_or_temporary_bytes(self):
self.payload = b"short"
with self.assertRaises(OSError):