Skip to content

Commit b3bcb2d

Browse files
committed
Replace MemoryStream to Stream
1 parent 0fc35a6 commit b3bcb2d

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

WebUntisAPI.Client/Models/Messages/Attachment.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public struct Attachment
4040
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
4141
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
4242
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
43-
public async Task<MemoryStream> DownloadContentAsStreamAsync(WebUntisClient client, int timeout = 2000, CancellationToken ct = default)
43+
public async Task<Stream> DownloadContentAsStreamAsync(WebUntisClient client, int timeout = 2000, CancellationToken ct = default)
4444
{
4545
string storageResponseString = await client.MakeAPIGetRequestAsync($"/WebUntis/api/rest/view/v1/messages/{_id}/attachmentstorageurl", ct);
4646

@@ -81,7 +81,7 @@ public async Task<MemoryStream> DownloadContentAsStreamAsync(WebUntisClient clie
8181
throw new UnauthorizedAccessException($"Invalid authentication. Detail: {detail}");
8282
}
8383

84-
return (MemoryStream)await response.Content.ReadAsStreamAsync();
84+
return await response.Content.ReadAsStreamAsync();
8585
}
8686
}
8787
}

WebUntisAPI.Client/WebUntisClient.Messages.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,16 @@ public async Task<MessagePreview[]> GetMessageInboxAsync(CancellationToken ct =
9393
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
9494
public async Task<MessagePreview> SendMessageAsync(Draft draft, MessagePerson[] recipients, int timeout = 2000, CancellationToken ct = default)
9595
{
96-
Tuple<string, MemoryStream>[] attachments = new Tuple<string, MemoryStream>[0];
96+
Tuple<string, Stream>[] attachments = new Tuple<string, Stream>[0];
9797
if (draft.Attachments.Count > 0)
9898
{
99-
Dictionary<string, Task<MemoryStream>> attachmentTasks = new Dictionary<string, Task<MemoryStream>>();
99+
Dictionary<string, Task<Stream>> attachmentTasks = new Dictionary<string, Task<Stream>>();
100100

101101
foreach (Attachment attachment in draft.Attachments)
102102
attachmentTasks.Add(attachment.Name, attachment.DownloadContentAsStreamAsync(this, timeout, ct));
103103

104104
await Task.WhenAll(attachmentTasks.Values);
105-
attachments = attachmentTasks.Select(attachment => new Tuple<string, MemoryStream>(attachment.Key, attachment.Value.Result)).ToArray();
105+
attachments = attachmentTasks.Select(attachment => new Tuple<string, Stream>(attachment.Key, attachment.Value.Result)).ToArray();
106106
}
107107

108108
return await SendMessageAsync(draft.Subject, draft.Content, recipients, draft.ForbidReply, attachments, ct);
@@ -121,7 +121,7 @@ public async Task<MessagePreview> SendMessageAsync(Draft draft, MessagePerson[]
121121
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
122122
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
123123
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
124-
public async Task<MessagePreview> SendMessageAsync(string subject, string content, MessagePerson[] recipients, bool forbidReply, Tuple<string, MemoryStream>[] attachments = null, CancellationToken ct = default)
124+
public async Task<MessagePreview> SendMessageAsync(string subject, string content, MessagePerson[] recipients, bool forbidReply, Tuple<string, Stream>[] attachments = null, CancellationToken ct = default)
125125
{
126126
// Check for disposing
127127
if (_disposedValue)
@@ -168,9 +168,12 @@ public async Task<MessagePreview> SendMessageAsync(string subject, string conten
168168
}
169169

170170
// Attachment part
171-
foreach (Tuple<string, MemoryStream> attachment in attachments)
171+
foreach (Tuple<string, Stream> attachment in attachments)
172172
{
173-
ByteArrayContent fileContent = new ByteArrayContent(attachment.Item2.ToArray());
173+
byte[] buffer = new byte[attachment.Item2.Length];
174+
int bytesRead = await attachment.Item2.ReadAsync(buffer, 0, buffer.Length);
175+
ByteArrayContent fileContent = new ByteArrayContent(buffer, 0, bytesRead);
176+
174177
fileContent.Headers.Add("Content-Type", "application/x-msdownload");
175178
requestContent.Add(fileContent, "attachments", attachment.Item1);
176179
}
@@ -309,7 +312,7 @@ public async Task<DraftPreview[]> GetSavedDraftsAsync(CancellationToken ct = def
309312
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
310313
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
311314
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
312-
public async Task<DraftPreview> CreateDraftAsync(string subject, string content, string recipientOption, bool forbidReply, bool copyToStudent, Tuple<string, MemoryStream>[] attachments = null, CancellationToken ct = default)
315+
public async Task<DraftPreview> CreateDraftAsync(string subject, string content, string recipientOption, bool forbidReply, bool copyToStudent, Tuple<string, Stream>[] attachments = null, CancellationToken ct = default)
313316
{
314317
// Check for disposing
315318
if (_disposedValue)
@@ -357,9 +360,12 @@ public async Task<DraftPreview> CreateDraftAsync(string subject, string content,
357360
}
358361

359362
// Attachment part
360-
foreach (Tuple<string, MemoryStream> attachment in attachments)
363+
foreach (Tuple<string, Stream> attachment in attachments)
361364
{
362-
ByteArrayContent fileContent = new ByteArrayContent(attachment.Item2.ToArray());
365+
byte[] buffer = new byte[attachment.Item2.Length];
366+
int bytesRead = await attachment.Item2.ReadAsync(buffer, 0, buffer.Length);
367+
ByteArrayContent fileContent = new ByteArrayContent(buffer, 0, bytesRead);
368+
363369
fileContent.Headers.Add("Content-Type", "application/x-msdownload");
364370
requestContent.Add(fileContent, "attachments", attachment.Item1);
365371
}
@@ -401,7 +407,7 @@ public async Task<DraftPreview> CreateDraftAsync(string subject, string content,
401407
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
402408
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
403409
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
404-
public async Task<DraftPreview> UpdateDraftAsync(Draft draft, Tuple<string, MemoryStream>[] newAttachments = null, Attachment[] attachmentToDelete = null, CancellationToken ct = default)
410+
public async Task<DraftPreview> UpdateDraftAsync(Draft draft, Tuple<string, Stream>[] newAttachments = null, Attachment[] attachmentToDelete = null, CancellationToken ct = default)
405411
{
406412
// Check for disposing
407413
if (_disposedValue)
@@ -454,9 +460,12 @@ public async Task<DraftPreview> UpdateDraftAsync(Draft draft, Tuple<string, Memo
454460
}
455461

456462
// Attachment part
457-
foreach (Tuple<string, MemoryStream> attachment in newAttachments ?? new Tuple<string, MemoryStream>[0])
463+
foreach (Tuple<string, Stream> attachment in newAttachments ?? new Tuple<string, Stream>[0])
458464
{
459-
ByteArrayContent fileContent = new ByteArrayContent(attachment.Item2.ToArray());
465+
byte[] buffer = new byte[attachment.Item2.Length];
466+
int bytesRead = await attachment.Item2.ReadAsync(buffer, 0, buffer.Length);
467+
ByteArrayContent fileContent = new ByteArrayContent(buffer, 0, bytesRead);
468+
460469
fileContent.Headers.Add("Content-Type", "application/x-msdownload");
461470
requestContent.Add(fileContent, "attachments", attachment.Item1);
462471
}

0 commit comments

Comments
 (0)