Skip to content

Commit cac021c

Browse files
committed
feat: Allow specifing how object data is encoded
Adds a --data-encoding flag to `ipfs object get` to let the user specify base64 encoding for object data. License: MIT Signed-off-by: Alex Potsides <[email protected]>
1 parent d643c04 commit cac021c

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

core/commands/object/object.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,22 @@ This command outputs data in the following encodings:
204204
* "protobuf"
205205
* "json"
206206
* "xml"
207-
(Specified by the "--encoding" or "--enc" flag)`,
207+
(Specified by the "--encoding" or "--enc" flag)
208+
209+
The encoding of the data field can be specifed by the --data-encoding flag
210+
211+
Supported values are:
212+
* "text" (default)
213+
* "base64"
214+
`,
208215
},
209216

210217
Arguments: []cmdkit.Argument{
211218
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
212219
},
220+
Options: []cmdkit.Option{
221+
cmdkit.StringOption("data-encoding", "Encoding type of the data field, either \"text\" or \"base64\".").WithDefault("text"),
222+
},
213223
Run: func(req oldcmds.Request, res oldcmds.Response) {
214224
n, err := req.InvocContext().GetNode()
215225
if err != nil {
@@ -219,6 +229,12 @@ This command outputs data in the following encodings:
219229

220230
fpath := path.Path(req.Arguments()[0])
221231

232+
datafieldenc, _, err := req.Option("data-encoding").String()
233+
if err != nil {
234+
res.SetError(err, cmdkit.ErrNormal)
235+
return
236+
}
237+
222238
object, err := core.Resolve(req.Context(), n.Namesys, n.Resolver, fpath)
223239
if err != nil {
224240
res.SetError(err, cmdkit.ErrNormal)
@@ -231,9 +247,15 @@ This command outputs data in the following encodings:
231247
return
232248
}
233249

250+
data, err := encodeData(pbo.Data(), datafieldenc)
251+
if err != nil {
252+
res.SetError(err, cmdkit.ErrNormal)
253+
return
254+
}
255+
234256
node := &Node{
235257
Links: make([]Link, len(object.Links())),
236-
Data: string(pbo.Data()),
258+
Data: data,
237259
}
238260

239261
for i, link := range object.Links() {
@@ -702,3 +724,14 @@ func unwrapOutput(i interface{}) (interface{}, error) {
702724

703725
return <-ch, nil
704726
}
727+
728+
func encodeData(data []byte, encoding string) (string, error) {
729+
switch encoding {
730+
case "text":
731+
return string(data), nil
732+
case "base64":
733+
return base64.StdEncoding.EncodeToString(data), nil
734+
}
735+
736+
return "", fmt.Errorf("unkown data field encoding")
737+
}

test/sharness/t0050-block.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ test_expect_success "created an object correctly!" '
179179
test_cmp obj_out obj_exp
180180
'
181181

182+
test_expect_success "can specify data encoding as base64" '
183+
ipfs object get --data-encoding base64 $HASH > obj_out &&
184+
echo "{\"Links\":[],\"Data\":\"dGVzdCBqc29uIGZvciBzaGFybmVzcyB0ZXN0\"}" > obj_exp &&
185+
test_cmp obj_out obj_exp
186+
'
187+
182188
test_expect_success "block get output looks right" '
183189
ipfs block get $HASH > pb_block_out &&
184190
test_cmp pb_block_out ../t0051-object-data/testPut.pb

0 commit comments

Comments
 (0)