diff --git a/swh/web/common/service.py b/swh/web/common/service.py --- a/swh/web/common/service.py +++ b/swh/web/common/service.py @@ -804,8 +804,9 @@ raise NotFoundExc(f"Content not found for revision {sha1_git}") content_d = content.to_dict() if with_data: - c = _first_element(storage.content_get([content.sha1])) - content_d["data"] = c["data"] + data = storage.content_get_data(content.sha1) + if data: + content_d["data"] = data return { "type": "file", "path": "." if not dir_path else dir_path, @@ -842,7 +843,7 @@ return converters.from_content(c.to_dict()) -def lookup_content_raw(q): +def lookup_content_raw(q: str) -> Dict[str, Any]: """Lookup the content defined by q. Args: @@ -859,14 +860,14 @@ """ c = lookup_content(q) content_sha1_bytes = hashutil.hash_to_bytes(c["checksums"]["sha1"]) - content = _first_element(storage.content_get([content_sha1_bytes])) - if not content: - algo, hash = query.parse_hash(q) + content_data = storage.content_get_data(content_sha1_bytes) + if not content_data: + algo, hash_ = query.parse_hash(q) raise NotFoundExc( - "Bytes of content with %s checksum equals to %s " - "are not available!" % (algo, hashutil.hash_to_hex(hash)) + f"Bytes of content with {algo} checksum equals " + f"to {hashutil.hash_to_hex(hash_)} are not available!" ) - return converters.from_content(content) + return converters.from_content({"sha1": content_sha1_bytes, "data": content_data}) def stat_counters(): diff --git a/swh/web/tests/api/views/test_content.py b/swh/web/tests/api/views/test_content.py --- a/swh/web/tests/api/views/test_content.py +++ b/swh/web/tests/api/views/test_content.py @@ -307,7 +307,7 @@ == "attachment; filename=content_sha1_%s_raw" % content["sha1"] ) assert rv["Content-Type"] == "application/octet-stream" - expected_data = archive_data.content_get(content["sha1"]) + expected_data = archive_data.content_get_data(content["sha1"]) assert rv.content == expected_data["data"] @@ -324,7 +324,7 @@ assert rv["Content-Type"] == "application/octet-stream" assert rv["Content-disposition"] == "attachment; filename=filename.txt" assert rv["Content-Type"] == "application/octet-stream" - expected_data = archive_data.content_get(content["sha1"]) + expected_data = archive_data.content_get_data(content["sha1"]) assert rv.content == expected_data["data"] diff --git a/swh/web/tests/browse/views/test_content.py b/swh/web/tests/browse/views/test_content.py --- a/swh/web/tests/browse/views/test_content.py +++ b/swh/web/tests/browse/views/test_content.py @@ -246,7 +246,7 @@ resp = client.get(url) - content_data = archive_data.content_get(content["sha1"])["data"] + content_data = archive_data.content_get_data(content["sha1"])["data"] assert resp.status_code == 200 assert resp["Content-Type"] == "text/plain" @@ -286,7 +286,7 @@ resp = client.get(url) filename = content["path"].split("/")[-1] - content_data = archive_data.content_get(content["sha1"])["data"] + content_data = archive_data.content_get_data(content["sha1"])["data"] assert resp.status_code == 200 assert resp["Content-Type"] == "application/octet-stream" @@ -575,7 +575,7 @@ def _process_content_for_display(archive_data, content): - content_data = archive_data.content_get(content["sha1"]) + content_data = archive_data.content_get_data(content["sha1"]) mime_type, encoding = get_mimetype_and_encoding_for_content(content_data["data"]) diff --git a/swh/web/tests/common/test_service.py b/swh/web/tests/common/test_service.py --- a/swh/web/tests/common/test_service.py +++ b/swh/web/tests/common/test_service.py @@ -472,7 +472,9 @@ if e["type"] == "file" ] expected_dir_entry = random.choice(dir_entries) - expected_data = archive_data.content_get(expected_dir_entry["checksums"]["sha1"]) + expected_data = archive_data.content_get_data( + expected_dir_entry["checksums"]["sha1"] + ) actual_dir_entry = service.lookup_directory_with_revision( revision, expected_dir_entry["name"], with_data=True @@ -598,7 +600,7 @@ def test_lookup_content_raw(archive_data, content): actual_content = service.lookup_content_raw("sha256:%s" % content["sha256"]) - expected_content = archive_data.content_get(content["sha1"]) + expected_content = archive_data.content_get_data(content["sha1"]) assert actual_content == expected_content diff --git a/swh/web/tests/conftest.py b/swh/web/tests/conftest.py --- a/swh/web/tests/conftest.py +++ b/swh/web/tests/conftest.py @@ -185,10 +185,12 @@ content, hashess={"sha1", "sha1_git", "sha256", "blake2s256"} ) - def content_get(self, cnt_id): + def content_get_data(self, cnt_id: str) -> Optional[Dict[str, Any]]: cnt_id_bytes = hash_to_bytes(cnt_id) - cnt = next(self.storage.content_get([cnt_id_bytes])) - return converters.from_content(cnt) + cnt_data = self.storage.content_get_data(cnt_id_bytes) + if cnt_data is None: + return None + return converters.from_content({"data": cnt_data, "sha1": cnt_id_bytes}) def directory_get(self, dir_id): return {"id": dir_id, "content": self.directory_ls(dir_id)} diff --git a/swh/web/tests/data.py b/swh/web/tests/data.py --- a/swh/web/tests/data.py +++ b/swh/web/tests/data.py @@ -260,10 +260,11 @@ path = "" if sha1 in content_path: path = content_path[sha1] - cnt = next(storage.content_get([sha1])) - mimetype, encoding = get_mimetype_and_encoding_for_content(cnt["data"]) - _, _, cnt["data"] = _re_encode_content(mimetype, encoding, cnt["data"]) - content_display_data = prepare_content_for_display(cnt["data"], mimetype, path) + cnt_data = storage.content_get_data(sha1) + assert cnt_data is not None + mimetype, encoding = get_mimetype_and_encoding_for_content(cnt_data) + _, _, cnt_data = _re_encode_content(mimetype, encoding, cnt_data) + content_display_data = prepare_content_for_display(cnt_data, mimetype, path) content_metadata.update( { diff --git a/swh/web/tests/strategies.py b/swh/web/tests/strategies.py --- a/swh/web/tests/strategies.py +++ b/swh/web/tests/strategies.py @@ -188,9 +188,7 @@ into the test archive. """ return new_content().filter( - lambda c: next( - get_tests_data()["storage"].content_get([hash_to_bytes(c["sha1"])]) - ) + lambda c: get_tests_data()["storage"].content_get_data(hash_to_bytes(c["sha1"])) is None )