Skip to content

Commit 749a3e2

Browse files
authored
Adding AcceptEncodings override (#127)
* Adding AcceptEncodings override * Fixing docs
1 parent 86af424 commit 749a3e2

3 files changed

Lines changed: 48 additions & 44 deletions

File tree

samples/Sample.CLI/Program.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using EdjCase.ICP.Candid.Utilities;
2020
using EdjCase.ICP.BLS.Models;
2121
using System.Diagnostics;
22+
using System.IO.Compression;
2223

2324
public class Program
2425
{
@@ -162,7 +163,41 @@ string outputFilePath
162163
AssetCanisterApiClient client = new(this.agent, canisterId);
163164

164165
Console.WriteLine($"Downloading asset '{key}'...");
165-
byte[] assetBytes = await client.DownloadAssetAsync(key);
166+
(byte[] assetBytes, string contentEncoding) = await client.DownloadAssetAsync(key);
167+
switch (contentEncoding)
168+
{
169+
case "identity":
170+
break;
171+
case "gzip":
172+
using (var memoryStream = new MemoryStream(assetBytes))
173+
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
174+
using (var decompressedStream = new MemoryStream())
175+
{
176+
gzipStream.CopyTo(decompressedStream);
177+
assetBytes = decompressedStream.ToArray();
178+
}
179+
break;
180+
case "deflate":
181+
using (var memoryStream = new MemoryStream(assetBytes))
182+
using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
183+
using (var decompressedStream = new MemoryStream())
184+
{
185+
deflateStream.CopyTo(decompressedStream);
186+
assetBytes = decompressedStream.ToArray();
187+
}
188+
break;
189+
case "br":
190+
using (var memoryStream = new MemoryStream(assetBytes))
191+
using (var brotliStream = new BrotliStream(memoryStream, CompressionMode.Decompress))
192+
using (var decompressedStream = new MemoryStream())
193+
{
194+
brotliStream.CopyTo(decompressedStream);
195+
assetBytes = decompressedStream.ToArray();
196+
}
197+
break;
198+
default:
199+
throw new NotImplementedException($"Content encoding {contentEncoding} is not implemented");
200+
}
166201
File.WriteAllBytes(outputFilePath, assetBytes);
167202
Console.WriteLine($"Downloaded asset '{key}' to {outputFilePath}");
168203
}

src/Agent/API.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Agent/Standards/AssetCanister/AssetCanisterApiClient.cs

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,15 @@ public async Task UploadAssetChunkedAsync(
187187
/// </summary>
188188
/// <param name="key">The key of the asset to download.</param>
189189
/// <param name="maxConcurrency">The maximum number of concurrent chunk downloads.</param>
190+
/// <param name="acceptEncodings">The encodings to accept for the asset in order. Defaults to ["gzip", "deflate", "br", "identity"]</param>
190191
/// <returns>The downloaded asset content as a byte array.</returns>
191-
public async Task<byte[]> DownloadAssetAsync(string key, int maxConcurrency = 10)
192+
public async Task<(byte[] Asset, string ContentEncoding)> DownloadAssetAsync(
193+
string key,
194+
int maxConcurrency = 10,
195+
List<string>? acceptEncodings = null
196+
)
192197
{
193-
List<string> acceptEncodings = new() { "identity", "gzip", "deflate", "br" };
198+
acceptEncodings ??= new() { "gzip", "deflate", "br", "identity" };
194199
GetResult result = await this.GetAsync(key, acceptEncodings);
195200

196201
if (!result.TotalLength.TryToUInt64(out ulong totalLength))
@@ -199,12 +204,12 @@ public async Task<byte[]> DownloadAssetAsync(string key, int maxConcurrency = 10
199204
}
200205
if (totalLength == (ulong)result.Content.Length)
201206
{
202-
return result.Content;
207+
return (result.Content, result.ContentEncoding);
203208
}
204209
int chunkCount = (int)Math.Ceiling((double)totalLength / result.Content.Length);
205210

206211
// Create a list to store the chunk tasks
207-
List<Task<byte[]>> chunkTasks = new List<Task<byte[]>>();
212+
List<Task<byte[]>> chunkTasks = new();
208213

209214
// Create a semaphore to limit the number of concurrent tasks
210215
SemaphoreSlim semaphore = new(maxConcurrency);
@@ -236,44 +241,7 @@ public async Task<byte[]> DownloadAssetAsync(string key, int maxConcurrency = 10
236241
// Combine all the bytes into one byte[]
237242
byte[] combinedBytes = result.Content.Concat(chunkTasks.SelectMany(t => t.Result)).ToArray();
238243

239-
switch (result.ContentEncoding)
240-
{
241-
case "identity":
242-
case null:
243-
case "":
244-
break;
245-
case "gzip":
246-
using (var memoryStream = new MemoryStream(combinedBytes))
247-
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
248-
using (var decompressedStream = new MemoryStream())
249-
{
250-
gzipStream.CopyTo(decompressedStream);
251-
combinedBytes = decompressedStream.ToArray();
252-
}
253-
break;
254-
case "deflate":
255-
using (var memoryStream = new MemoryStream(combinedBytes))
256-
using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
257-
using (var decompressedStream = new MemoryStream())
258-
{
259-
deflateStream.CopyTo(decompressedStream);
260-
combinedBytes = decompressedStream.ToArray();
261-
}
262-
break;
263-
case "br":
264-
using (var memoryStream = new MemoryStream(combinedBytes))
265-
using (var brotliStream = new BrotliStream(memoryStream, CompressionMode.Decompress))
266-
using (var decompressedStream = new MemoryStream())
267-
{
268-
brotliStream.CopyTo(decompressedStream);
269-
combinedBytes = decompressedStream.ToArray();
270-
}
271-
break;
272-
default:
273-
throw new NotImplementedException($"Content encoding {result.ContentEncoding} is not supported");
274-
}
275-
276-
return combinedBytes;
244+
return (combinedBytes, result.ContentEncoding);
277245
}
278246

279247
/// <summary>

0 commit comments

Comments
 (0)