Skip to content

Commit

Permalink
fix ipfs object get
Browse files Browse the repository at this point in the history
Always base64 encode when encoding to JSON. Fixes #3359.

This is a breaking change.

License: MIT
Signed-off-by: Steven Allen <[email protected]>
  • Loading branch information
Stebalien committed May 15, 2019
1 parent 3316dc1 commit 16dec9a
Showing 1 changed file with 6 additions and 44 deletions.
50 changes: 6 additions & 44 deletions core/commands/object/object.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package objectcmd

import (
"encoding/base64"
"errors"
"fmt"
"io"
Expand All @@ -21,7 +20,7 @@ import (

type Node struct {
Links []Link
Data string
Data []byte
}

type Link struct {
Expand Down Expand Up @@ -197,12 +196,7 @@ This command outputs data in the following encodings:
* "xml"
(Specified by the "--encoding" or "--enc" flag)
The encoding of the object's data field can be specifed by using the
--data-encoding flag
Supported values are:
* "text" (default)
* "base64"
When encoded as JSON or XML, the Data section will be base64 encoded.
`,
},

Expand All @@ -225,11 +219,6 @@ Supported values are:

path := path.New(req.Arguments[0])

datafieldenc, _ := req.Options[encodingOptionName].(string)
if err != nil {
return err
}

nd, err := api.Object().Get(req.Context, path)
if err != nil {
return err
Expand All @@ -245,14 +234,9 @@ Supported values are:
return err
}

out, err := encodeData(data, datafieldenc)
if err != nil {
return err
}

node := &Node{
Links: make([]Link, len(nd.Links())),
Data: out,
Data: data,
}

for i, link := range nd.Links() {
Expand All @@ -269,7 +253,7 @@ Supported values are:
Encoders: cmds.EncoderMap{
cmds.Protobuf: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Node) error {
// deserialize the Data field as text as this was the standard behaviour
object, err := deserializeNode(out, "text")
object, err := deserializeNode(out)
if err != nil {
return nil
}
Expand Down Expand Up @@ -517,20 +501,9 @@ Available templates:
}

// converts the Node object into a real dag.ProtoNode
func deserializeNode(nd *Node, dataFieldEncoding string) (*dag.ProtoNode, error) {
func deserializeNode(nd *Node) (*dag.ProtoNode, error) {
dagnode := new(dag.ProtoNode)
switch dataFieldEncoding {
case "text":
dagnode.SetData([]byte(nd.Data))
case "base64":
data, err := base64.StdEncoding.DecodeString(nd.Data)
if err != nil {
return nil, err
}
dagnode.SetData(data)
default:
return nil, ErrDataEncoding
}
dagnode.SetData(nd.Data)

links := make([]*ipld.Link, len(nd.Links))
for i, link := range nd.Links {
Expand All @@ -548,14 +521,3 @@ func deserializeNode(nd *Node, dataFieldEncoding string) (*dag.ProtoNode, error)

return dagnode, nil
}

func encodeData(data []byte, encoding string) (string, error) {
switch encoding {
case "text":
return string(data), nil
case "base64":
return base64.StdEncoding.EncodeToString(data), nil
}

return "", ErrDataEncoding
}

0 comments on commit 16dec9a

Please sign in to comment.