Browse Source

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
tags/2.0.0-beta
Alex Gravely RogueException 7 years ago
parent
commit
865080add9
2 changed files with 23 additions and 5 deletions
  1. +3
    -0
      src/Discord.Net.Rest/ClientHelper.cs
  2. +20
    -5
      src/Discord.Net.Rest/Net/Converters/ImageConverter.cs

+ 3
- 0
src/Discord.Net.Rest/ClientHelper.cs View File

@@ -120,6 +120,9 @@ namespace Discord.Rest
string name, IVoiceRegion region, Stream jpegIcon, RequestOptions options)
{
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);
return RestGuild.Create(client, model);
}


+ 20
- 5
src/Discord.Net.Rest/Net/Converters/ImageConverter.cs View File

@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using System;
using System;
using System.IO;
using Newtonsoft.Json;
using Model = Discord.API.Image;

namespace Discord.Net.Converters
@@ -23,10 +24,24 @@ namespace Discord.Net.Converters

if (image.Stream != null)
{
byte[] bytes = new byte[image.Stream.Length - image.Stream.Position];
image.Stream.Read(bytes, 0, bytes.Length);
byte[] bytes;
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}");
}
else if (image.Hash != null)


Loading…
Cancel
Save