(
image_contexts: Vec<ImageContextData>,
)
| 1256 | } |
| 1257 | |
| 1258 | fn resolve_missing_image_payloads( |
| 1259 | image_contexts: Vec<ImageContextData>, |
| 1260 | ) -> Result<Vec<ImageContextData>, String> { |
| 1261 | let mut resolved = Vec::with_capacity(image_contexts.len()); |
| 1262 | |
| 1263 | for mut image in image_contexts { |
| 1264 | let missing_payload = |
| 1265 | is_blank_text(image.image_path.as_ref()) && is_blank_text(image.data_url.as_ref()); |
| 1266 | if !missing_payload { |
| 1267 | resolved.push(image); |
| 1268 | continue; |
| 1269 | } |
| 1270 | |
| 1271 | let stored = get_image_context(&image.id).ok_or_else(|| { |
| 1272 | format!( |
| 1273 | "Image context not found for image_id={}. It may have expired. Please re-attach the image and retry.", |
| 1274 | image.id |
| 1275 | ) |
| 1276 | })?; |
| 1277 | |
| 1278 | if is_blank_text(image.image_path.as_ref()) { |
| 1279 | image.image_path = stored |
| 1280 | .image_path |
| 1281 | .clone() |
| 1282 | .filter(|s: &String| !s.trim().is_empty()); |
| 1283 | } |
| 1284 | if is_blank_text(image.data_url.as_ref()) { |
| 1285 | image.data_url = stored |
| 1286 | .data_url |
| 1287 | .clone() |
| 1288 | .filter(|s: &String| !s.trim().is_empty()); |
| 1289 | } |
| 1290 | if image.mime_type.trim().is_empty() { |
| 1291 | image.mime_type = stored.mime_type.clone(); |
| 1292 | } |
| 1293 | |
| 1294 | let mut metadata = image |
| 1295 | .metadata |
| 1296 | .take() |
| 1297 | .unwrap_or_else(|| serde_json::json!({})); |
| 1298 | if !metadata.is_object() { |
| 1299 | metadata = serde_json::json!({ "raw_metadata": metadata }); |
| 1300 | } |
| 1301 | if let Some(obj) = metadata.as_object_mut() { |
| 1302 | if !obj.contains_key("name") { |
| 1303 | obj.insert("name".to_string(), serde_json::json!(stored.image_name)); |
| 1304 | } |
| 1305 | if !obj.contains_key("width") { |
| 1306 | obj.insert("width".to_string(), serde_json::json!(stored.width)); |
| 1307 | } |
| 1308 | if !obj.contains_key("height") { |
| 1309 | obj.insert("height".to_string(), serde_json::json!(stored.height)); |
| 1310 | } |
| 1311 | if !obj.contains_key("file_size") { |
| 1312 | obj.insert("file_size".to_string(), serde_json::json!(stored.file_size)); |
| 1313 | } |
| 1314 | if !obj.contains_key("source") { |
| 1315 | obj.insert("source".to_string(), serde_json::json!(stored.source)); |
no test coverage detected