Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/content.py
# Copyright (C) 2015-2022 The Software Heritage developers | # Copyright (C) 2015-2022 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 functools | import functools | ||||
from typing import Optional | from typing import Optional | ||||
import urllib.parse | |||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from rest_framework.request import Request | from rest_framework.request import Request | ||||
from swh.web.api import utils | from swh.web.api import utils | ||||
from swh.web.api.apidoc import api_doc, format_docstring | from swh.web.api.apidoc import api_doc, format_docstring | ||||
from swh.web.api.apiurls import api_route | from swh.web.api.apiurls import api_route | ||||
from swh.web.api.views.utils import api_lookup | from swh.web.api.views.utils import api_lookup | ||||
▲ Show 20 Lines • Show All 207 Lines • ▼ Show 20 Lines | def api_content_raw(request: Request, q: str): | ||||
content_raw = archive.lookup_content_raw(q) | content_raw = archive.lookup_content_raw(q) | ||||
if not content_raw: | if not content_raw: | ||||
raise NotFoundExc("Content %s is not found." % q) | raise NotFoundExc("Content %s is not found." % q) | ||||
filename = request.query_params.get("filename") | filename = request.query_params.get("filename") | ||||
if not filename: | if not filename: | ||||
filename = "content_%s_raw" % q.replace(":", "_") | filename = "content_%s_raw" % q.replace(":", "_") | ||||
try: | |||||
filename.encode("ascii") | |||||
file_expr = 'filename="{}"'.format(filename) | |||||
except UnicodeEncodeError: | |||||
file_expr = "filename*=utf-8''{}".format(urllib.parse.quote(filename)) | |||||
response = HttpResponse( | response = HttpResponse( | ||||
generate(content_raw), content_type="application/octet-stream" | generate(content_raw), content_type="application/octet-stream" | ||||
) | ) | ||||
response["Content-disposition"] = "attachment; filename=%s" % filename | response["Content-disposition"] = "attachment; %s" % file_expr | ||||
return response | return response | ||||
@api_route(r"/content/symbol/(?P<q>.+)/", "api-1-content-symbol") | @api_route(r"/content/symbol/(?P<q>.+)/", "api-1-content-symbol") | ||||
@api_doc("/content/symbol/", tags=["hidden"]) | @api_doc("/content/symbol/", tags=["hidden"]) | ||||
def api_content_symbol(request: Request, q: str): | def api_content_symbol(request: Request, q: str): | ||||
"""Search content objects by `Ctags <http://ctags.sourceforge.net/>`_-style | """Search content objects by `Ctags <http://ctags.sourceforge.net/>`_-style | ||||
symbol (e.g., function name, data type, method, ...). | symbol (e.g., function name, data type, method, ...). | ||||
▲ Show 20 Lines • Show All 166 Lines • Show Last 20 Lines |