@contentauth/c2pa-node 0.5.4 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -184,21 +184,27 @@ fn check_strength(strength: f32) -> Result<()> {
184
184
  Ok(())
185
185
  }
186
186
 
187
+ /// Minimum expected size for a valid ONNX model file (10 MB).
188
+ const MIN_MODEL_SIZE: usize = 10 * 1024 * 1024;
189
+
190
+ /// Maximum number of download attempts per model file.
191
+ const MAX_RETRIES: u32 = 3;
192
+
187
193
  // Taken from Trustmark xtask.
188
194
  // Will not overwrite models if they already exist.
189
195
  pub fn fetch_model(variant: Variant, dir_path: &std::path::Path) -> Result<PathBuf> {
190
196
  let rt = runtime();
191
197
  let client = Client::builder()
192
- .timeout(Duration::from_secs(10))
193
- .connect_timeout(Duration::from_secs(5))
194
- .read_timeout(Duration::from_secs(5))
198
+ .timeout(Duration::from_secs(120))
199
+ .connect_timeout(Duration::from_secs(10))
200
+ .read_timeout(Duration::from_secs(60))
195
201
  .build()
196
202
  .map_err(|e| Error::ModelDownload(format!("Failed to build HTTP client: {e}")))?;
197
203
 
198
204
  // Ensure directory exists
199
205
  std::fs::create_dir_all(dir_path)?;
200
206
 
201
- let root = "https://cc-assets.netlify.app/watermarking/trustmark-models";
207
+ let root = "https://cai-watermark.adobe.net/watermarking/trustmark-models";
202
208
 
203
209
  // Construct filenames manually since encoder_filename/decoder_filename are private
204
210
  let encoder_filename = format!("encoder_{variant}.onnx");
@@ -210,32 +216,70 @@ pub fn fetch_model(variant: Variant, dir_path: &std::path::Path) -> Result<PathB
210
216
  continue;
211
217
  }
212
218
  let model_url = format!("{root}/{filename}");
213
- let model_bytes = rt.block_on(async {
214
- let response = client
215
- .get(&model_url)
216
- .send()
217
- .await
218
- .map_err(|e| Error::ModelDownload(format!("Failed to GET model: {e}")))?;
219
- response
220
- .bytes()
221
- .await
222
- .map_err(|e| Error::ModelDownload(format!("Failed to read model bytes: {e}")))
223
- .map(|b| b.to_vec())
224
- })?;
225
- let mut file = OpenOptions::new()
226
- .write(true)
227
- .create_new(true)
228
- .open(&model_path)
229
- .map_err(|e| {
230
- Error::ModelDownload(format!(
231
- "Created {dir_path:?} but failed to open model directory{model_path:?}: {e}"
232
- ))
233
- })?;
234
- file.write_all(&model_bytes).map_err(|e| {
235
- Error::ModelDownload(format!(
236
- "Failed to write model to model directory {model_path:?}: {e}"
237
- ))
238
- })?;
219
+
220
+ let mut last_err = None;
221
+ for attempt in 1..=MAX_RETRIES {
222
+ match download_model(&rt, &client, &model_url) {
223
+ Ok(model_bytes) => {
224
+ if model_bytes.len() < MIN_MODEL_SIZE {
225
+ last_err = Some(Error::ModelDownload(format!(
226
+ "Downloaded model {filename} is too small ({} bytes), expected at least {MIN_MODEL_SIZE} bytes",
227
+ model_bytes.len()
228
+ )));
229
+ continue;
230
+ }
231
+
232
+ // Write to a temporary file first, then rename for atomicity.
233
+ let tmp_path = dir_path.join(format!("{filename}.tmp"));
234
+ let mut file = OpenOptions::new()
235
+ .write(true)
236
+ .create(true)
237
+ .truncate(true)
238
+ .open(&tmp_path)
239
+ .map_err(|e| {
240
+ Error::ModelDownload(format!(
241
+ "Failed to create temp file {tmp_path:?}: {e}"
242
+ ))
243
+ })?;
244
+ file.write_all(&model_bytes).map_err(|e| {
245
+ Error::ModelDownload(format!("Failed to write model to {tmp_path:?}: {e}"))
246
+ })?;
247
+ std::fs::rename(&tmp_path, &model_path).map_err(|e| {
248
+ Error::ModelDownload(format!(
249
+ "Failed to rename {tmp_path:?} to {model_path:?}: {e}"
250
+ ))
251
+ })?;
252
+
253
+ last_err = None;
254
+ break;
255
+ }
256
+ Err(e) => {
257
+ last_err = Some(e);
258
+ if attempt < MAX_RETRIES {
259
+ std::thread::sleep(Duration::from_secs(u64::from(attempt)));
260
+ }
261
+ }
262
+ }
263
+ }
264
+
265
+ if let Some(err) = last_err {
266
+ return Err(err);
267
+ }
239
268
  }
240
269
  Ok(dir_path.to_path_buf())
241
270
  }
271
+
272
+ fn download_model(rt: &tokio::runtime::Runtime, client: &Client, url: &str) -> Result<Vec<u8>> {
273
+ rt.block_on(async {
274
+ let response = client
275
+ .get(url)
276
+ .send()
277
+ .await
278
+ .map_err(|e| Error::ModelDownload(format!("Failed to GET model: {e}")))?;
279
+ response
280
+ .bytes()
281
+ .await
282
+ .map_err(|e| Error::ModelDownload(format!("Failed to read model bytes: {e}")))
283
+ .map(|b| b.to_vec())
284
+ })
285
+ }
package/src/utils.rs CHANGED
@@ -46,25 +46,27 @@ pub fn parse_settings(
46
46
  Some(js_value) => {
47
47
  if js_value.is_a::<JsString, _>(cx) {
48
48
  let settings_string = js_value
49
- .downcast::<JsString, _>(cx).map_err(|_| Error::Signing(format!("{}: Expected settings string", error_prefix)))?
49
+ .downcast::<JsString, _>(cx)
50
+ .map_err(|_| {
51
+ Error::Signing(format!("{error_prefix}: Expected settings string"))
52
+ })?
50
53
  .value(cx);
51
54
 
52
55
  // Create context with settings
53
56
  let context = Context::new()
54
57
  .with_settings(settings_string.as_str())
55
- .map_err(|e| Error::Signing(format!("{}: Invalid settings: {}", error_prefix, e)))?;
56
-
58
+ .map_err(|e| {
59
+ Error::Signing(format!("{error_prefix}: Invalid settings: {e}"))
60
+ })?;
57
61
  Ok(Some(context))
58
62
  } else if js_value.is_a::<JsNull, _>(cx) || js_value.is_a::<JsUndefined, _>(cx) {
59
63
  Ok(None)
60
64
  } else {
61
65
  Err(Error::Signing(format!(
62
- "{}: Settings must be a string, null, or undefined",
63
- error_prefix
66
+ "{error_prefix}: Settings must be a string, null, or undefined",
64
67
  )))
65
68
  }
66
69
  }
67
70
  None => Ok(None),
68
71
  }
69
72
  }
70
-