The Stream.Length accessor may throw a NotSupportedException if the Stream does not support seeking.
Callers should check CanSeek prior to accessing the Length property. See remarks from CanSeek docs.
One such Stream that does not support seeking is the response stream from SocketsHttpHandler.
This can be worked around by buffering the whole content in memory first, which clearly isn't ideal.
I believe this library should check CanSeek and either:
throw that non-seekable streams are not supported
buffer the content in order to obtain the length
skip the stream from the length calculation, potentially log a warning, and let the discord fail the request if the length is actually over the limit
Version
3.6.1
Working Version
Versions before #399
Logs
System.NotSupportedException: Specified method is not supported.
at System.Net.Http.HttpBaseStream.get_Length()
at Discord.Rest.ChannelHelper.<>c.<SendFilesAsync>b16_0(FileAttachment x)
at System.Linq.Enumerable.Sum[TSource](IEnumerable1 source, Func2 selector)
at Discord.Rest.ChannelHelper.SendFilesAsync(IMessageChannel channel, BaseDiscordClient client, IEnumerable`1 attachments, String text, Boolean isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, Embed[] embeds)
at Discord.Rest.ChannelHelper.SendFileAsync(IMessageChannel channel, BaseDiscordClient client, Stream stream, String filename, String text, Boolean isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, Boolean isSpoiler, Embed[] embeds)
Sample
using HttpResponseMessage response = await _http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
using Stream responseStream = await response.Content.ReadAsStreamAsync();
await channel.SendFileAsync(responseStream, "file.mp4");
Check The Docs
Verify Issue Source
Check your intents
Description
After #399, the
SendFilesAsync
helper will sum up theStream
lengths to determine if they fit intoMaxUploadLimit
.https://github.com/Discord-Net-Labs/Discord.Net-Labs/blob/fc475251acc2dcfb8714007889370d51f05ffadf/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs#L377
The
Stream.Length
accessor may throw aNotSupportedException
if theStream
does not support seeking. Callers should checkCanSeek
prior to accessing theLength
property. See remarks fromCanSeek
docs.One such
Stream
that does not support seeking is the response stream fromSocketsHttpHandler
. This can be worked around by buffering the whole content in memory first, which clearly isn't ideal.I believe this library should check
CanSeek
and either:Version
3.6.1
Working Version
Versions before #399
Logs
Sample