Skip to content

Commit

Permalink
Hide /content/<INSCRIPTION_ID> HTML inscriptions (#2778)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Nov 30, 2023
1 parent 78f10da commit 3085fd7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ impl Inscription {
}

pub(crate) fn hidden(&self) -> bool {
lazy_static! {
static ref CONTENT: Regex = Regex::new(r"^\s*/content/[[:xdigit:]]{64}i\d+\s*$").unwrap();
}

let Some(content_type) = self.content_type() else {
return true;
};
Expand All @@ -320,6 +324,16 @@ impl Inscription {
return true;
}

if content_type.starts_with("text/html")
&& self
.body()
.and_then(|body| str::from_utf8(body).ok())
.map(|body| CONTENT.is_match(body))
.unwrap_or_default()
{
return true;
}

false
}
}
Expand Down Expand Up @@ -796,6 +810,26 @@ mod tests {
case(Some("text/plain;charset=utf-8"), Some("foo"), true);
case(Some("text/plain;charset=cn-big5"), Some("foo"), true);
case(Some("application/json"), Some("foo"), true);
case(
Some("text/markdown"),
Some("/content/09a8d837ec0bcaec668ecf405e696a16bee5990863659c224ff888fb6f8f45e7i0"),
false,
);
case(
Some("text/html"),
Some("/content/09a8d837ec0bcaec668ecf405e696a16bee5990863659c224ff888fb6f8f45e7i0"),
true,
);
case(
Some("text/html;charset=utf-8"),
Some("/content/09a8d837ec0bcaec668ecf405e696a16bee5990863659c224ff888fb6f8f45e7i0"),
true,
);
case(
Some("text/html"),
Some(" /content/09a8d837ec0bcaec668ecf405e696a16bee5990863659c224ff888fb6f8f45e7i0 \n"),
true,
);

assert!(Inscription {
content_type: Some("text/plain".as_bytes().into()),
Expand Down

0 comments on commit 3085fd7

Please sign in to comment.