1515from __future__ import annotations
1616
1717import ctypes
18+ import asyncio
19+ import os
20+ import mimetypes
21+ import aiofiles
1822from typing import List , Union , Callable , Dict , Awaitable , Optional , Mapping , cast
1923from abc import abstractmethod , ABC
2024
25+
2126from ._ffi_client import FfiClient , FfiHandle
2227from ._proto import ffi_pb2 as proto_ffi
2328from ._proto import participant_pb2 as proto_participant
3843from .rpc import RpcError
3944from ._proto .rpc_pb2 import RpcMethodInvocationResponseRequest
4045from .log import logger
41- import asyncio
4246
4347from .rpc import RpcInvocationData
44- from .data_stream import TextStreamWriter , FileStreamWriter
48+ from .data_stream import TextStreamWriter , FileStreamWriter , STREAM_CHUNK_SIZE
4549
4650
4751class PublishTrackError (Exception ):
@@ -552,19 +556,21 @@ async def stream_text(
552556 topic = topic ,
553557 extensions = extensions ,
554558 reply_to_id = reply_to_id ,
559+ destination_identities = destination_identities ,
555560 )
556561
557- await writer ._send_header (destination_identities = destination_identities )
562+ await writer ._send_header ()
558563
559564 return writer
560565
561566 async def stream_file (
562567 self ,
563568 file_name : str ,
564569 file_size : int | None = None ,
565- mime_type : str = "" ,
570+ mime_type : str = "application/octet-stream " ,
566571 extensions : Dict [str , str ] = {},
567572 stream_id : str | None = None ,
573+ destination_identities : List [str ] = [],
568574 ):
569575 writer = FileStreamWriter (
570576 self ,
@@ -573,10 +579,42 @@ async def stream_file(
573579 total_size = file_size ,
574580 stream_id = stream_id ,
575581 mime_type = mime_type ,
582+ destination_identities = destination_identities ,
576583 )
577584
585+ await writer ._send_header ()
586+
578587 return writer
579588
589+ async def send_file (
590+ self ,
591+ file_path : str ,
592+ destination_identities : List [str ] = [],
593+ extensions : Dict [str , str ] = {},
594+ stream_id : str | None = None ,
595+ ):
596+ file_size = os .path .getsize (file_path )
597+ file_name = os .path .basename (file_path )
598+ mime_type , _ = mimetypes .guess_type (file_path )
599+ if mime_type is None :
600+ mime_type = (
601+ "application/octet-stream" # Fallback MIME type for unknown files
602+ )
603+
604+ writer : FileStreamWriter = await self .stream_file (
605+ file_name = file_name ,
606+ file_size = file_size ,
607+ mime_type = mime_type ,
608+ extensions = extensions ,
609+ stream_id = stream_id ,
610+ destination_identities = destination_identities ,
611+ )
612+
613+ async with aiofiles .open (file_path , "rb" ) as f :
614+ while bytes := await f .read (STREAM_CHUNK_SIZE ):
615+ await writer .write (bytes )
616+ await writer .close ()
617+
580618 async def publish_track (
581619 self , track : LocalTrack , options : TrackPublishOptions = TrackPublishOptions ()
582620 ) -> LocalTrackPublication :
0 commit comments