Browse Source

Ensure UploadFile is always a seekable stream.

tags/1.0-rc
RogueException 8 years ago
parent
commit
35d7a0cec8
2 changed files with 10 additions and 8 deletions
  1. +2
    -7
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  2. +8
    -1
      src/Discord.Net.Rest/Net/DefaultRestClient.cs

+ 2
- 7
src/Discord.Net.Rest/DiscordRestApiClient.cs View File

@@ -494,13 +494,8 @@ namespace Discord.API

if (args.Content.GetValueOrDefault(null) == null)
args.Content = "";
else if (args.Content.IsSpecified)
{
if (args.Content.Value == null)
args.Content = "";
if (args.Content.Value?.Length > DiscordConfig.MaxMessageSize)
throw new ArgumentOutOfRangeException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content));
}
else if (args.Content.IsSpecified && args.Content.Value?.Length > DiscordConfig.MaxMessageSize)
throw new ArgumentOutOfRangeException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content));

var ids = new BucketIds(channelId: channelId);
return await SendMultipartAsync<Message>("POST", () => $"channels/{channelId}/messages", args.ToDictionary(), ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);


+ 8
- 1
src/Discord.Net.Rest/Net/DefaultRestClient.cs View File

@@ -101,7 +101,14 @@ namespace Discord.Net.Rest
if (p.Value is MultipartFile)
{
var fileValue = (MultipartFile)p.Value;
content.Add(new StreamContent(fileValue.Stream), p.Key, fileValue.Filename);
var stream = fileValue.Stream;
if (!stream.CanSeek)
{
var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
stream = memoryStream;
}
content.Add(new StreamContent(stream), p.Key, fileValue.Filename);
continue;
}



Loading…
Cancel
Save