generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gateway): HTML preview for dag-cbor and dag-json (#315)
* feat(gateway): preview dag-cbor/-json with links * feat: use hex.Dump (and <pre>) for bytes type * fix: redirects around codecs * fix: correctly check if ETags match
- Loading branch information
Showing
16 changed files
with
457 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package assets | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/ipld/go-ipld-prime/datamodel" | ||
cidlink "github.com/ipld/go-ipld-prime/linking/cid" | ||
) | ||
|
||
type ParsedNode struct { | ||
Keys []*ParsedNode | ||
Values []*ParsedNode | ||
Value string | ||
CID string | ||
Long bool | ||
} | ||
|
||
func ParseNode(node datamodel.Node) (*ParsedNode, error) { | ||
dag := &ParsedNode{} | ||
|
||
switch node.Kind() { | ||
case datamodel.Kind_Map: | ||
it := node.MapIterator() | ||
|
||
for !it.Done() { | ||
k, v, err := it.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
kd, err := ParseNode(k) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vd, err := ParseNode(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dag.Keys = append(dag.Keys, kd) | ||
dag.Values = append(dag.Values, vd) | ||
} | ||
case datamodel.Kind_List: | ||
it := node.ListIterator() | ||
for !it.Done() { | ||
k, v, err := it.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vd, err := ParseNode(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dag.Keys = append(dag.Keys, &ParsedNode{Value: fmt.Sprintf("%d", k)}) | ||
dag.Values = append(dag.Values, vd) | ||
} | ||
case datamodel.Kind_Bool: | ||
v, err := node.AsBool() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Value = fmt.Sprintf("%t", v) | ||
case datamodel.Kind_Int: | ||
v, err := node.AsInt() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Value = fmt.Sprintf("%d", v) | ||
case datamodel.Kind_Float: | ||
v, err := node.AsFloat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Value = fmt.Sprintf("%f", v) | ||
case datamodel.Kind_String: | ||
v, err := node.AsString() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Value = v | ||
case datamodel.Kind_Bytes: | ||
v, err := node.AsBytes() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Long = true | ||
dag.Value = hex.Dump(v) | ||
case datamodel.Kind_Link: | ||
lnk, err := node.AsLink() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Value = lnk.String() | ||
|
||
cl, isCid := lnk.(cidlink.Link) | ||
if isCid { | ||
dag.CID = cl.Cid.String() | ||
} | ||
case datamodel.Kind_Invalid: | ||
dag.Value = "INVALID" | ||
case datamodel.Kind_Null: | ||
dag.Value = "NULL" | ||
default: | ||
dag.Value = "UNKNOWN" | ||
} | ||
|
||
return dag, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+742 Bytes
gateway/assets/test/dag/bafyreiagdtlc3xwhbeywzpwmxvwkogcujhlsm6f4cfdgpjpyu77gkubro4.block
Binary file not shown.
Binary file added
BIN
+664 Bytes
gateway/assets/test/dag/bafyreiaocls5bt2ha5vszv5pwz34zzcdf3axk3uqa56bgsgvlkbezw67hq.block
Binary file not shown.
Binary file added
BIN
+238 Bytes
gateway/assets/test/dag/bafyreicnokmhmrnlp2wjhyk2haep4tqxiptwfrp2rrs7rzq7uk766chqvq.block
Binary file not shown.
Binary file added
BIN
+332 Bytes
gateway/assets/test/dag/bafyreihnpl7ami7esahkfdnemm6idx4r2n6u3apmtcrxlqwuapgjsciihy.block
Binary file not shown.
Oops, something went wrong.