diff --git a/upub/core/src/config.rs b/upub/core/src/config.rs index 5e4800b..6788909 100644 --- a/upub/core/src/config.rs +++ b/upub/core/src/config.rs @@ -12,6 +12,9 @@ pub struct Config { #[serde(default)] pub security: SecurityConfig, + #[serde(default)] + pub compat: CompatibilityConfig, + // TODO should i move app keys here? } @@ -96,6 +99,13 @@ pub struct SecurityConfig { pub reinsertion_attempt_limit: u32, } +#[serde_inline_default::serde_inline_default] +#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, serde_default::DefaultFromSerde)] +pub struct CompatibilityConfig { + #[serde(default)] + pub fix_attachment_images_media_type: bool, + +} impl Config { pub fn load(path: Option<&std::path::PathBuf>) -> Self { diff --git a/upub/core/src/traits/normalize.rs b/upub/core/src/traits/normalize.rs index e214230..e275150 100644 --- a/upub/core/src/traits/normalize.rs +++ b/upub/core/src/traits/normalize.rs @@ -80,14 +80,6 @@ impl Normalizer for crate::Context { tracing::warn!("ignoring array-in-array while processing attachments"); continue }, - Node::Link(l) => crate::model::attachment::ActiveModel { - internal: sea_orm::ActiveValue::NotSet, - url: Set(self.cloaked(l.href().unwrap_or_default())), - object: Set(object_model.internal), - document_type: Set(apb::DocumentType::Page), - name: Set(l.name().str()), - media_type: Set(l.media_type().unwrap_or("link").to_string()), - }, Node::Object(o) => { let mut model = AP::attachment_q(o.as_document()?, object_model.internal, None)?; if let Set(u) | Unchanged(u) = model.url { @@ -95,6 +87,28 @@ impl Normalizer for crate::Context { } model }, + Node::Link(l) => { + let url = l.href().unwrap_or_default(); + let mut media_type = l.media_type().unwrap_or("link").to_string(); + let mut document_type = apb::DocumentType::Page; + if self.cfg().compat.fix_attachment_images_media_type + && [".jpg", ".jpeg", ".png", ".webp", ".bmp"] // TODO more image types??? + .iter() + .any(|x| url.ends_with(x)) + { + document_type = apb::DocumentType::Image; + media_type = format!("image/{}", url.split('.').last().unwrap_or_default()); + + } + crate::model::attachment::ActiveModel { + internal: sea_orm::ActiveValue::NotSet, + url: Set(self.cloaked(url)), + object: Set(object_model.internal), + document_type: Set(document_type), + name: Set(l.name().str()), + media_type: Set(media_type), + } + }, }; crate::model::attachment::Entity::insert(attachment_model) .exec(tx)