Skip to content

Commit

Permalink
Support video inscriptions (#1349)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jan 24, 2023
1 parent c29bee8 commit 5751a22
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(crate) enum Media {
Pdf,
Text,
Unknown,
Video,
}

impl Media {
Expand Down Expand Up @@ -61,6 +62,7 @@ const TABLE: &[(&str, Media, &[&str])] = &[
("image/webp", Media::Image, &["webp"]),
("text/html;charset=utf-8", Media::Iframe, &["html"]),
("text/plain;charset=utf-8", Media::Text, &["txt"]),
("video/webm", Media::Video, &["webm"]),
];

#[cfg(test)]
Expand Down
35 changes: 28 additions & 7 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
crate::templates::{
BlockHtml, ClockSvg, HomeHtml, InputHtml, InscriptionHtml, InscriptionsHtml, OutputHtml,
PageContent, PageHtml, PreviewAudioHtml, PreviewImageHtml, PreviewPdfHtml, PreviewTextHtml,
PreviewUnknownHtml, RangeHtml, RareTxt, SatHtml, TransactionHtml,
PreviewUnknownHtml, PreviewVideoHtml, RangeHtml, RareTxt, SatHtml, TransactionHtml,
},
axum::{
body,
Expand Down Expand Up @@ -744,6 +744,11 @@ impl Server {

return match inscription.media() {
Media::Audio => Ok(PreviewAudioHtml { inscription_id }.into_response()),
Media::Iframe => Ok(
Self::content_response(inscription)
.ok_or_not_found(|| format!("inscription {inscription_id} content"))?
.into_response(),
),
Media::Image => Ok(
(
[(
Expand All @@ -754,11 +759,6 @@ impl Server {
)
.into_response(),
),
Media::Iframe => Ok(
Self::content_response(inscription)
.ok_or_not_found(|| format!("inscription {inscription_id} content"))?
.into_response(),
),
Media::Pdf => Ok(
(
[(
Expand All @@ -773,7 +773,6 @@ impl Server {
let content = inscription
.body()
.ok_or_not_found(|| format!("inscription {inscription_id} content"))?;

Ok(
PreviewTextHtml {
text: str::from_utf8(content)
Expand All @@ -783,6 +782,7 @@ impl Server {
)
}
Media::Unknown => Ok(PreviewUnknownHtml.into_response()),
Media::Video => Ok(PreviewVideoHtml { inscription_id }.into_response()),
};
}

Expand Down Expand Up @@ -2098,6 +2098,27 @@ mod tests {
);
}

#[test]
fn video_preview() {
let server = TestServer::new();
server.mine_blocks(1);

let txid = server.bitcoin_rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0)],
witness: inscription("video/webm", "hello").to_witness(),
..Default::default()
});
let inscription_id = InscriptionId::from(txid);

server.mine_blocks(1);

server.assert_response_regex(
format!("/preview/{inscription_id}"),
StatusCode::OK,
format!(r".*<video .*>\s*<source src=/content/{inscription_id}>.*"),
);
}

#[test]
fn inscription_page_title() {
let server = TestServer::new_with_sat_index();
Expand Down
1 change: 1 addition & 0 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) use {
output::OutputHtml,
preview::{
PreviewAudioHtml, PreviewImageHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml,
PreviewVideoHtml,
},
range::RangeHtml,
rare::RareTxt,
Expand Down
5 changes: 5 additions & 0 deletions src/templates/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ pub(crate) struct PreviewTextHtml<'a> {

#[derive(boilerplate::Boilerplate)]
pub(crate) struct PreviewUnknownHtml;

#[derive(boilerplate::Boilerplate)]
pub(crate) struct PreviewVideoHtml {
pub(crate) inscription_id: InscriptionId,
}
16 changes: 16 additions & 0 deletions static/preview-video.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
html {
background-color: #131516;
height: 100%;
}

body {
align-items: center;
display: flex;
height: 100%;
margin: 0;
}

video {
height: 100%;
width: 100%;
}
12 changes: 12 additions & 0 deletions templates/preview-video.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<link rel=stylesheet href=/static/preview-video.css>
</head>
<body>
<video controls loop>
<source src=/content/{{self.inscription_id}}>
</video>
</body>
</html>

0 comments on commit 5751a22

Please sign in to comment.