-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Storage download upload * Update dotnet-ci.yml
- Loading branch information
1 parent
8d643e7
commit 8fb4114
Showing
11 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace Thirdweb.Tests; | ||
|
||
public class StorageTests : BaseTests | ||
{ | ||
public StorageTests(ITestOutputHelper output) | ||
: base(output) { } | ||
|
||
[Fact] | ||
public async Task DownloadTest_SecretKey() | ||
{ | ||
var client = new ThirdwebClient(secretKey: _secretKey); | ||
var res = await ThirdwebStorage.Download<string>(client, "https://1.rpc.thirdweb.com/providers"); | ||
Assert.NotNull(res); | ||
} | ||
|
||
[Fact] | ||
public async Task DownloadTest_Client_BundleId() | ||
{ | ||
var client = new ThirdwebClient(clientId: _clientIdBundleIdOnly, bundleId: _bundleIdBundleIdOnly); | ||
var res = await ThirdwebStorage.Download<string>(client, "https://1.rpc.thirdweb.com/providers"); | ||
Assert.NotNull(res); | ||
} | ||
|
||
[Fact] | ||
public async Task UploadTest_SecretKey() | ||
{ | ||
var client = new ThirdwebClient(secretKey: _secretKey); | ||
var path = Path.Combine(Path.GetTempPath(), "testJson.json"); | ||
File.WriteAllText(path, "{\"test\": \"test\"}"); | ||
var res = await ThirdwebStorage.Upload(client, path); | ||
Assert.StartsWith($"https://{client.ClientId}.ipfscdn.io/ipfs/", res.PreviewUrl); | ||
} | ||
|
||
[Fact] | ||
public async Task UploadTest_Client_BundleId() | ||
{ | ||
var client = new ThirdwebClient(clientId: _clientIdBundleIdOnly, bundleId: _bundleIdBundleIdOnly); | ||
var path = Path.Combine(Path.GetTempPath(), "testJson.json"); | ||
File.WriteAllText(path, "{\"test\": \"test\"}"); | ||
var res = await ThirdwebStorage.Upload(client, path); | ||
Assert.StartsWith($"https://{client.ClientId}.ipfscdn.io/ipfs/", res.PreviewUrl); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Thirdweb | ||
{ | ||
[System.Serializable] | ||
public struct IPFSUploadResult | ||
{ | ||
public string IpfsHash; | ||
public string PinSize; | ||
public string Timestamp; | ||
public string PreviewUrl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Thirdweb | ||
{ | ||
public class ThirdwebStorage | ||
{ | ||
public static async Task<T> Download<T>(ThirdwebClient client, string uri, int? requestTimeout = null) | ||
{ | ||
if (string.IsNullOrEmpty(uri)) | ||
{ | ||
throw new ArgumentNullException(nameof(uri)); | ||
} | ||
|
||
uri = uri.ReplaceIPFS(string.IsNullOrEmpty(client.ClientId) ? Constants.FALLBACK_IPFS_GATEWAY : $"https://{client.ClientId}.ipfscdn.io/ipfs/"); | ||
|
||
using var httpClient = new HttpClient(); | ||
|
||
var isThirdwebRequest = new Uri(uri).Host.EndsWith(".ipfscdn.io"); | ||
if (isThirdwebRequest) | ||
{ | ||
var headers = Utils.GetThirdwebHeaders(client); | ||
foreach (var header in headers) | ||
{ | ||
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); | ||
} | ||
} | ||
|
||
requestTimeout ??= client.FetchTimeoutOptions.GetTimeout(TimeoutType.Storage); | ||
|
||
var response = await httpClient.GetAsync(uri, new CancellationTokenSource(requestTimeout.Value).Token); | ||
|
||
if (!response.IsSuccessStatusCode) | ||
{ | ||
throw new Exception($"Failed to download {uri}: {response.StatusCode} | {response.ReasonPhrase} | {await response.Content.ReadAsStringAsync()}"); | ||
} | ||
|
||
var content = await response.Content.ReadAsStringAsync(); | ||
|
||
return typeof(T) == typeof(string) ? (T)(object)content : JsonConvert.DeserializeObject<T>(content); | ||
} | ||
|
||
public static async Task<IPFSUploadResult> Upload(ThirdwebClient client, string path) | ||
{ | ||
if (string.IsNullOrEmpty(client.ClientId)) | ||
{ | ||
throw new UnauthorizedAccessException("You cannot use Upload features without setting a Client ID."); | ||
} | ||
|
||
if (string.IsNullOrEmpty(path)) | ||
{ | ||
throw new ArgumentNullException(nameof(path)); | ||
} | ||
|
||
using var httpClient = new HttpClient(); | ||
using var form = new MultipartFormDataContent { { new ByteArrayContent(File.ReadAllBytes(path)), "file", Path.GetFileName(path) } }; | ||
|
||
var headers = Utils.GetThirdwebHeaders(client); | ||
foreach (var header in headers) | ||
{ | ||
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); | ||
} | ||
|
||
var response = await httpClient.PostAsync(Constants.PIN_URI, form); | ||
|
||
if (!response.IsSuccessStatusCode) | ||
{ | ||
throw new Exception($"Failed to upload {path}: {response.StatusCode} | {response.ReasonPhrase} | {await response.Content.ReadAsStringAsync()}"); | ||
} | ||
|
||
var result = await response.Content.ReadAsStringAsync(); | ||
|
||
var res = JsonConvert.DeserializeObject<IPFSUploadResult>(result); | ||
res.PreviewUrl = $"https://{client.ClientId}.ipfscdn.io/ipfs/{res.IpfsHash}"; | ||
return res; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters