Skip to content

Commit

Permalink
Fix: Handle IPFS downloads in download_file_to_buffer when return 413.
Browse files Browse the repository at this point in the history
  • Loading branch information
1yam committed Jul 11, 2023
1 parent b30cf00 commit 067c63b
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/aleph/sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,21 +633,25 @@ async def download_file_to_buffer(
:param chunk_size: Size of the chunk to download.
"""
IPFS_HASH = ItemHash(file_hash)
if ItemType.from_hash(IPFS_HASH) == ItemType.ipfs:
return await self.download_file_ipfs_to_buffer(
file_hash, output_buffer, chunk_size
)

async with aiohttp.ClientSession() as session:
async with self.http_session.get(
f"/api/v0/storage/raw/{file_hash}"
) as response:
response.raise_for_status()

while True:
chunk = await response.content.read(chunk_size)
if not chunk:
break
output_buffer.write(chunk)
if response.status == 200:
response.raise_for_status()
while True:
chunk = await response.content.read(chunk_size)
if not chunk:
break
output_buffer.write(chunk)
if response.status == 413:
if ItemType.from_hash(IPFS_HASH) == ItemType.ipfs:
return await self.download_file_ipfs_to_buffer(
file_hash, output_buffer, chunk_size
)
else:
raise Exception("Unsupported file hash")

async def download_file_ipfs_to_buffer(
self,
Expand Down

0 comments on commit 067c63b

Please sign in to comment.