Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/content.py
Show All 18 Lines | |||||
from swh.web.browse.utils import ( | from swh.web.browse.utils import ( | ||||
content_display_max_size, | content_display_max_size, | ||||
gen_directory_link, | gen_directory_link, | ||||
gen_link, | gen_link, | ||||
prepare_content_for_display, | prepare_content_for_display, | ||||
request_content, | request_content, | ||||
) | ) | ||||
from swh.web.common import archive, highlightjs, query | from swh.web.common import archive, highlightjs, query | ||||
from swh.web.common.exc import NotFoundExc, handle_view_exception | from swh.web.common.exc import NotFoundExc | ||||
from swh.web.common.identifiers import get_swhids_info | from swh.web.common.identifiers import get_swhids_info | ||||
from swh.web.common.typing import ContentMetadata, SWHObjectInfo | from swh.web.common.typing import ContentMetadata, SWHObjectInfo | ||||
from swh.web.common.utils import gen_path_info, reverse, swh_object_icons | from swh.web.common.utils import gen_path_info, reverse, swh_object_icons | ||||
@browse_route( | @browse_route( | ||||
r"content/(?P<query_string>[0-9a-z_:]*[0-9a-f]+.)/raw/", | r"content/(?P<query_string>[0-9a-z_:]*[0-9a-f]+.)/raw/", | ||||
view_name="browse-content-raw", | view_name="browse-content-raw", | ||||
checksum_args=["query_string"], | checksum_args=["query_string"], | ||||
) | ) | ||||
def content_raw(request, query_string): | def content_raw(request, query_string): | ||||
"""Django view that produces a raw display of a content identified | """Django view that produces a raw display of a content identified | ||||
by its hash value. | by its hash value. | ||||
The url that points to it is | The url that points to it is | ||||
:http:get:`/browse/content/[(algo_hash):](hash)/raw/` | :http:get:`/browse/content/[(algo_hash):](hash)/raw/` | ||||
""" | """ | ||||
try: | |||||
re_encode = bool(strtobool(request.GET.get("re_encode", "false"))) | re_encode = bool(strtobool(request.GET.get("re_encode", "false"))) | ||||
algo, checksum = query.parse_hash(query_string) | algo, checksum = query.parse_hash(query_string) | ||||
checksum = hash_to_hex(checksum) | checksum = hash_to_hex(checksum) | ||||
content_data = request_content(query_string, max_size=None, re_encode=re_encode) | content_data = request_content(query_string, max_size=None, re_encode=re_encode) | ||||
except Exception as exc: | |||||
return handle_view_exception(request, exc) | |||||
filename = request.GET.get("filename", None) | filename = request.GET.get("filename", None) | ||||
if not filename: | if not filename: | ||||
filename = "%s_%s" % (algo, checksum) | filename = "%s_%s" % (algo, checksum) | ||||
if ( | if ( | ||||
content_data["mimetype"].startswith("text/") | content_data["mimetype"].startswith("text/") | ||||
or content_data["mimetype"] == "inode/x-empty" | or content_data["mimetype"] == "inode/x-empty" | ||||
▲ Show 20 Lines • Show All 122 Lines • ▼ Show 20 Lines | |||||
) | ) | ||||
def content_display(request, query_string): | def content_display(request, query_string): | ||||
"""Django view that produces an HTML display of a content identified | """Django view that produces an HTML display of a content identified | ||||
by its hash value. | by its hash value. | ||||
The url that points to it is | The url that points to it is | ||||
:http:get:`/browse/content/[(algo_hash):](hash)/` | :http:get:`/browse/content/[(algo_hash):](hash)/` | ||||
""" | """ | ||||
try: | |||||
algo, checksum = query.parse_hash(query_string) | algo, checksum = query.parse_hash(query_string) | ||||
checksum = hash_to_hex(checksum) | checksum = hash_to_hex(checksum) | ||||
content_data = request_content(query_string, raise_if_unavailable=False) | content_data = request_content(query_string, raise_if_unavailable=False) | ||||
origin_url = request.GET.get("origin_url") | origin_url = request.GET.get("origin_url") | ||||
selected_language = request.GET.get("language") | selected_language = request.GET.get("language") | ||||
if not origin_url: | if not origin_url: | ||||
origin_url = request.GET.get("origin") | origin_url = request.GET.get("origin") | ||||
snapshot_id = request.GET.get("snapshot") | snapshot_id = request.GET.get("snapshot") | ||||
path = request.GET.get("path") | path = request.GET.get("path") | ||||
snapshot_context = None | snapshot_context = None | ||||
if origin_url is not None or snapshot_id is not None: | if origin_url is not None or snapshot_id is not None: | ||||
try: | try: | ||||
snapshot_context = get_snapshot_context( | snapshot_context = get_snapshot_context( | ||||
origin_url=origin_url, | origin_url=origin_url, | ||||
snapshot_id=snapshot_id, | snapshot_id=snapshot_id, | ||||
branch_name=request.GET.get("branch"), | branch_name=request.GET.get("branch"), | ||||
release_name=request.GET.get("release"), | release_name=request.GET.get("release"), | ||||
revision_id=request.GET.get("revision"), | revision_id=request.GET.get("revision"), | ||||
path=path, | path=path, | ||||
browse_context=CONTENT, | browse_context=CONTENT, | ||||
) | ) | ||||
except NotFoundExc as e: | except NotFoundExc as e: | ||||
if str(e).startswith("Origin"): | if str(e).startswith("Origin"): | ||||
raw_cnt_url = reverse( | raw_cnt_url = reverse( | ||||
"browse-content", url_args={"query_string": query_string} | "browse-content", url_args={"query_string": query_string} | ||||
) | ) | ||||
error_message = ( | error_message = ( | ||||
"The Software Heritage archive has a content " | "The Software Heritage archive has a content " | ||||
"with the hash you provided but the origin " | "with the hash you provided but the origin " | ||||
"mentioned in your request appears broken: %s. " | "mentioned in your request appears broken: %s. " | ||||
"Please check the URL and try again.\n\n" | "Please check the URL and try again.\n\n" | ||||
"Nevertheless, you can still browse the content " | "Nevertheless, you can still browse the content " | ||||
"without origin information: %s" | "without origin information: %s" | ||||
% (gen_link(origin_url), gen_link(raw_cnt_url)) | % (gen_link(origin_url), gen_link(raw_cnt_url)) | ||||
) | ) | ||||
raise NotFoundExc(error_message) | raise NotFoundExc(error_message) | ||||
else: | else: | ||||
raise e | raise e | ||||
except Exception as exc: | |||||
return handle_view_exception(request, exc) | |||||
content = None | content = None | ||||
language = None | language = None | ||||
mimetype = None | mimetype = None | ||||
if content_data["raw_data"] is not None: | if content_data["raw_data"] is not None: | ||||
content_display_data = prepare_content_for_display( | content_display_data = prepare_content_for_display( | ||||
content_data["raw_data"], content_data["mimetype"], path | content_data["raw_data"], content_data["mimetype"], path | ||||
) | ) | ||||
content = content_display_data["content_data"] | content = content_display_data["content_data"] | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | if path: | ||||
"browse-directory", | "browse-directory", | ||||
url_args={"sha1_git": root_dir}, | url_args={"sha1_git": root_dir}, | ||||
query_params=query_params, | query_params=query_params, | ||||
) | ) | ||||
breadcrumbs.append({"name": pi["name"], "url": dir_url}) | breadcrumbs.append({"name": pi["name"], "url": dir_url}) | ||||
breadcrumbs.append({"name": filename, "url": None}) | breadcrumbs.append({"name": filename, "url": None}) | ||||
if path and root_dir != path: | if path and root_dir != path: | ||||
try: | |||||
dir_info = archive.lookup_directory_with_path(root_dir, path) | dir_info = archive.lookup_directory_with_path(root_dir, path) | ||||
directory_id = dir_info["target"] | directory_id = dir_info["target"] | ||||
except Exception as exc: | |||||
return handle_view_exception(request, exc) | |||||
elif root_dir != path: | elif root_dir != path: | ||||
directory_id = root_dir | directory_id = root_dir | ||||
else: | else: | ||||
root_dir = None | root_dir = None | ||||
if directory_id: | if directory_id: | ||||
directory_url = gen_directory_link(directory_id) | directory_url = gen_directory_link(directory_id) | ||||
▲ Show 20 Lines • Show All 104 Lines • Show Last 20 Lines |