Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/utils.py
Show First 20 Lines • Show All 123 Lines • ▼ Show 20 Lines | if mimetype.startswith('text/'): | ||||
content_data = content_data.decode('utf-8', 'replace')\ | content_data = content_data.decode('utf-8', 'replace')\ | ||||
.encode('utf-8') | .encode('utf-8') | ||||
elif encoding not in ['utf-8', 'binary']: | elif encoding not in ['utf-8', 'binary']: | ||||
content_data = content_data.decode(encoding, 'replace')\ | content_data = content_data.decode(encoding, 'replace')\ | ||||
.encode('utf-8') | .encode('utf-8') | ||||
elif mimetype.startswith('application/octet-stream'): | elif mimetype.startswith('application/octet-stream'): | ||||
# file may detect a text content as binary | # file may detect a text content as binary | ||||
# so try to decode it for display | # so try to decode it for display | ||||
encodings = ['us-ascii'] | encodings = ['us-ascii', 'utf-8'] | ||||
encodings += ['iso-8859-%s' % i for i in range(1, 17)] | encodings += ['iso-8859-%s' % i for i in range(1, 17)] | ||||
for encoding in encodings: | for enc in encodings: | ||||
try: | try: | ||||
content_data = content_data.decode(encoding)\ | content_data = content_data.decode(enc).encode('utf-8') | ||||
.encode('utf-8') | |||||
except Exception: | except Exception: | ||||
pass | pass | ||||
else: | else: | ||||
# ensure display in content view | # ensure display in content view | ||||
encoding = enc | |||||
mimetype = 'text/plain' | mimetype = 'text/plain' | ||||
break | break | ||||
return mimetype, content_data | return mimetype, encoding, content_data | ||||
def request_content(query_string, max_size=content_display_max_size, | def request_content(query_string, max_size=content_display_max_size, | ||||
raise_if_unavailable=True, re_encode=True): | raise_if_unavailable=True, re_encode=True): | ||||
"""Function that retrieves a content from the archive. | """Function that retrieves a content from the archive. | ||||
Raw bytes content is first retrieved, then the content mime type. | Raw bytes content is first retrieved, then the content mime type. | ||||
If the mime type is not stored in the archive, it will be computed | If the mime type is not stored in the archive, it will be computed | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | if not max_size or content_data['length'] < max_size: | ||||
else: | else: | ||||
content_data['raw_data'] = content_raw['data'] | content_data['raw_data'] = content_raw['data'] | ||||
if not filetype: | if not filetype: | ||||
mimetype, encoding = \ | mimetype, encoding = \ | ||||
get_mimetype_and_encoding_for_content(content_data['raw_data']) # noqa | get_mimetype_and_encoding_for_content(content_data['raw_data']) # noqa | ||||
if re_encode: | if re_encode: | ||||
mimetype, raw_data = _re_encode_content( | mimetype, encoding, raw_data = _re_encode_content( | ||||
mimetype, encoding, content_data['raw_data']) | mimetype, encoding, content_data['raw_data']) | ||||
content_data['raw_data'] = raw_data | content_data['raw_data'] = raw_data | ||||
else: | else: | ||||
content_data['raw_data'] = None | content_data['raw_data'] = None | ||||
content_data['mimetype'] = mimetype | content_data['mimetype'] = mimetype | ||||
content_data['encoding'] = encoding | content_data['encoding'] = encoding | ||||
▲ Show 20 Lines • Show All 874 Lines • Show Last 20 Lines |