Skip to content

Commit b2e32dd

Browse files
committed
feat: check if stream matches input format when loading jumbf
1 parent 19f1a3e commit b2e32dd

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

sdk/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ pub enum Error {
228228
#[error("thumbnail format {0} is unsupported")]
229229
UnsupportedThumbnailFormat(String),
230230

231+
#[error("the specified stream is not of format {0}")]
232+
IncorrectFormat(String),
233+
231234
#[error("`trust.signer_info` is missing from settings")]
232235
MissingSignerSettings,
233236

sdk/src/jumbf_io.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,34 @@ pub(crate) fn is_bmff_format(asset_type: &str) -> bool {
100100
bmff_io.supported_types().contains(&asset_type)
101101
}
102102

103+
/// Checks whether the stream matches the specified asset type, or otherwise returns
104+
/// [Error::IncorrectFormat].
105+
pub fn check_stream_supported(asset_type: &str, stream: &mut dyn CAIRead) -> Result<()> {
106+
match get_assetio_handler(asset_type) {
107+
Some(asset_handler) => {
108+
if !asset_handler.supports_stream(stream)? {
109+
Err(Error::IncorrectFormat(asset_type.to_owned()))
110+
} else {
111+
Ok(())
112+
}
113+
}
114+
None => Err(Error::IncorrectFormat(asset_type.to_owned())),
115+
}
116+
}
117+
103118
/// Return jumbf block from in memory asset
104119
#[allow(dead_code)]
105120
pub fn load_jumbf_from_memory(asset_type: &str, data: &[u8]) -> Result<Vec<u8>> {
106121
let mut buf_reader = Cursor::new(data);
107122

123+
check_stream_supported(asset_type, &mut buf_reader)?;
108124
load_jumbf_from_stream(asset_type, &mut buf_reader)
109125
}
110126

111127
/// Return jumbf block from stream asset
112128
pub fn load_jumbf_from_stream(asset_type: &str, input_stream: &mut dyn CAIRead) -> Result<Vec<u8>> {
129+
check_stream_supported(asset_type, input_stream)?;
130+
113131
let cai_block = match get_cailoader_handler(asset_type) {
114132
Some(asset_handler) => asset_handler.read_cai(input_stream)?,
115133
None => return Err(Error::UnsupportedType),
@@ -127,6 +145,8 @@ pub fn save_jumbf_to_stream(
127145
output_stream: &mut dyn CAIReadWrite,
128146
store_bytes: &[u8],
129147
) -> Result<()> {
148+
check_stream_supported(asset_type, input_stream)?;
149+
130150
match get_caiwriter_handler(asset_type) {
131151
Some(asset_handler) => asset_handler.write_cai(input_stream, output_stream, store_bytes),
132152
None => Err(Error::UnsupportedType),
@@ -136,6 +156,8 @@ pub fn save_jumbf_to_stream(
136156
/// writes the jumbf data in store_bytes into an asset in data and returns the newly created asset
137157
pub fn save_jumbf_to_memory(asset_type: &str, data: &[u8], store_bytes: &[u8]) -> Result<Vec<u8>> {
138158
let mut input_stream = Cursor::new(data);
159+
check_stream_supported(asset_type, &mut input_stream)?;
160+
139161
let output_vec: Vec<u8> = Vec::with_capacity(data.len() + store_bytes.len() + 1024);
140162
let mut output_stream = Cursor::new(output_vec);
141163

sdk/src/store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5797,11 +5797,11 @@ pub mod tests {
57975797
&mut report,
57985798
&Settings::default(),
57995799
);
5800-
assert!(matches!(result, Err(Error::UnsupportedType)));
5800+
assert!(matches!(result, Err(Error::IncorrectFormat(_))));
58015801
println!("Error report: {report:?}");
58025802
assert!(!report.logged_items().is_empty());
58035803

5804-
assert!(report.has_error(Error::UnsupportedType));
5804+
assert!(report.has_error(Error::IncorrectFormat(format.to_owned())));
58055805
}
58065806

58075807
#[test]

0 commit comments

Comments
 (0)