+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/aleph/services/ipfs/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ async def get_ipfs_size(
dag_node = await asyncio.wait_for(
self.ipfs_client.dag.get(hash), timeout=timeout
)
result = 0
if isinstance(dag_node, str):
dag_node = json.loads(dag_node)
if isinstance(dag_node, dict):
if "Data" in dag_node and isinstance(dag_node["Data"], dict):
# This is the common structure for UnixFS nodes after aioipfs parsing
Expand All @@ -81,7 +84,11 @@ async def get_ipfs_size(
"Tsize" in dag_node
): # Sometimes it might be at the top level directly
result = dag_node["Tsize"]
elif "Links" in dag_node and isinstance(dag_node["Links"], list):
if (
result == 0
and "Links" in dag_node
and isinstance(dag_node["Links"], list)
):
total_size = 0
for link in dag_node["Links"]:
# In case it's a link list, get the Tsize property if exists
Expand Down
26 changes: 26 additions & 0 deletions tests/services/test_ipfs_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import json
from unittest.mock import AsyncMock, patch

import aioipfs
Expand Down Expand Up @@ -133,6 +134,31 @@ async def test_get_ipfs_size_non_dict():
ipfs_client.block.stat.assert_called_once_with("test_hash")


@pytest.mark.asyncio
async def test_get_ipfs_size_json():
"""Test when dag.get returns a non-dictionary (bytes)"""
# Setup
mock_dag_get_data = {
"Data": {"/": {"bytes": "CAE"}},
"Links": [
{"Hash": {"/": "hash1"}, "Name": "test1", "Tsize": 12596071},
{"Hash": {"/": "hash2"}, "Name": "test2", "Tsize": 20168090},
],
}
ipfs_client = AsyncMock()
ipfs_client.dag.get = AsyncMock(return_value=json.dumps(mock_dag_get_data))

service = IpfsService(ipfs_client=ipfs_client)

# Execute
result = await service.get_ipfs_size("test_hash")

# Assert
assert result == 32764161
ipfs_client.dag.get.assert_called_once_with("test_hash")
ipfs_client.block.stat.assert_not_called()


@pytest.mark.asyncio
async def test_get_ipfs_size_api_error():
"""Test handling of APIError"""
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载