Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SVG and HTML inscriptions #1035

Merged
merged 11 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/content.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[derive(Debug, PartialEq)]
pub(crate) enum Content<'a> {
Text(&'a str),
IFrame,
Image,
Text(&'a str),
}
6 changes: 4 additions & 2 deletions src/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ impl Inscription {
let content = self.content.as_ref()?;

match self.content_type()? {
"text/plain;charset=utf-8" => Some(Content::Text(str::from_utf8(content).ok()?)),
content_type::HTML | content_type::SVG => Some(Content::IFrame),
content_type::TEXT => Some(Content::Text(str::from_utf8(content).ok()?)),
content_type if content_type::is_image(content_type) => Some(Content::Image),
_ => None,
}
Expand All @@ -115,7 +116,7 @@ impl Inscription {
}

pub(crate) fn is_graphical(&self) -> bool {
matches!(self.content(), Some(Content::Image))
matches!(self.content(), Some(Content::Image) | Some(Content::IFrame))
}
}

Expand Down Expand Up @@ -712,6 +713,7 @@ mod tests {
assert!(inscription("image/png", []).is_graphical());
assert!(!inscription("foo", []).is_graphical());
assert!(inscription("image/gif", []).is_graphical());
assert!(inscription("image/svg+xml", []).is_graphical());
assert!(!Inscription::new(None, Some(Vec::new())).is_graphical());
}
}
10 changes: 8 additions & 2 deletions src/inscription/content_type.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use super::*;

pub const HTML: &str = "text/html;charset=utf-8";
pub const SVG: &str = "image/svg+xml";
pub const TEXT: &str = "text/plain;charset=utf-8";

const TABLE: &[(&str, bool, &[&str])] = &[
("image/apng", true, &["apng"]),
("image/gif", true, &["gif"]),
("image/jpeg", true, &["jpg", "jpeg"]),
("image/png", true, &["png"]),
("image/webp", true, &["webp"]),
("text/plain;charset=utf-8", false, &["txt"]),
(HTML, false, &["html"]),
(SVG, true, &["svg"]),
(TEXT, false, &["txt"]),
];

lazy_static! {
Expand Down Expand Up @@ -52,7 +58,7 @@ mod tests {
assert_eq!(super::for_extension("jpeg").unwrap(), "image/jpeg");
assert_eq!(
super::for_extension("foo").unwrap_err().to_string(),
"unsupported file extension `.foo`, supported extensions: apng gif jpg png webp txt"
"unsupported file extension `.foo`, supported extensions: apng gif jpg png webp html svg txt"
);
}
}
14 changes: 13 additions & 1 deletion src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,19 @@ impl Server {
ServerError::NotFound(format!("inscription {inscription_id} has no content"))
})?;

Ok(([(header::CONTENT_TYPE, content_type)], content).into_response())
Ok(
(
[
(header::CONTENT_TYPE, content_type),
(
header::CONTENT_SECURITY_POLICY,
"default-src 'none' 'unsafe-eval' 'unsafe-inline'".to_string(),
),
],
content,
)
.into_response(),
)
}

fn content_response(inscription: Inscription) -> Option<(String, Vec<u8>)> {
Expand Down
44 changes: 44 additions & 0 deletions src/subcommand/server/templates/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,51 @@ impl<'a> Display for ContentHtml<'a> {
write!(f, "</pre>")
}
Some(Content::Image) => write!(f, "<img src=/content/{}>", self.inscription_id),
Some(Content::IFrame) => {
write!(f, "<iframe src=/content/{}></iframe>", self.inscription_id)
}
None => write!(f, "<p>UNKNOWN</p>"),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn text() {
assert_eq!(
ContentHtml {
content: Some(Content::Text("foo")),
inscription_id: txid(1),
}
.to_string(),
"<pre>foo</pre>"
);
}

#[test]
fn image() {
assert_eq!(
ContentHtml {
content: Some(Content::Image),
inscription_id: txid(1),
}
.to_string(),
"<img src=/content/1111111111111111111111111111111111111111111111111111111111111111>"
);
}

#[test]
fn iframe() {
assert_eq!(
ContentHtml {
content: Some(Content::IFrame),
inscription_id: txid(1),
}
.to_string(),
"<iframe src=/content/1111111111111111111111111111111111111111111111111111111111111111></iframe>"
);
}
}
16 changes: 16 additions & 0 deletions static/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ a.mythic {
image-rendering: pixelated;
}

.inscriptions a iframe {
width: 100%;
height: 100%;
}

.content {
display: flex;
justify-content: center;
Expand All @@ -188,6 +193,17 @@ a.mythic {
image-rendering: pixelated;
}

iframe {
border: none;
pointer-events: none;
}

.content iframe {
aspect-ratio: 1 / 1;
border: none;
min-width: 50%;
}

a.content {
color: inherit;
text-decoration: none;
Expand Down
4 changes: 4 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ fn inscription_content() {
response.headers().get("content-type").unwrap(),
"text/plain;charset=utf-8"
);
assert_eq!(
response.headers().get("content-security-policy").unwrap(),
"default-src 'none' 'unsafe-eval' 'unsafe-inline'"
);
assert_eq!(response.bytes().unwrap(), "HELLOWORLD");
}

Expand Down