15
15
from __future__ import annotations
16
16
17
17
import ctypes
18
+ import asyncio
19
+ import os
20
+ import mimetypes
21
+ import aiofiles
18
22
from typing import List , Union , Callable , Dict , Awaitable , Optional , Mapping , cast
19
23
from abc import abstractmethod , ABC
20
24
25
+
21
26
from ._ffi_client import FfiClient , FfiHandle
22
27
from ._proto import ffi_pb2 as proto_ffi
23
28
from ._proto import participant_pb2 as proto_participant
38
43
from .rpc import RpcError
39
44
from ._proto .rpc_pb2 import RpcMethodInvocationResponseRequest
40
45
from .log import logger
41
- import asyncio
42
46
43
47
from .rpc import RpcInvocationData
44
- from .data_stream import TextStreamWriter , FileStreamWriter
48
+ from .data_stream import TextStreamWriter , FileStreamWriter , STREAM_CHUNK_SIZE
45
49
46
50
47
51
class PublishTrackError (Exception ):
@@ -552,19 +556,21 @@ async def stream_text(
552
556
topic = topic ,
553
557
extensions = extensions ,
554
558
reply_to_id = reply_to_id ,
559
+ destination_identities = destination_identities ,
555
560
)
556
561
557
- await writer ._send_header (destination_identities = destination_identities )
562
+ await writer ._send_header ()
558
563
559
564
return writer
560
565
561
566
async def stream_file (
562
567
self ,
563
568
file_name : str ,
564
569
file_size : int | None = None ,
565
- mime_type : str = "" ,
570
+ mime_type : str = "application/octet-stream " ,
566
571
extensions : Dict [str , str ] = {},
567
572
stream_id : str | None = None ,
573
+ destination_identities : List [str ] = [],
568
574
):
569
575
writer = FileStreamWriter (
570
576
self ,
@@ -573,10 +579,42 @@ async def stream_file(
573
579
total_size = file_size ,
574
580
stream_id = stream_id ,
575
581
mime_type = mime_type ,
582
+ destination_identities = destination_identities ,
576
583
)
577
584
585
+ await writer ._send_header ()
586
+
578
587
return writer
579
588
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
+
580
618
async def publish_track (
581
619
self , track : LocalTrack , options : TrackPublishOptions = TrackPublishOptions ()
582
620
) -> LocalTrackPublication :
0 commit comments