Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/metadata.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 2021 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 base64 | import base64 | ||||
import re | |||||
import iso8601 | import iso8601 | ||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from swh.model import hashutil, identifiers | from swh.model import hashutil, identifiers | ||||
from swh.model.model import MetadataAuthority, MetadataAuthorityType | from swh.model.model import MetadataAuthority, MetadataAuthorityType | ||||
from swh.web.api.apidoc import api_doc, format_docstring | from swh.web.api.apidoc import api_doc, format_docstring | ||||
▲ Show 20 Lines • Show All 115 Lines • ▼ Show 20 Lines | def api_raw_extrinsic_metadata_swhid(request, target): | ||||
for metadata in result_page.results: | for metadata in result_page.results: | ||||
result = converters.from_raw_extrinsic_metadata(metadata) | result = converters.from_raw_extrinsic_metadata(metadata) | ||||
# We can't reliably send metadata directly, because it is a bytestring, | # We can't reliably send metadata directly, because it is a bytestring, | ||||
# and we have to return JSON documents. | # and we have to return JSON documents. | ||||
result["metadata_url"] = reverse( | result["metadata_url"] = reverse( | ||||
"api-1-raw-extrinsic-metadata-get", | "api-1-raw-extrinsic-metadata-get", | ||||
url_args={"id": hashutil.hash_to_hex(metadata.id)}, | url_args={"id": hashutil.hash_to_hex(metadata.id)}, | ||||
query_params={"filename": f"{target}_metadata"}, | |||||
request=request, | request=request, | ||||
) | ) | ||||
results.append(result) | results.append(result) | ||||
response = { | response = { | ||||
"results": results, | "results": results, | ||||
"headers": {}, | "headers": {}, | ||||
Show All 23 Lines | def api_raw_extrinsic_metadata_get(request, id): | ||||
metadata = archive.storage.raw_extrinsic_metadata_get_by_ids( | metadata = archive.storage.raw_extrinsic_metadata_get_by_ids( | ||||
[hashutil.hash_to_bytes(id)] | [hashutil.hash_to_bytes(id)] | ||||
) | ) | ||||
if not metadata: | if not metadata: | ||||
raise NotFoundExc( | raise NotFoundExc( | ||||
"Metadata not found. Use /raw-extrinsic-metadata/swhid/ to access metadata." | "Metadata not found. Use /raw-extrinsic-metadata/swhid/ to access metadata." | ||||
) | ) | ||||
return HttpResponse(metadata[0].metadata, content_type="application/octet-stream") | response = HttpResponse( | ||||
metadata[0].metadata, content_type="application/octet-stream" | |||||
) | |||||
filename = request.query_params.get("filename") | |||||
if filename and re.match("[a-zA-Z0-9:._-]+", filename): | |||||
response["Content-disposition"] = f'attachment; filename="{filename}"' | |||||
else: | |||||
# It should always be not-None and match the regexp if the URL was created by | |||||
# /raw-extrinsic-metadata/swhid/, but we're better safe than sorry. | |||||
response["Content-disposition"] = "attachment" | |||||
return response |