Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/raw.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 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 | ||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from rest_framework.exceptions import PermissionDenied | |||||
from swh.model import model | from swh.model import model | ||||
from swh.model.git_objects import ( | from swh.model.git_objects import ( | ||||
content_git_object, | content_git_object, | ||||
directory_git_object, | directory_git_object, | ||||
release_git_object, | release_git_object, | ||||
revision_git_object, | revision_git_object, | ||||
snapshot_git_object, | snapshot_git_object, | ||||
) | ) | ||||
from swh.model.hashutil import hash_to_hex | from swh.model.hashutil import hash_to_hex | ||||
from swh.model.swhids import CoreSWHID, ObjectType | from swh.model.swhids import CoreSWHID, ObjectType | ||||
from swh.storage.algos.directory import directory_get | from swh.storage.algos.directory import directory_get | ||||
from swh.storage.algos.snapshot import snapshot_get_all_branches | from swh.storage.algos.snapshot import snapshot_get_all_branches | ||||
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.auth.utils import API_RAW_OBJECT_PERMISSION | |||||
from swh.web.common import archive | from swh.web.common import archive | ||||
from swh.web.common.exc import NotFoundExc | from swh.web.common.exc import NotFoundExc | ||||
from swh.web.common.utils import SWHID_RE | from swh.web.common.utils import SWHID_RE | ||||
@api_route( | @api_route( | ||||
f"/raw/(?P<swhid>{SWHID_RE})/", | f"/raw/(?P<swhid>{SWHID_RE})/", | ||||
"api-1-raw-object", | "api-1-raw-object", | ||||
throttle_scope="swh_raw_object", | |||||
) | ) | ||||
@api_doc("/raw/") | @api_doc("/raw/") | ||||
@format_docstring() | @format_docstring() | ||||
def api_raw_object(request, swhid): | def api_raw_object(request, swhid): | ||||
""" | """ | ||||
.. http:get:: /api/1/raw/(swhid)/ | .. http:get:: /api/1/raw/(swhid)/ | ||||
Get the object corresponding to the SWHID in raw form. | Get the object corresponding to the SWHID in raw form. | ||||
This endpoint exposes the internal representation (see the | This endpoint exposes the internal representation (see the | ||||
``*_git_object`` functions in :mod:`swh.model.git_objects`), and | ``*_git_object`` functions in :mod:`swh.model.git_objects`), and | ||||
so can be used to fetch a binary blob which hashes to the same | so can be used to fetch a binary blob which hashes to the same | ||||
identifier. | identifier. | ||||
.. warning:: | |||||
That endpoint is not publicly available and requires authentication and | |||||
special user permission in order to be able to request it. | |||||
:param string swhid: the object's SWHID | :param string swhid: the object's SWHID | ||||
:resheader Content-Type: application/octet-stream | :resheader Content-Type: application/octet-stream | ||||
:statuscode 200: no error | :statuscode 200: no error | ||||
:statuscode 404: the requested object can not be found in the archive | :statuscode 404: the requested object can not be found in the archive | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`raw/swh:1:snp:6a3a2cf0b2b90ce7ae1cf0a221ed68035b686f5a` | :swh_web_api:`raw/swh:1:snp:6a3a2cf0b2b90ce7ae1cf0a221ed68035b686f5a` | ||||
""" | """ | ||||
if not (request.user.is_staff or request.user.has_perm(API_RAW_OBJECT_PERMISSION)): | |||||
raise PermissionDenied() | |||||
swhid = CoreSWHID.from_string(swhid) | swhid = CoreSWHID.from_string(swhid) | ||||
object_id = swhid.object_id | object_id = swhid.object_id | ||||
object_type = swhid.object_type | object_type = swhid.object_type | ||||
def not_found(): | def not_found(): | ||||
return NotFoundExc(f"Object with id {swhid} not found.") | return NotFoundExc(f"Object with id {swhid} not found.") | ||||
▲ Show 20 Lines • Show All 45 Lines • Show Last 20 Lines |