@@ -34,15 +34,16 @@ public struct Attachment
3434 /// <summary>
3535 /// Get the content of the attachment as stream with a progress report
3636 /// </summary>
37- /// <param name="client">The client with them you got this instane (It doesn't have to be the same client but the same account)</param>
38- /// <param name="timeout">The time after that the download of the content will cancelled (In miliseconds)</param>
37+ /// <param name="client">The client with them you got this instance (It doesn't have to be the same client but the same account)</param>
38+ /// <param name="targetStream">The stream to write the download to</param>
39+ /// <param name="timeout">The time after that the download of the content will cancelled (In milliseconds)</param>
3940 /// <param name="progress">The progress of the download in percent</param>
4041 /// <param name="ct">Cancellation token</param>
4142 /// <returns>The attachment as stream</returns>
4243 /// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
4344 /// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
4445 /// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
45- public async Task < Stream > DownloadContentAsStreamAsync ( WebUntisClient client , TimeSpan timeout , IProgress < double > progress = null , CancellationToken ct = default )
46+ public async Task DownloadContentAsStreamAsync ( WebUntisClient client , Stream targetStream , TimeSpan timeout , IProgress < double > progress = null , CancellationToken ct = default )
4647 {
4748 string storageResponseString = await client . MakeAPIGetRequestAsync ( $ "/WebUntis/api/rest/view/v1/messages/{ Id } /attachmentstorageurl", ct ) ;
4849
@@ -69,41 +70,33 @@ public async Task<Stream> DownloadContentAsStreamAsync(WebUntisClient client, Ti
6970 downloadClient . Timeout = timeout ;
7071 HttpResponseMessage response = await downloadClient . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , ct ) ;
7172
72- using ( MemoryStream content = new MemoryStream ( ) )
73+ using ( Stream contentStream = await response . Content . ReadAsStreamAsync ( ) )
7374 {
74- using ( Stream contentStream = await response . Content . ReadAsStreamAsync ( ) )
75- {
76- int totalBytes = ( int ? ) response . Content . Headers . ContentLength ?? - 1 ;
77- int totalRecievedBytes = 0 ;
75+ int totalBytes = ( int ? ) response . Content . Headers . ContentLength ?? - 1 ;
76+ int totalReceivedBytes = 0 ;
7877
79- int bytesRead = 0 ;
80- byte [ ] buffer = new byte [ 8192 ] ;
81- while ( ( bytesRead = await contentStream . ReadAsync ( buffer , 0 , buffer . Length , ct ) ) > 0 )
82- {
83- if ( ct . IsCancellationRequested )
84- return default ;
78+ int bytesRead = 0 ;
79+ byte [ ] buffer = new byte [ 4096 ] ;
80+ while ( ( bytesRead = await contentStream . ReadAsync ( buffer , 0 , buffer . Length , ct ) ) > 0 )
81+ {
82+ if ( ct . IsCancellationRequested )
83+ return ;
8584
86- content . Write ( buffer , 0 , bytesRead ) ;
87- totalRecievedBytes += bytesRead ;
88- progress ? . Report ( ( double ) totalRecievedBytes / totalBytes * 100d ) ;
89- }
85+ targetStream . Write ( buffer , 0 , bytesRead ) ;
86+ totalReceivedBytes += bytesRead ;
87+ progress ? . Report ( ( double ) totalReceivedBytes / totalBytes * 100d ) ;
9088 }
89+ }
9190
92- if ( ct . IsCancellationRequested )
93- return default ;
91+ if ( ct . IsCancellationRequested )
92+ return ;
9493
95- // Verify response
96- if ( response . StatusCode == HttpStatusCode . Unauthorized || response . StatusCode == HttpStatusCode . Forbidden )
97- {
98- string detail = Regex . Match ( Encoding . UTF8 . GetString ( content . ToArray ( ) ) , @"<Message>([a-zA-z0-9\s]+)</Message>" ) . Groups [ 1 ] . Value ; // Get the error message
99- throw new UnauthorizedAccessException ( $ "Invalid authentication. Detail: { detail } ") ;
100- }
101-
102- if ( response . StatusCode != HttpStatusCode . OK )
103- throw new HttpRequestException ( $ "There was an error while the http request (Code: { response . StatusCode } ).") ;
94+ // Verify response
95+ if ( response . StatusCode == HttpStatusCode . Unauthorized || response . StatusCode == HttpStatusCode . Forbidden )
96+ throw new UnauthorizedAccessException ( $ "Invalid authentication.") ;
10497
105- return content ;
106- }
98+ if ( response . StatusCode != HttpStatusCode . OK )
99+ throw new HttpRequestException ( $ "There was an error while the http request (Code: { response . StatusCode } )." ) ;
107100 }
108101 }
109102 }
0 commit comments