fix: serve WebP with a portable MIME type

This commit is contained in:
nyan404 committed 2026-11-17 16:34:10 +03:00
1 parent d182cd30fa
commit 30d012de80
5 files changed
+23 -1

No files matched your search

+1
View File
@@ -27,6 +27,7 @@
- Weather easter egg: `python -m unittest discover -s tests -p test_weather.py -q` and `node --check web/weather-effect.js`; inspect `/belki` and `/admin` with rain/snow/mixed fixtures, mobile widths, reduced motion, and API failure. Prefer the remote test-zone MCP when exposed; otherwise use an isolated local server with temporary SQLite paths and stop it after validation. Keep screenshots outside the repo. - Weather easter egg: `python -m unittest discover -s tests -p test_weather.py -q` and `node --check web/weather-effect.js`; inspect `/belki` and `/admin` with rain/snow/mixed fixtures, mobile widths, reduced motion, and API failure. Prefer the remote test-zone MCP when exposed; otherwise use an isolated local server with temporary SQLite paths and stop it after validation. Keep screenshots outside the repo.
- For CI-only changes, prefer a targeted YAML parse and a diff review. - For CI-only changes, prefer a targeted YAML parse and a diff review.
- For image/runtime changes, check the `Dockerfile`, Python entrypoint, static asset serving, and CI job together. - For image/runtime changes, check the `Dockerfile`, Python entrypoint, static asset serving, and CI job together.
- Python 3.12 slim does not recognize WebP in strict `mimetypes.guess_type()` without system MIME data. Keep the explicit `image/webp` response in `Handler._file`; `tests.test_lu_trainer` covers a missing MIME mapping. Do not rely only on workstation MIME tables when adding media formats.
- For k3s changes, validate YAML and current cluster inventory first; keep `KUBE_CONFIG_B64` masked/protected in GitLab. - For k3s changes, validate YAML and current cluster inventory first; keep `KUBE_CONFIG_B64` masked/protected in GitLab.
- Do not add unnecessary local build artifacts or temporary files to the repository. - Do not add unnecessary local build artifacts or temporary files to the repository.
- Admin report UI changes should cover both the persisted history flag and browser behavior: run the focused history/admin filter tests, Python compilation, `git diff --check`, and a JavaScript syntax check for `web/admin.html`. The report list defaults to the calendar current month; do not silently replace the existing sort order. - Admin report UI changes should cover both the persisted history flag and browser behavior: run the focused history/admin filter tests, Python compilation, `git diff --check`, and a JavaScript syntax check for `web/admin.html`. The report list defaults to the calendar current month; do not silently replace the existing sort order.
+2
View File
@@ -1,5 +1,7 @@
# MEMORY # MEMORY
- 2026-11-17: CI Python 3.12 slim returns `(None, None)` for `mimetypes.guess_type("forest.webp")`; WebP is only in its non-strict common table without system MIME data. `Handler._file` now explicitly serves `.webp` as `image/webp`, independent of host MIME tables. HTTP regression coverage simulates an absent mapping; retain it when modifying static serving.
- 2026-11-17: owner renamed the trainer public page to `/trainer` (also `/trainer/`). Homepage navigation, Python/Nginx page aliases and browser entry URLs follow that address. Internal `web/lu-trainer.html` and `/lu-trainer/` asset URLs are unchanged; the prior `/lu-trainer` page address below is historical. No push/deployment. - 2026-11-17: owner renamed the trainer public page to `/trainer` (also `/trainer/`). Homepage navigation, Python/Nginx page aliases and browser entry URLs follow that address. Internal `web/lu-trainer.html` and `/lu-trainer/` asset URLs are unchanged; the prior `/lu-trainer` page address below is historical. No push/deployment.
- 2026-11-17: implemented the owner-requested LU trainer at `/lu-trainer` and `/lu-trainer/`. The DOM-free original JS engine preserves the documented upstream training model (not verified CatWar rules); both grids consume snapshots without hidden hazards, with temporary hints disclosed only as action results. One in-memory attempt, separate notes, confirmed resets, terminal action locks and no CatWar account/report integration. Original imagegen forest/cat provenance is in `web/lu-trainer/assets/README.md`. No new runtime dependency. Local Node/Python/Chromium and isolated Nginx checks are recorded in `structure_and_data.md`; no registry push or deployment. - 2026-11-17: implemented the owner-requested LU trainer at `/lu-trainer` and `/lu-trainer/`. The DOM-free original JS engine preserves the documented upstream training model (not verified CatWar rules); both grids consume snapshots without hidden hazards, with temporary hints disclosed only as action results. One in-memory attempt, separate notes, confirmed resets, terminal action locks and no CatWar account/report integration. Original imagegen forest/cat provenance is in `web/lu-trainer/assets/README.md`. No new runtime dependency. Local Node/Python/Chromium and isolated Nginx checks are recorded in `structure_and_data.md`; no registry push or deployment.
+6 -1
View File
@@ -585,8 +585,13 @@ class Handler(BaseHTTPRequestHandler):
def _file(self, path: Path) -> None: def _file(self, path: Path) -> None:
"""Отдаёт существующий статический файл с подходящим MIME-типом.""" """Отдаёт существующий статический файл с подходящим MIME-типом."""
content = path.read_bytes() content = path.read_bytes()
# Python slim images may lack the system MIME database for WebP.
content_type = (
"image/webp" if path.suffix.lower() == ".webp"
else mimetypes.guess_type(path.name)[0] or "application/octet-stream"
)
self.send_response(200) self.send_response(200)
self.send_header("Content-Type", mimetypes.guess_type(path.name)[0] or "application/octet-stream") self.send_header("Content-Type", content_type)
self.send_header("Content-Length", str(len(content))) self.send_header("Content-Length", str(len(content)))
self.end_headers() self.end_headers()
self.wfile.write(content) self.wfile.write(content)
+6
View File
@@ -1,5 +1,11 @@
# Structure and Data # Structure and Data
## Portable WebP MIME response — 2026-11-17
- Reproduced the reported CI mismatch in stock `python:3.12-slim` (Python 3.12.16): strict `mimetypes.guess_type("forest.webp")` returns `(None, None)` while the non-strict common table contains `image/webp`. The workstation MIME database masked this during the original checks.
- `app/main.py` `Handler._file` now explicitly sets `image/webp` for `.webp` (case-insensitive); all other formats keep the existing lookup/fallback. No dependency, image definition, route, game logic or gateway configuration change. `tests/test_lu_trainer.py` adds an HTTP regression check with MIME guessing unavailable.
- Validation passed: 4 focused trainer HTTP tests locally; all 178 Python discovery tests in disposable `python:3.12-slim` (Python 3.12.16), with source mounted read-only and test stores under container `/tmp`; Python source compilation and diff checks. The container, pulled image and temporary local venv/data were removed. Remote test-zone/token-optimizer MCP are unavailable. No push/deployment.
## Trainer public route — 2026-11-17 ## Trainer public route — 2026-11-17
- Owner requested `/trainer` instead of `/lu-trainer`. Python and Nginx now expose `/trainer` and `/trainer/`; the homepage link and browser entry URLs use `/trainer`. Internal `web/lu-trainer.html` and `/lu-trainer/` asset URLs are retained. No game mechanics, runtime dependencies or deployment changes. - Owner requested `/trainer` instead of `/lu-trainer`. Python and Nginx now expose `/trainer` and `/trainer/`; the homepage link and browser entry URLs use `/trainer`. Internal `web/lu-trainer.html` and `/lu-trainer/` asset URLs are retained. No game mechanics, runtime dependencies or deployment changes.
+8
View File
@@ -2,6 +2,7 @@
import http.client import http.client
import threading import threading
import unittest import unittest
from unittest.mock import patch
from app.main import BelkiHTTPServer, Handler from app.main import BelkiHTTPServer, Handler
@@ -44,6 +45,13 @@ class TrainerRoutesTests(unittest.TestCase):
self.assertIn(media_type, content_type) self.assertIn(media_type, content_type)
self.assertGreater(len(content), 0) self.assertGreater(len(content), 0)
def test_webp_mime_does_not_depend_on_system_mime_database(self):
with patch('app.main.mimetypes.guess_type', return_value=(None, None)):
status, content_type, content = self.get('/lu-trainer/assets/forest.webp')
self.assertEqual(status, 200)
self.assertEqual(content_type, 'image/webp')
self.assertTrue(content.startswith(b'RIFF'))
def test_missing_and_escaping_assets_are_not_served(self): def test_missing_and_escaping_assets_are_not_served(self):
for path in ['/lu-trainer/missing.js', '/lu-trainer/%2e%2e/%2e%2e/project-info.txt']: for path in ['/lu-trainer/missing.js', '/lu-trainer/%2e%2e/%2e%2e/project-info.txt']:
self.assertEqual(self.get(path)[0], 404) self.assertEqual(self.get(path)[0], 404)