From e42d09f37fa280997dd844b50e96394f32056776 Mon Sep 17 00:00:00 2001 From: 3cL1p5e7 <3cL1p5e7@gmail.com> Date: Tue, 15 Feb 2022 19:44:11 +0300 Subject: [PATCH 1/5] added get_chunks_info query --- src/ic-certified-assets/src/lib.rs | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/ic-certified-assets/src/lib.rs b/src/ic-certified-assets/src/lib.rs index db55151fc..9035943f4 100644 --- a/src/ic-certified-assets/src/lib.rs +++ b/src/ic-certified-assets/src/lib.rs @@ -203,6 +203,18 @@ struct HttpRequest { body: ByteBuf, } +#[derive(Clone, Debug, CandidType, Deserialize)] +struct ChunkInfo { + chunk_id: ChunkId, + total_length: usize, +} + +#[derive(Clone, Debug, CandidType, Deserialize)] +struct ChunksInfoReponse { + total_length: usize, + chunks: Vec, +} + #[derive(Clone, Debug, CandidType, Deserialize)] struct HttpResponse { status_code: u16, @@ -402,6 +414,35 @@ fn get(arg: GetArg) -> EncodedAsset { }) } +#[query] +fn get_chunks_info(arg: GetArg) -> ChunksInfoReponse { + STATE.with(|s| { + let assets = s.assets.borrow(); + let asset = assets.get(&arg.key).unwrap_or_else(|| { + trap("asset not found"); + }); + + let mut result = ChunksInfoReponse { + total_length: 0, + chunks: vec![], + }; + + + for enc in arg.accept_encodings.iter() { + if let Some(asset_enc) = asset.encodings.get(enc) { + for (i, chunk) in asset_enc.content_chunks.iter().enumerate() { + result.total_length = result.total_length + asset_enc.total_length; + result.chunks.push(ChunkInfo { + chunk_id: Nat::from(i), + total_length: chunk.len(), + }); + } + } + } + result + }) +} + #[query] fn get_chunk(arg: GetChunkArg) -> GetChunkResponse { STATE.with(|s| { From f30ca493477b842afd4042b7584a7ef8fccc9276 Mon Sep 17 00:00:00 2001 From: 3cL1p5e7 <3cL1p5e7@gmail.com> Date: Tue, 15 Feb 2022 19:58:04 +0300 Subject: [PATCH 2/5] fix total_length type --- src/ic-certified-assets/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ic-certified-assets/src/lib.rs b/src/ic-certified-assets/src/lib.rs index 9035943f4..2d4fefb27 100644 --- a/src/ic-certified-assets/src/lib.rs +++ b/src/ic-certified-assets/src/lib.rs @@ -206,12 +206,12 @@ struct HttpRequest { #[derive(Clone, Debug, CandidType, Deserialize)] struct ChunkInfo { chunk_id: ChunkId, - total_length: usize, + total_length: Nat, } #[derive(Clone, Debug, CandidType, Deserialize)] struct ChunksInfoReponse { - total_length: usize, + total_length: Nat, chunks: Vec, } @@ -423,7 +423,7 @@ fn get_chunks_info(arg: GetArg) -> ChunksInfoReponse { }); let mut result = ChunksInfoReponse { - total_length: 0, + total_length: Nat::from(0), chunks: vec![], }; @@ -434,7 +434,7 @@ fn get_chunks_info(arg: GetArg) -> ChunksInfoReponse { result.total_length = result.total_length + asset_enc.total_length; result.chunks.push(ChunkInfo { chunk_id: Nat::from(i), - total_length: chunk.len(), + total_length: Nat::from(chunk.len()), }); } } From cc525f55b73a3e64aac8013296aaa95780050e53 Mon Sep 17 00:00:00 2001 From: 3cL1p5e7 <3cL1p5e7@gmail.com> Date: Wed, 16 Feb 2022 03:00:15 +0300 Subject: [PATCH 3/5] fixes by review: total_length as u64 and ChunkInfo.total_length -> chunk_length --- src/ic-certified-assets/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ic-certified-assets/src/lib.rs b/src/ic-certified-assets/src/lib.rs index 2d4fefb27..d1e504e17 100644 --- a/src/ic-certified-assets/src/lib.rs +++ b/src/ic-certified-assets/src/lib.rs @@ -206,12 +206,12 @@ struct HttpRequest { #[derive(Clone, Debug, CandidType, Deserialize)] struct ChunkInfo { chunk_id: ChunkId, - total_length: Nat, + chunk_length: u64, } #[derive(Clone, Debug, CandidType, Deserialize)] struct ChunksInfoReponse { - total_length: Nat, + total_length: u64, chunks: Vec, } @@ -423,7 +423,7 @@ fn get_chunks_info(arg: GetArg) -> ChunksInfoReponse { }); let mut result = ChunksInfoReponse { - total_length: Nat::from(0), + total_length: 0, chunks: vec![], }; @@ -431,10 +431,10 @@ fn get_chunks_info(arg: GetArg) -> ChunksInfoReponse { for enc in arg.accept_encodings.iter() { if let Some(asset_enc) = asset.encodings.get(enc) { for (i, chunk) in asset_enc.content_chunks.iter().enumerate() { - result.total_length = result.total_length + asset_enc.total_length; + result.total_length = result.total_length + (asset_enc.total_length as u64); result.chunks.push(ChunkInfo { chunk_id: Nat::from(i), - total_length: Nat::from(chunk.len()), + chunk_length: chunk.len() as u64, }); } } From 9f4c2180c0ae8a652e8da9e357d49660395a9735 Mon Sep 17 00:00:00 2001 From: 3cL1p5e7 <3cL1p5e7@gmail.com> Date: Thu, 17 Feb 2022 22:43:40 +0300 Subject: [PATCH 4/5] certified_assets: fix lint and fmt warnings --- src/ic-certified-assets/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ic-certified-assets/src/lib.rs b/src/ic-certified-assets/src/lib.rs index d1e504e17..f6f757a0f 100644 --- a/src/ic-certified-assets/src/lib.rs +++ b/src/ic-certified-assets/src/lib.rs @@ -427,11 +427,10 @@ fn get_chunks_info(arg: GetArg) -> ChunksInfoReponse { chunks: vec![], }; - for enc in arg.accept_encodings.iter() { if let Some(asset_enc) = asset.encodings.get(enc) { for (i, chunk) in asset_enc.content_chunks.iter().enumerate() { - result.total_length = result.total_length + (asset_enc.total_length as u64); + result.total_length += asset_enc.total_length as u64; result.chunks.push(ChunkInfo { chunk_id: Nat::from(i), chunk_length: chunk.len() as u64, From 99c28f28a93461510c5f5384d9591791c12c16c3 Mon Sep 17 00:00:00 2001 From: 3cL1p5e7 <3cL1p5e7@gmail.com> Date: Tue, 22 Feb 2022 16:20:13 +0300 Subject: [PATCH 5/5] ic-certified-assets: fix get_chunk_info encodings argument --- src/ic-certified-assets/src/lib.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/ic-certified-assets/src/lib.rs b/src/ic-certified-assets/src/lib.rs index f6f757a0f..7f583d817 100644 --- a/src/ic-certified-assets/src/lib.rs +++ b/src/ic-certified-assets/src/lib.rs @@ -163,6 +163,12 @@ struct GetArg { accept_encodings: Vec, } +#[derive(Clone, Debug, CandidType, Deserialize)] +struct GetChunksInfoArg { + key: Key, + content_encoding: String, +} + #[derive(Clone, Debug, CandidType, Deserialize)] struct GetChunkArg { key: Key, @@ -415,7 +421,7 @@ fn get(arg: GetArg) -> EncodedAsset { } #[query] -fn get_chunks_info(arg: GetArg) -> ChunksInfoReponse { +fn get_chunks_info(arg: GetChunksInfoArg) -> ChunksInfoReponse { STATE.with(|s| { let assets = s.assets.borrow(); let asset = assets.get(&arg.key).unwrap_or_else(|| { @@ -427,15 +433,14 @@ fn get_chunks_info(arg: GetArg) -> ChunksInfoReponse { chunks: vec![], }; - for enc in arg.accept_encodings.iter() { - if let Some(asset_enc) = asset.encodings.get(enc) { - for (i, chunk) in asset_enc.content_chunks.iter().enumerate() { - result.total_length += asset_enc.total_length as u64; - result.chunks.push(ChunkInfo { - chunk_id: Nat::from(i), - chunk_length: chunk.len() as u64, - }); - } + if let Some(asset_enc) = asset.encodings.get(&arg.content_encoding) { + for (i, chunk) in asset_enc.content_chunks.iter().enumerate() { + let chunk_length = chunk.len() as u64; + result.total_length += chunk_length; + result.chunks.push(ChunkInfo { + chunk_id: Nat::from(i), + chunk_length, + }); } } result