diff --git a/db/src/stores/fs/mod.rs b/db/src/stores/fs/mod.rs index 4ce787f..d479646 100644 --- a/db/src/stores/fs/mod.rs +++ b/db/src/stores/fs/mod.rs @@ -495,17 +495,35 @@ impl FsStore { connection.insert_entry(mime_entry)?; } - let label_entry = Entry { - entity: blob_address.clone(), - attribute: ATTR_LABEL.parse().unwrap(), - value: name - .unwrap_or_else(|| filename.as_os_str().to_string_lossy().to_string()) - .into(), - provenance: "SYSTEM INIT".to_string(), - timestamp: chrono::Utc::now().naive_utc(), - user: context.user.clone(), + let label = { + if let Some(name) = name { + Some(name) + } else { + let hash = b58_encode(blob_address.encode()?); + let filename = filename.as_os_str().to_string_lossy().to_string(); + if filename.starts_with(&hash) { + let rest = filename.split_at(hash.len()).1; + let rest = rest.trim_start_matches(|ch| !char::is_alphanumeric(ch)); + (!rest.is_empty()).then_some(rest.to_string()) + } else { + Some(filename) + } + } }; - let label_entry_addr = connection.insert_entry(label_entry)?; + + let mut label_entry_addr = None; + + if let Some(label) = label { + let label_entry = Entry { + entity: blob_address.clone(), + attribute: ATTR_LABEL.parse().unwrap(), + value: label.into(), + provenance: "SYSTEM INIT".to_string(), + timestamp: chrono::Utc::now().naive_utc(), + user: context.user.clone(), + }; + label_entry_addr = Some(connection.insert_entry(label_entry)?); + } if let Some(upath) = upath { let resolved_path = match resolve_cache { @@ -526,15 +544,17 @@ impl FsStore { }; let dir_has_entry_addr = connection.insert_entry(dir_has_entry)?; - let alias_entry = Entry { - entity: dir_has_entry_addr, - attribute: ATTR_BY.parse().unwrap(), - value: label_entry_addr.into(), - provenance: "SYSTEM INIT".to_string(), - timestamp: chrono::Utc::now().naive_utc(), - user: context.user.clone(), - }; - connection.insert_entry(alias_entry)?; + if let Some(label_entry_addr) = label_entry_addr { + let alias_entry = Entry { + entity: dir_has_entry_addr, + attribute: ATTR_BY.parse().unwrap(), + value: label_entry_addr.into(), + provenance: "SYSTEM INIT".to_string(), + timestamp: chrono::Utc::now().naive_utc(), + user: context.user.clone(), + }; + connection.insert_entry(alias_entry)?; + } } Ok(blob_address)