diff --git a/src/ic-certified-assets/src/lib.rs b/src/ic-certified-assets/src/lib.rs index db55151fc..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, @@ -203,6 +209,18 @@ struct HttpRequest { body: ByteBuf, } +#[derive(Clone, Debug, CandidType, Deserialize)] +struct ChunkInfo { + chunk_id: ChunkId, + chunk_length: u64, +} + +#[derive(Clone, Debug, CandidType, Deserialize)] +struct ChunksInfoReponse { + total_length: u64, + chunks: Vec, +} + #[derive(Clone, Debug, CandidType, Deserialize)] struct HttpResponse { status_code: u16, @@ -402,6 +420,33 @@ fn get(arg: GetArg) -> EncodedAsset { }) } +#[query] +fn get_chunks_info(arg: GetChunksInfoArg) -> 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![], + }; + + 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 + }) +} + #[query] fn get_chunk(arg: GetChunkArg) -> GetChunkResponse { STATE.with(|s| {