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.
This commit is contained in:
Marcin Kuzminski 2020-02-18 16:05:30 +01:00
parent ceaa1dd337
commit 82dddbb8b2
2 changed files with 34 additions and 1 deletions

View file

@ -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()

View file

@ -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