mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-02 14:58:03 +08:00
core/commands: get: Error if specified compression level is invalid
This commit is contained in:
parent
816a047a12
commit
5efd99c1fe
@ -4,6 +4,7 @@ import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@ -22,6 +23,8 @@ import (
|
||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/cheggaaa/pb"
|
||||
)
|
||||
|
||||
var ErrInvalidCompressionLevel = errors.New("Compression level must be between 1 and 9")
|
||||
|
||||
var GetCmd = &cmds.Command{
|
||||
Helptext: cmds.HelpText{
|
||||
Tagline: "Download IPFS objects",
|
||||
@ -47,7 +50,14 @@ may also specify the level of compression by specifying '-l=<1-9>'.
|
||||
cmds.BoolOption("compress", "C", "Compress the output with GZIP compression"),
|
||||
cmds.IntOption("compression-level", "l", "The level of compression (an int between 1 and 9)"),
|
||||
},
|
||||
PreRun: getCheckOptions,
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
err := getCheckOptions(req)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrClient)
|
||||
return
|
||||
}
|
||||
|
||||
node, err := req.Context().GetNode()
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
@ -62,6 +72,11 @@ may also specify the level of compression by specifying '-l=<1-9>'.
|
||||
} else {
|
||||
compressionLevel = gzip.NoCompression
|
||||
}
|
||||
} else {
|
||||
if compressionLevel < 1 || compressionLevel > 9 {
|
||||
res.SetError(ErrInvalidCompressionLevel, cmds.ErrClient)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
reader, err := get(node, req.Arguments()[0], compressionLevel)
|
||||
@ -217,6 +232,14 @@ may also specify the level of compression by specifying '-l=<1-9>'.
|
||||
},
|
||||
}
|
||||
|
||||
func getCheckOptions(req cmds.Request) error {
|
||||
compressionLevel, found, _ := req.Option("compression-level").Int()
|
||||
if found && (compressionLevel < 1 || compressionLevel > 9) {
|
||||
return ErrInvalidCompressionLevel
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func get(node *core.IpfsNode, path string, compression int) (io.Reader, error) {
|
||||
buf := NewBufReadWriter()
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user