Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/utils.py
Show All 15 Lines | |||||
import sentry_sdk | import sentry_sdk | ||||
from swh.web.common import highlightjs, service | from swh.web.common import highlightjs, service | ||||
from swh.web.common.exc import http_status_code_message | from swh.web.common.exc import http_status_code_message | ||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
reverse, | reverse, | ||||
format_utc_iso_date, | format_utc_iso_date, | ||||
rst_to_html, | rst_to_html, | ||||
browsers_supported_image_mimes, | |||||
) | ) | ||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
def get_directory_entries(sha1_git): | def get_directory_entries(sha1_git): | ||||
"""Function that retrieves the content of a directory | """Function that retrieves the content of a directory | ||||
from the archive. | from the archive. | ||||
▲ Show 20 Lines • Show All 204 Lines • ▼ Show 20 Lines | ): | ||||
if license: | if license: | ||||
content_data["licenses"] = ", ".join(license["facts"][0]["licenses"]) | content_data["licenses"] = ", ".join(license["facts"][0]["licenses"]) | ||||
else: | else: | ||||
content_data["licenses"] = "not detected" | content_data["licenses"] = "not detected" | ||||
return content_data | return content_data | ||||
_browsers_supported_image_mimes = set( | |||||
[ | |||||
"image/gif", | |||||
"image/png", | |||||
"image/jpeg", | |||||
"image/bmp", | |||||
"image/webp", | |||||
"image/svg", | |||||
"image/svg+xml", | |||||
] | |||||
) | |||||
def prepare_content_for_display(content_data, mime_type, path): | def prepare_content_for_display(content_data, mime_type, path): | ||||
"""Function that prepares a content for HTML display. | """Function that prepares a content for HTML display. | ||||
The function tries to associate a programming language to a | The function tries to associate a programming language to a | ||||
content in order to perform syntax highlighting client-side | content in order to perform syntax highlighting client-side | ||||
using highlightjs. The language is determined using either | using highlightjs. The language is determined using either | ||||
the content filename or its mime type. | the content filename or its mime type. | ||||
If the mime type corresponds to an image format supported | If the mime type corresponds to an image format supported | ||||
Show All 18 Lines | if not language: | ||||
language = highlightjs.get_hljs_language_from_mime_type(mime_type) | language = highlightjs.get_hljs_language_from_mime_type(mime_type) | ||||
if not language: | if not language: | ||||
language = "nohighlight" | language = "nohighlight" | ||||
elif mime_type.startswith("application/"): | elif mime_type.startswith("application/"): | ||||
mime_type = mime_type.replace("application/", "text/") | mime_type = mime_type.replace("application/", "text/") | ||||
if mime_type.startswith("image/"): | if mime_type.startswith("image/"): | ||||
if mime_type in _browsers_supported_image_mimes: | if mime_type in browsers_supported_image_mimes: | ||||
content_data = base64.b64encode(content_data).decode("ascii") | content_data = base64.b64encode(content_data).decode("ascii") | ||||
else: | |||||
content_data = None | |||||
if mime_type.startswith("image/svg"): | if mime_type.startswith("image/svg"): | ||||
mime_type = "image/svg+xml" | mime_type = "image/svg+xml" | ||||
if mime_type.startswith("text/"): | if mime_type.startswith("text/"): | ||||
content_data = content_data.decode("utf-8", errors="replace") | content_data = content_data.decode("utf-8", errors="replace") | ||||
return {"content_data": content_data, "language": language, "mimetype": mime_type} | return {"content_data": content_data, "language": language, "mimetype": mime_type} | ||||
▲ Show 20 Lines • Show All 456 Lines • Show Last 20 Lines |