Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/directory.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import os | import os | ||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from django.shortcuts import render, redirect | from django.shortcuts import render, redirect | ||||
from django.template.defaultfilters import filesizeformat | from django.template.defaultfilters import filesizeformat | ||||
import sentry_sdk | |||||
from swh.web.common import service | from swh.web.common import service | ||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
reverse, gen_path_info | reverse, gen_path_info | ||||
) | ) | ||||
from swh.web.common.exc import handle_view_exception, NotFoundExc | from swh.web.common.exc import handle_view_exception, NotFoundExc | ||||
from swh.web.browse.utils import ( | from swh.web.browse.utils import ( | ||||
get_directory_entries, get_snapshot_context, | get_directory_entries, get_snapshot_context, | ||||
Show All 24 Lines | try: | ||||
dirs, files = get_directory_entries(sha1_git) | dirs, files = get_directory_entries(sha1_git) | ||||
origin_url = request.GET.get('origin_url', None) | origin_url = request.GET.get('origin_url', None) | ||||
if not origin_url: | if not origin_url: | ||||
origin_url = request.GET.get('origin', None) | origin_url = request.GET.get('origin', None) | ||||
snapshot_context = None | snapshot_context = None | ||||
if origin_url: | if origin_url: | ||||
try: | try: | ||||
snapshot_context = get_snapshot_context(origin_url=origin_url) | snapshot_context = get_snapshot_context(origin_url=origin_url) | ||||
except Exception: | except NotFoundExc: | ||||
raw_dir_url = reverse('browse-directory', | raw_dir_url = reverse('browse-directory', | ||||
url_args={'sha1_git': sha1_git}) | url_args={'sha1_git': sha1_git}) | ||||
error_message = \ | error_message = \ | ||||
('The Software Heritage archive has a directory ' | ('The Software Heritage archive has a directory ' | ||||
'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 directory ' | 'Nevertheless, you can still browse the directory ' | ||||
▲ Show 20 Lines • Show All 106 Lines • ▼ Show 20 Lines | try: | ||||
path = os.path.normpath(path) | path = os.path.normpath(path) | ||||
if not path.startswith('../'): | if not path.startswith('../'): | ||||
dir_info = service.lookup_directory_with_path(sha1_git, path) | dir_info = service.lookup_directory_with_path(sha1_git, path) | ||||
if dir_info['type'] == 'file': | if dir_info['type'] == 'file': | ||||
sha1 = dir_info['checksums']['sha1'] | sha1 = dir_info['checksums']['sha1'] | ||||
data_url = reverse('browse-content-raw', | data_url = reverse('browse-content-raw', | ||||
url_args={'query_string': sha1}) | url_args={'query_string': sha1}) | ||||
return redirect(data_url) | return redirect(data_url) | ||||
except Exception: | except Exception as exc: | ||||
pass | sentry_sdk.capture_exception(exc) | ||||
return HttpResponse(status=404) | return HttpResponse(status=404) |