# HG changeset patch # User Marcin Kuzminski # Date 2020-02-18 15:05:30 # Node ID 490fbd1de502df0e15810540a39e22ac744bf279 # Parent a948e8d84a2cbe87c2587f16a8fdc6b60c897d95 file-store: use our own logic for setting content-type. This solves a problem when previously used resolver set different content-type+content-encoding which is an incorrect behaviour. diff --git a/rhodecode/apps/file_store/views.py b/rhodecode/apps/file_store/views.py --- a/rhodecode/apps/file_store/views.py +++ b/rhodecode/apps/file_store/views.py @@ -33,6 +33,7 @@ from rhodecode.lib import audit_logger from rhodecode.lib.auth import ( CSRFRequired, NotAnonymous, HasRepoPermissionAny, HasRepoGroupPermissionAny, LoginRequired) +from rhodecode.lib.vcs.conf.mtypes import get_mimetypes_db from rhodecode.model.db import Session, FileStore, UserApiKeys log = logging.getLogger(__name__) @@ -46,6 +47,15 @@ class FileStoreView(BaseAppView): self.storage = utils.get_file_storage(self.request.registry.settings) return c + def _guess_type(self, file_name): + """ + Our own type guesser for mimetypes using the rich DB + """ + if not hasattr(self, 'db'): + self.db = get_mimetypes_db() + _content_type, _encoding = self.db.guess_type(file_name, strict=False) + return _content_type, _encoding + def _serve_file(self, file_uid): if not self.storage.exists(file_uid): @@ -92,10 +102,18 @@ class FileStoreView(BaseAppView): FileStore.bump_access_counter(file_uid) file_path = self.storage.store_path(file_uid) - return FileResponse(file_path) + content_type = 'application/octet-stream' + content_encoding = None + + _content_type, _encoding = self._guess_type(file_path) + if _content_type: + content_type = _content_type + # For file store we don't submit any session data, this logic tells the # Session lib to skip it setattr(self.request, '_file_response', True) + return FileResponse(file_path, request=self.request, + content_type=content_type, content_encoding=content_encoding) @LoginRequired() @NotAnonymous() diff --git a/rhodecode/lib/vcs/conf/mtypes.py b/rhodecode/lib/vcs/conf/mtypes.py --- a/rhodecode/lib/vcs/conf/mtypes.py +++ b/rhodecode/lib/vcs/conf/mtypes.py @@ -18,6 +18,19 @@ # RhodeCode Enterprise Edition, including its added features, Support services, # and proprietary license terms, please see https://rhodecode.com/licenses/ +DEFAULTS = { + 'encodings_map': {'.gz': 'gzip', + '.Z': 'compress', + '.bz2': 'bzip2', + '.xz': 'xz'}, + 'suffix_map': {'.svgz': '.svg.gz', + '.tgz': '.tar.gz', + '.taz': '.tar.gz', + '.tz': '.tar.gz', + '.tbz2': '.tar.bz2', + '.txz': '.tar.xz'}, +} + TYPES_MAP = [ {'.jpg': 'image/jpg', '.mid': 'audio/midi', @@ -1203,4 +1216,6 @@ def get_mimetypes_db(extra_types=None): types_map[1].update(extra_types) db = mimetypes.MimeTypes() db.types_map = types_map + db.encodings_map.update(DEFAULTS['encodings_map']) + db.suffix_map.update(DEFAULTS['suffix_map']) return db