Fix CreateGuildAsync not sending icon stream. (#768)

* Fix CreateGuildAsync not doing anything with the input stream for the guild icon.

Also fixes an issue with potential stream types that throw a NotSupportedException when checking its properties. [Apparently, they exist.](https://github.com/dotnet/corefx/blob/master/src/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpResponseStream.cs)

* Merged with old method

* Removed duplicate decl
This commit is contained in:
Alex Gravely
2017-08-17 02:19:16 -04:00
committed by RogueException
parent 506a6c96c9
commit 865080add9
2 changed files with 23 additions and 5 deletions

View File

@@ -120,6 +120,9 @@ namespace Discord.Rest
string name, IVoiceRegion region, Stream jpegIcon, RequestOptions options) string name, IVoiceRegion region, Stream jpegIcon, RequestOptions options)
{ {
var args = new CreateGuildParams(name, region.Id); var args = new CreateGuildParams(name, region.Id);
if (jpegIcon != null)
args.Icon = new API.Image(jpegIcon);
var model = await client.ApiClient.CreateGuildAsync(args, options).ConfigureAwait(false); var model = await client.ApiClient.CreateGuildAsync(args, options).ConfigureAwait(false);
return RestGuild.Create(client, model); return RestGuild.Create(client, model);
} }

View File

@@ -1,5 +1,6 @@
using Newtonsoft.Json; using System;
using System; using System.IO;
using Newtonsoft.Json;
using Model = Discord.API.Image; using Model = Discord.API.Image;
namespace Discord.Net.Converters namespace Discord.Net.Converters
@@ -23,10 +24,24 @@ namespace Discord.Net.Converters
if (image.Stream != null) if (image.Stream != null)
{ {
byte[] bytes = new byte[image.Stream.Length - image.Stream.Position]; byte[] bytes;
image.Stream.Read(bytes, 0, bytes.Length); int length;
if (image.Stream.CanSeek)
{
bytes = new byte[image.Stream.Length - image.Stream.Position];
length = image.Stream.Read(bytes, 0, bytes.Length);
}
else
{
var cloneStream = new MemoryStream();
image.Stream.CopyTo(cloneStream);
bytes = new byte[cloneStream.Length];
cloneStream.Position = 0;
cloneStream.Read(bytes, 0, bytes.Length);
length = (int)cloneStream.Length;
}
string base64 = Convert.ToBase64String(bytes); string base64 = Convert.ToBase64String(bytes, 0, length);
writer.WriteValue($"data:image/jpeg;base64,{base64}"); writer.WriteValue($"data:image/jpeg;base64,{base64}");
} }
else if (image.Hash != null) else if (image.Hash != null)