2222 Type ,
2323 TypeVar ,
2424 Union ,
25- Protocol ,
2625)
2726from io import BytesIO
2827
4847from pydantic import ValidationError , BaseModel
4948
5049from aleph .sdk .types import Account , GenericMessage , StorageEnum
51-
50+ from aleph . sdk . utils import copy_async_readable_to_buffer , Writable , AsyncReadable
5251from .conf import settings
5352from .exceptions import (
5453 BroadcastError ,
6968 magic = None # type:ignore
7069
7170T = TypeVar ("T" )
72- C = TypeVar ("C" , str , bytes , covariant = True )
73- U = TypeVar ("U" , str , bytes , contravariant = True )
74-
75-
76- class AsyncReadable (Protocol [C ]):
77- async def read (self , n : int = - 1 ) -> C :
78- ...
79-
80-
81- class Writable (Protocol [U ]):
82- def write (self , buffer : U ) -> int :
83- ...
84-
85-
86- async def copy_async_readable_to_buffer (
87- readable : AsyncReadable [C ], buffer : Writable [C ], chunk_size : int
88- ):
89- while True :
90- chunk = await readable .read (chunk_size )
91- if not chunk :
92- break
93- buffer .write (chunk )
9471
9572
9673def async_wrapper (f ):
@@ -651,22 +628,21 @@ async def download_file_to_buffer(
651628 :param output_buffer: Writable binary buffer. The file will be written to this buffer.
652629 """
653630
654- async with aiohttp .ClientSession () as session :
655- async with self .http_session .get (
656- f"/api/v0/storage/raw/{ file_hash } "
657- ) as response :
658- if response .status == 200 :
659- await copy_async_readable_to_buffer (
660- response .content , output_buffer , chunk_size = 16 * 1024
631+ async with self .http_session .get (
632+ f"/api/v0/storage/raw/{ file_hash } "
633+ ) as response :
634+ if response .status == 200 :
635+ await copy_async_readable_to_buffer (
636+ response .content , output_buffer , chunk_size = 16 * 1024
637+ )
638+ if response .status == 413 :
639+ ipfs_hash = ItemHash (file_hash )
640+ if ipfs_hash .item_type == ItemType .ipfs :
641+ return await self .download_file_ipfs_to_buffer (
642+ file_hash , output_buffer
661643 )
662- if response .status == 413 :
663- ipfs_hash = ItemHash (file_hash )
664- if ItemType .from_hash (ipfs_hash ) == ItemType .ipfs :
665- return await self .download_file_ipfs_to_buffer (
666- file_hash , output_buffer
667- )
668- else :
669- raise FileTooLarge (f"The file from { file_hash } is too large" )
644+ else :
645+ raise FileTooLarge (f"The file from { file_hash } is too large" )
670646
671647 async def download_file_ipfs_to_buffer (
672648 self ,
@@ -687,8 +663,6 @@ async def download_file_ipfs_to_buffer(
687663 await copy_async_readable_to_buffer (
688664 response .content , output_buffer , chunk_size = 16 * 1024
689665 )
690- elif response .status == 413 :
691- raise FileTooLarge ()
692666 else :
693667 response .raise_for_status ()
694668
0 commit comments