Skip to content

Commit

Permalink
Render GLB/GLTF models in preview (#2369)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Aug 25, 2023
1 parent 5c09dd6 commit c910079
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl<'index> Updater<'_> {
// There's no try_iter on tokio::sync::mpsc::Receiver like std::sync::mpsc::Receiver.
// So we just loop until BATCH_SIZE doing try_recv until it returns None.
let mut outpoints = vec![outpoint];
for _ in 0..BATCH_SIZE-1 {
for _ in 0..BATCH_SIZE - 1 {
let Ok(outpoint) = outpoint_receiver.try_recv() else {
break;
};
Expand All @@ -296,7 +296,10 @@ impl<'index> Updater<'_> {
};
// Send all tx output values back in order
for (i, tx) in txs.iter().flatten().enumerate() {
let Ok(_) = value_sender.send(tx.output[usize::try_from(outpoints[i].vout).unwrap()].value).await else {
let Ok(_) = value_sender
.send(tx.output[usize::try_from(outpoints[i].vout).unwrap()].value)
.await
else {
log::error!("Value channel closed unexpectedly");
return;
};
Expand Down
4 changes: 3 additions & 1 deletion src/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ impl Inscription {
pub(crate) fn from_transaction(tx: &Transaction) -> Vec<TransactionInscription> {
let mut result = Vec::new();
for (index, tx_in) in tx.input.iter().enumerate() {
let Ok(inscriptions) = InscriptionParser::parse(&tx_in.witness) else { continue };
let Ok(inscriptions) = InscriptionParser::parse(&tx_in.witness) else {
continue;
};

result.extend(
inscriptions
Expand Down
4 changes: 3 additions & 1 deletion src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) enum Media {
Audio,
Iframe,
Image,
Model,
Pdf,
Text,
Unknown,
Expand All @@ -31,7 +32,8 @@ impl Media {
("image/png", Media::Image, &["png"]),
("image/svg+xml", Media::Iframe, &["svg"]),
("image/webp", Media::Image, &["webp"]),
("model/gltf-binary", Media::Unknown, &["glb"]),
("model/gltf+json", Media::Model, &["gltf"]),
("model/gltf-binary", Media::Model, &["glb"]),
("model/stl", Media::Unknown, &["stl"]),
("text/css", Media::Text, &["css"]),
("text/html", Media::Iframe, &[]),
Expand Down
14 changes: 12 additions & 2 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use {
crate::templates::{
BlockHtml, ClockSvg, HomeHtml, InputHtml, InscriptionHtml, InscriptionJson, InscriptionsHtml,
InscriptionsJson, OutputHtml, OutputJson, PageContent, PageHtml, PreviewAudioHtml,
PreviewImageHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml,
RangeHtml, RareTxt, SatHtml, SatJson, TransactionHtml,
PreviewImageHtml, PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml,
PreviewVideoHtml, RangeHtml, RareTxt, SatHtml, SatJson, TransactionHtml,
},
axum::{
body,
Expand Down Expand Up @@ -919,6 +919,16 @@ impl Server {
)
.into_response(),
),
Media::Model => Ok(
(
[(
header::CONTENT_SECURITY_POLICY,
"script-src-elem 'self' https://ajax.googleapis.com",
)],
PreviewModelHtml { inscription_id },
)
.into_response(),
),
Media::Pdf => Ok(
(
[(
Expand Down
4 changes: 2 additions & 2 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub(crate) use {
output::{OutputHtml, OutputJson},
page_config::PageConfig,
preview::{
PreviewAudioHtml, PreviewImageHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml,
PreviewVideoHtml,
PreviewAudioHtml, PreviewImageHtml, PreviewModelHtml, 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 @@ -10,6 +10,11 @@ pub(crate) struct PreviewImageHtml {
pub(crate) inscription_id: InscriptionId,
}

#[derive(boilerplate::Boilerplate)]
pub(crate) struct PreviewModelHtml {
pub(crate) inscription_id: InscriptionId,
}

#[derive(boilerplate::Boilerplate)]
pub(crate) struct PreviewPdfHtml {
pub(crate) inscription_id: InscriptionId,
Expand Down
17 changes: 17 additions & 0 deletions templates/preview-model.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<script type=module src=https://ajax.googleapis.com/ajax/libs/model-viewer/3.1.1/model-viewer.min.js></script>
<style>
model-viewer {
position: fixed;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<model-viewer src=/content/{{ self.inscription_id }} auto-rotate=true camera-controls=true shadow-intensity=1></model-viewer>
</body>
</html>
2 changes: 1 addition & 1 deletion tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn hash() {
#[test]
fn unrecognized_object() {
CommandBuilder::new("parse A")
.stderr_regex(r#"error: .*: unrecognized object\n.*"#)
.stderr_regex(r"error: .*: unrecognized object\n.*")
.expected_exit_code(2)
.run_and_extract_stdout();
}

0 comments on commit c910079

Please sign in to comment.