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

Add option to decode 'Data' field as base64 #2520

Merged
merged 3 commits into from
Apr 5, 2016
Merged
Changes from 1 commit
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
31 changes: 24 additions & 7 deletions core/commands/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io/ioutil"
"strings"
"text/tabwriter"
"encoding/base64"

mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"

Expand Down Expand Up @@ -224,7 +225,8 @@ This command outputs data in the following encodings:
Marshalers: cmds.MarshalerMap{
cmds.EncodingType("protobuf"): func(res cmds.Response) (io.Reader, error) {
node := res.Output().(*Node)
object, err := deserializeNode(node)
// deserialize the Data field as text as this was the standard behaviour
object, err := deserializeNode(node, "text")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -342,6 +344,7 @@ And then run:
},
Options: []cmds.Option{
cmds.StringOption("inputenc", "Encoding type of input data, either \"protobuf\" or \"json\"."),
cmds.StringOption("datafieldenc", "Encoding type of the data field, either \"text\" or \"base64\"."),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add .Default("text") to the end of this option, so you don't have to do the !found check later

},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
Expand All @@ -365,7 +368,16 @@ And then run:
inputenc = "json"
}

output, err := objectPut(n, input, inputenc)
datafieldenc, found, err := req.Option("datafieldenc").String()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
if !found {
datafieldenc = "text"
}

output, err := objectPut(n, input, inputenc, datafieldenc)
if err != nil {
errType := cmds.ErrNormal
if err == ErrUnknownObjectEnc {
Expand Down Expand Up @@ -454,7 +466,7 @@ func nodeFromTemplate(template string) (*dag.Node, error) {
var ErrEmptyNode = errors.New("no data or links in this node")

// objectPut takes a format option, serializes bytes from stdin and updates the dag with that data
func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, error) {
func objectPut(n *core.IpfsNode, input io.Reader, encoding string, dataFieldEncoding string) (*Object, error) {

data, err := ioutil.ReadAll(io.LimitReader(input, inputLimit+10))
if err != nil {
Expand All @@ -480,7 +492,7 @@ func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, err
return nil, ErrEmptyNode
}

dagnode, err = deserializeNode(node)
dagnode, err = deserializeNode(node, dataFieldEncoding)
if err != nil {
return nil, err
}
Expand All @@ -501,7 +513,7 @@ func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, err
return nil, ErrEmptyNode
}

dagnode, err = deserializeNode(node)
dagnode, err = deserializeNode(node, dataFieldEncoding)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -566,9 +578,14 @@ func getOutput(dagnode *dag.Node) (*Object, error) {
}

// converts the Node object into a real dag.Node
func deserializeNode(node *Node) (*dag.Node, error) {
func deserializeNode(node *Node, dataFieldEncoding string) (*dag.Node, error) {
dagnode := new(dag.Node)
dagnode.Data = []byte(node.Data)
if dataFieldEncoding == "text" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should do a switch here, so we don't accidentaly get base64 encoding by passing "cats"

dagnode.Data = []byte(node.Data)
} else {
dagnode.Data, _ = base64.StdEncoding.DecodeString(node.Data)
}

dagnode.Links = make([]*dag.Link, len(node.Links))
for i, link := range node.Links {
hash, err := mh.FromB58String(link.Hash)
Expand Down