Skip to content

Commit

Permalink
Read storage as byte if passed, for Unity Texture (#34)
Browse files Browse the repository at this point in the history
* Read storage as byte if passed, for Unity Texture

* Update Thirdweb.Storage.Tests.cs
  • Loading branch information
0xFirekeeper authored Jun 15, 2024
1 parent 30f0393 commit 7b684e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Thirdweb.Tests/Thirdweb.Storage.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public async Task DownloadTest_ThirdwebIPFS()
Assert.NotNull(res);
}

[Fact]
public async Task DownloadTest_Bytes()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var res = await ThirdwebStorage.Download<byte[]>(client, "https://1.rpc.thirdweb.com/providers");
Assert.NotNull(res);
Assert.NotEmpty(res);
}

[Fact]
public async Task DownloadTest_400()
{
Expand Down
16 changes: 13 additions & 3 deletions Thirdweb/Thirdweb.Storage/ThirdwebStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,19 @@ public static async Task<T> Download<T>(ThirdwebClient client, string uri, int?
throw new Exception($"Failed to download {uri}: {response.StatusCode} | {await response.Content.ReadAsStringAsync()}");
}

var content = await response.Content.ReadAsStringAsync();

return typeof(T) == typeof(string) ? (T)(object)content : JsonConvert.DeserializeObject<T>(content);
if (typeof(T) == typeof(byte[]))
{
return (T)(object)await response.Content.ReadAsByteArrayAsync();
}
else if (typeof(T) == typeof(string))
{
return (T)(object)await response.Content.ReadAsStringAsync();
}
else
{
var content = await response.Content.ReadAsByteArrayAsync();
return JsonConvert.DeserializeObject<T>(System.Text.Encoding.UTF8.GetString(content));
}
}

public static async Task<IPFSUploadResult> Upload(ThirdwebClient client, string path)
Expand Down

0 comments on commit 7b684e5

Please sign in to comment.