Detect text files via MIME + binary heuristic instead of hardcoded extension list
Server-side _file_type() now uses mimetypes for images/known text, and falls back to null-byte check on first 8KB for everything else. Client uses the "type" field from /files API instead of local extension sets. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -74,6 +74,18 @@ def ensure_cert(cert_dir: Path, extra_ips: list[str] | None = None) -> tuple[Pat
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent
|
||||
|
||||
def _file_type(path: Path) -> str:
|
||||
ct = mimetypes.guess_type(path.name)[0] or ''
|
||||
if ct.startswith('image/'):
|
||||
return 'image'
|
||||
if ct.startswith('text/'):
|
||||
return 'text'
|
||||
try:
|
||||
with open(path, 'rb') as f:
|
||||
return 'text' if b'\x00' not in f.read(8192) else 'other'
|
||||
except OSError:
|
||||
return 'other'
|
||||
|
||||
|
||||
# ── HTTP Handler ──
|
||||
|
||||
@@ -149,7 +161,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||
files = []
|
||||
for p in sorted(self.server.upload_dir.iterdir(), key=lambda p: p.stat().st_mtime, reverse=True):
|
||||
if p.is_file():
|
||||
files.append({"name": p.name, "size": p.stat().st_size})
|
||||
files.append({"name": p.name, "size": p.stat().st_size, "type": _file_type(p)})
|
||||
self._json(files)
|
||||
|
||||
def _route_download(self):
|
||||
|
||||
Reference in New Issue
Block a user