Skip to content

Commit

Permalink
Display content type and size /inscription (ordinals#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Dec 16, 2022
1 parent c7a9c28 commit 46b3eb6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,21 @@ impl Inscription {
pub(crate) fn content(&self) -> Option<Content> {
let content = self.content.as_ref()?;

match self.content_type.as_ref()?.as_slice() {
b"text/plain;charset=utf-8" => Some(Content::Text(str::from_utf8(content).ok()?)),
b"image/png" => Some(Content::Png(content)),
match self.content_type()? {
"text/plain;charset=utf-8" => Some(Content::Text(str::from_utf8(content).ok()?)),
"image/png" => Some(Content::Png(content)),
_ => None,
}
}

pub(crate) fn content_type(&self) -> Option<&str> {
str::from_utf8(self.content_type.as_ref()?).ok()
}

pub(crate) fn content_size(&self) -> Option<usize> {
Some(self.content.as_ref()?.len())
}

pub(crate) fn content_html(&self) -> Trusted<ContentHtml> {
Trusted(ContentHtml(self.content()))
}
Expand Down
32 changes: 32 additions & 0 deletions src/subcommand/server/templates/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ mod tests {
"
<h1>Inscription ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc</h1>
<dl>
<dt>content size</dt>
<dd>10 bytes</dd>
<dt>content type</dt>
<dd>text/plain;charset=utf-8</dd>
<dt>location</dt>
<dd>1111111111111111111111111111111111111111111111111111111111111111:1:0</dd>
</dl>
Expand All @@ -53,6 +57,10 @@ mod tests {
"
<h1>Inscription ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc</h1>
<dl>
<dt>content size</dt>
<dd>100 bytes</dd>
<dt>content type</dt>
<dd>image/png</dd>
<dt>location</dt>
<dd>1111111111111111111111111111111111111111111111111111111111111111:1:0</dd>
</dl>
Expand All @@ -61,4 +69,28 @@ mod tests {
.unindent()
);
}

#[test]
fn empty_inscription() {
pretty_assert_eq!(
InscriptionHtml {
inscription_id: InscriptionId::from_str(
"ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc"
)
.unwrap(),
inscription: Inscription::new(None, None),
satpoint: satpoint(1, 0),
}
.to_string(),
"
<h1>Inscription ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc</h1>
<dl>
<dt>location</dt>
<dd>1111111111111111111111111111111111111111111111111111111111111111:1:0</dd>
</dl>
UNKNOWN
"
.unindent()
);
}
}
8 changes: 8 additions & 0 deletions templates/inscription.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<h1>Inscription {{ self.inscription_id }}</h1>
<dl>
%% if let Some(content_size) = self.inscription.content_size() {
<dt>content size</dt>
<dd>{{ content_size }} bytes</dd>
%% }
%% if let Some(content_type) = self.inscription.content_type() {
<dt>content type</dt>
<dd>{{ content_type }}</dd>
%% }
<dt>location</dt>
<dd>{{ self.satpoint }}</dd>
</dl>
Expand Down
12 changes: 12 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ fn inscription_page() {
&format!(
".*<h1>Inscription {reveal_tx}</h1>
<dl>
<dt>content size</dt>
<dd>10 bytes</dd>
<dt>content type</dt>
<dd>text/plain;charset=utf-8</dd>
<dt>location</dt>
<dd>{reveal_tx}:0:0</dd>
</dl>
Expand Down Expand Up @@ -116,6 +120,10 @@ fn inscription_page_after_send() {
&format!(
".*<h1>Inscription {reveal_txid}</h1>
<dl>
<dt>content size</dt>
<dd>10 bytes</dd>
<dt>content type</dt>
<dd>text/plain;charset=utf-8</dd>
<dt>location</dt>
<dd>{reveal_txid}:0:0</dd>
</dl>
Expand All @@ -139,6 +147,10 @@ HELLOWORLD.*",
&format!(
".*<h1>Inscription {reveal_txid}</h1>
<dl>
<dt>content size</dt>
<dd>10 bytes</dd>
<dt>content type</dt>
<dd>text/plain;charset=utf-8</dd>
<dt>location</dt>
<dd>{}:0:0</dd>
</dl>
Expand Down
8 changes: 8 additions & 0 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ fn send_works_on_signet() {
&format!(
".*<h1>Inscription {reveal_txid}</h1>
<dl>
<dt>content size</dt>
<dd>520 bytes</dd>
<dt>content type</dt>
<dd>image/png</dd>
<dt>location</dt>
<dd>{send_txid}:0:0</dd>
</dl>
Expand Down Expand Up @@ -148,6 +152,10 @@ fn send_inscribed_sat() {
&format!(
".*<h1>Inscription {reveal_txid}</h1>
<dl>
<dt>content size</dt>
<dd>520 bytes</dd>
<dt>content type</dt>
<dd>image/png</dd>
<dt>location</dt>
<dd>{send_txid}:0:0</dd>
</dl>
Expand Down

0 comments on commit 46b3eb6

Please sign in to comment.