fix: infer document type from mimetype

This commit is contained in:
əlemi 2025-02-02 01:25:12 +01:00
parent 44690b3637
commit 6f0026a818
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -272,11 +272,27 @@ impl AP {
if !matches!(t, apb::BaseType::Object(apb::ObjectType::Document(_))) {
return Err(NormalizerError::WrongType(apb::BaseType::Object(apb::ObjectType::Document(apb::DocumentType::Document)), t));
}
// if they gave us just url+mimetype, try detecting document type from mimetype
let mut document_type = document.document_type().ok();
if document_type.is_none() {
if let Ok(media_type) = document.media_type() {
if let Some((t, _mime)) = media_type.split_once('/') {
document_type = match t {
"audio" => Some(apb::DocumentType::Audio),
"image" => Some(apb::DocumentType::Image),
"video" => Some(apb::DocumentType::Video),
_ => None,
}
}
}
}
Ok(crate::model::attachment::Model {
internal: 0,
url: document.url().id().unwrap_or_default(),
object: parent,
document_type: document.as_document().map_or(apb::DocumentType::Document, |x| x.document_type().unwrap_or(apb::DocumentType::Page)),
document_type: document_type.unwrap_or(apb::DocumentType::Page),
name: document.name().ok(),
media_type: document.media_type().unwrap_or("link".to_string()),
})