From: Michael Tremer Date: Tue, 11 Feb 2025 16:29:48 +0000 (+0000) Subject: mirrors: Send the correct MIME type X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=482bc45ede0eead599a50fd684d94e04f0c47fb1;p=pbs.git mirrors: Send the correct MIME type Signed-off-by: Michael Tremer --- diff --git a/configure.ac b/configure.ac index 41c8e636..0171bdab 100644 --- a/configure.ac +++ b/configure.ac @@ -93,6 +93,7 @@ AX_PYTHON_MODULE([jinja2], [fatal]) AX_PYTHON_MODULE([kerberos], [fatal]) AX_PYTHON_MODULE([ldap], [fatal]) AX_PYTHON_MODULE([location], [fatal]) +AX_PYTHON_MODULE([magic], [fatal]) AX_PYTHON_MODULE([markdown], [fatal]) AX_PYTHON_MODULE([pakfire], [fatal]) AX_PYTHON_MODULE([pygments], [fatal]) diff --git a/src/buildservice/__init__.py b/src/buildservice/__init__.py index 24bc179d..bcff0b0c 100644 --- a/src/buildservice/__init__.py +++ b/src/buildservice/__init__.py @@ -6,6 +6,7 @@ import configparser import datetime import functools import logging +import magic import os import shutil import ssl @@ -384,6 +385,17 @@ class Backend(object): return s + # Initialize libmagic + magic = magic.Magic(mime=True, uncompress=False) + + @run_in_thread + def mimetype(self, path): + # Make the path absolute + if not path.startswith("/"): + path = self.path(path) + + return self.magic.from_file(path) + @run_in_thread def makedirs(self, path, **kwargs): """ diff --git a/src/web/mirrors.py b/src/web/mirrors.py index b3d020b1..cb305bf0 100644 --- a/src/web/mirrors.py +++ b/src/web/mirrors.py @@ -182,8 +182,11 @@ class DownloadsHandler(base.BaseHandler): if not s: raise tornado.web.HTTPError(404) + # Fetch the MIME type + mimetype = await self.backend.mimetype(path) + # Send a couple of headers - self.set_header("Content-Type", "application/octet-stream") + self.set_header("Content-Type", mimetype or "application/octet-stream") self.set_header("Content-Length", s.st_size) self.set_header("Last-Modified", datetime.datetime.fromtimestamp(s.st_mtime)) self.set_header("Etag", "%x-%x" % (int(s.st_mtime), s.st_size))