Ensure UploadFile is always a seekable stream.

This commit is contained in:
RogueException
2017-03-22 06:08:58 -03:00
parent 13b9b15cf0
commit 35d7a0cec8
2 changed files with 10 additions and 8 deletions

View File

@@ -494,13 +494,8 @@ namespace Discord.API
if (args.Content.GetValueOrDefault(null) == null) if (args.Content.GetValueOrDefault(null) == null)
args.Content = ""; args.Content = "";
else if (args.Content.IsSpecified) 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));
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));
}
var ids = new BucketIds(channelId: channelId); var ids = new BucketIds(channelId: channelId);
return await SendMultipartAsync<Message>("POST", () => $"channels/{channelId}/messages", args.ToDictionary(), ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); return await SendMultipartAsync<Message>("POST", () => $"channels/{channelId}/messages", args.ToDictionary(), ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);

View File

@@ -101,7 +101,14 @@ namespace Discord.Net.Rest
if (p.Value is MultipartFile) if (p.Value is MultipartFile)
{ {
var fileValue = (MultipartFile)p.Value; 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; continue;
} }