Skip to content

Commit c41ea1e

Browse files
committed
CLI: send images by SHA-256 alone and stop decoding them (sc-8999); CLI 0.2.19
Images go to POST /snapshots with sha256 and no fingerprint; the server fingerprints files it doesn't have. The group check hashes image entries by file SHA-256 and sends image_hash: sha256. PDFs keep their fingerprint, since their bytes change every run while the drawing doesn't. Needs #127 live in production first.
1 parent 24c4151 commit c41ea1e

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

‎crates/visualci-cli/src/client.rs‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn par_map<T: Sync, R: Send>(items: &[T], f: impl Fn(&T) -> Result<R> + Sync
5858
})
5959
}
6060

61-
/// Every snapshot's registration and bytes (`registration`), decoded and fingerprinted in parallel, in order.
61+
/// Every snapshot's registration and bytes (`registration`), hashed in parallel, in order.
6262
///
6363
/// # Errors
6464
/// The first snapshot (in order) whose file can't be read or classified.
@@ -274,6 +274,8 @@ impl Client {
274274
"manifest_hash": manifest::manifest_hash(entries),
275275
"group_depth": group_depth,
276276
"groups": groups,
277+
// Images' entries hold their files' hashes (`entry_of`), not pixel fingerprints.
278+
"image_hash": "sha256",
277279
});
278280
Ok(serde_json::from_value(self.post(&format!("/api/v1/builds/{build_id}/groups"), &body)?)?)
279281
}
@@ -407,7 +409,7 @@ pub fn registration(s: &Snapshot) -> Result<(serde_json::Value, Vec<u8>)> {
407409
None => std::fs::read(&s.path).with_context(|| format!("reading {}", s.path.display()))?,
408410
};
409411
let (kind, fingerprint, mut content_type) = if s.content.is_some() {
410-
("text", visualci_core::sha256_hex(&bytes), "text/plain")
412+
("text", Some(visualci_core::sha256_hex(&bytes)), "text/plain")
411413
} else {
412414
classify(&s.path, &bytes)?
413415
};
@@ -416,11 +418,10 @@ pub fn registration(s: &Snapshot) -> Result<(serde_json::Value, Vec<u8>)> {
416418
content_type =
417419
Format::detect(s.format.as_deref(), None, if s.content.is_some() { None } else { name }).content_type();
418420
}
419-
let params = json!({
421+
let mut params = json!({
420422
"name": s.name,
421423
"platform": s.platform,
422424
"kind": kind,
423-
"fingerprint": fingerprint,
424425
"sha256": visualci_core::sha256_hex(&bytes),
425426
"size": bytes.len(),
426427
"content_type": content_type,
@@ -429,6 +430,10 @@ pub fn registration(s: &Snapshot) -> Result<(serde_json::Value, Vec<u8>)> {
429430
"format": s.format,
430431
"ignore": s.ignore,
431432
});
433+
// An image goes by its file's hash alone: the server fingerprints the files it doesn't have (sc-8999).
434+
if let Some(fingerprint) = fingerprint {
435+
params["fingerprint"] = json!(fingerprint);
436+
}
432437
Ok((params, bytes))
433438
}
434439

@@ -453,7 +458,8 @@ pub fn entry_of(params: &serde_json::Value) -> Result<visualci_core::manifest::E
453458
name,
454459
platform: str_of("platform").unwrap_or_default(),
455460
kind,
456-
fingerprint: str_of("fingerprint").unwrap_or_default(),
461+
// An image's entry holds its file's hash, as `check_groups` tells the server (`image_hash`).
462+
fingerprint: str_of("fingerprint").or_else(|| str_of("sha256")).unwrap_or_default(),
457463
format,
458464
engine: str_of("engine"),
459465
threshold: params["threshold"].as_f64(),
@@ -462,11 +468,13 @@ pub fn entry_of(params: &serde_json::Value) -> Result<visualci_core::manifest::E
462468
})
463469
}
464470

465-
/// Kind, fingerprint and content type of a file.
466-
pub fn classify(path: &Path, bytes: &[u8]) -> Result<(&'static str, String, &'static str)> {
471+
/// Kind, fingerprint and content type of a file. An image has no fingerprint here: it isn't decoded, and the server
472+
/// fingerprints the images it hasn't seen (sc-8999). A PDF keeps its fingerprint, since its bytes change from run to
473+
/// run (dates, IDs) while what it draws doesn't, and the fingerprint saves the upload and the render.
474+
pub fn classify(path: &Path, bytes: &[u8]) -> Result<(&'static str, Option<String>, &'static str)> {
467475
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase();
468476
if bytes.starts_with(b"%PDF") {
469-
return Ok(("document", visualci_core::pdf::fingerprint_bytes(bytes).document, "application/pdf"));
477+
return Ok(("document", Some(visualci_core::pdf::fingerprint_bytes(bytes).document), "application/pdf"));
470478
}
471479
let image_type = match ext.as_str() {
472480
"png" => Some("image/png"),
@@ -475,12 +483,10 @@ pub fn classify(path: &Path, bytes: &[u8]) -> Result<(&'static str, String, &'st
475483
_ => None,
476484
};
477485
if let Some(content_type) = image_type {
478-
let fp = visualci_core::image_hash::fingerprint_bytes(bytes)
479-
.with_context(|| format!("decoding {}", path.display()))?;
480-
return Ok(("image", fp, content_type));
486+
return Ok(("image", None, content_type));
481487
}
482488
let name = path.file_name().and_then(|n| n.to_str());
483-
Ok(("text", visualci_core::sha256_hex(bytes), Format::detect(None, None, name).content_type()))
489+
Ok(("text", Some(visualci_core::sha256_hex(bytes)), Format::detect(None, None, name).content_type()))
484490
}
485491

486492
/// One snapshot split out of a snapshot file: its name, format, content and the file it came from.
@@ -794,8 +800,11 @@ mod tests {
794800
let sequential: Vec<_> = snaps.iter().map(|s| registration(s).unwrap()).collect();
795801
let parallel = registrations(&snaps).unwrap();
796802
assert_eq!(parallel, sequential);
797-
// Distinct images got distinct fingerprints, so the order really was checked.
798-
assert_ne!(parallel[0].0["fingerprint"], parallel[1].0["fingerprint"]);
803+
// Distinct images got distinct hashes, so the order really was checked.
804+
assert_ne!(parallel[0].0["sha256"], parallel[1].0["sha256"]);
805+
// Images go by their hash alone, undecoded; texts keep their fingerprint.
806+
assert!(parallel[0].0.get("fingerprint").is_none(), "{}", parallel[0].0);
807+
assert!(parallel.iter().any(|(p, _)| p["kind"] == "text" && p["fingerprint"].is_string()));
799808

800809
// The first failing snapshot's error, as the sequential loop gave.
801810
snaps.insert(5, snap("missing-a", dir.join("missing-a.png"), None));

0 commit comments

Comments
 (0)