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

Inspect to return a new format #104

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion api.go
Expand Up @@ -496,7 +496,7 @@ func (adm AdminClient) newRequest(ctx context.Context, method string, reqData re
}

// Initialize a new HTTP request for the method.
req, err = http.NewRequestWithContext(ctx, method, targetURL.String(), nil)
req, err = http.NewRequestWithContext(ctx, method, targetURL.String(), bytes.NewReader(reqData.content))
if err != nil {
return nil, err
}
Expand Down
74 changes: 50 additions & 24 deletions inspect.go
Expand Up @@ -18,6 +18,7 @@ package madmin

import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
Expand All @@ -28,43 +29,68 @@ import (
// InspectOptions provides options to Inspect.
type InspectOptions struct {
Volume, File string
PublicKey []byte
}

// Inspect makes an admin call to download a raw files from disk.
func (adm *AdminClient) Inspect(ctx context.Context, d InspectOptions) (key [32]byte, c io.ReadCloser, err error) {
path := fmt.Sprintf(adminAPIPrefix + "/inspect-data")
q := make(url.Values)
q.Set("volume", d.Volume)
q.Set("file", d.File)
resp, err := adm.executeMethod(ctx,
http.MethodGet, requestData{
relPath: path,
queryValues: q,
},
)
func (adm *AdminClient) Inspect(ctx context.Context, d InspectOptions) (key []byte, c io.ReadCloser, err error) {
// Add form key/values in the body
form := make(url.Values)
form.Set("volume", d.Volume)
form.Set("file", d.File)
if d.PublicKey != nil {
form.Set("public-key", base64.StdEncoding.EncodeToString(d.PublicKey))
}

method := ""
reqData := requestData{
relPath: fmt.Sprintf(adminAPIPrefix + "/inspect-data"),
}

// If the public-key is specified, create a POST request and send
// parameters as multipart-form instead of query values
if d.PublicKey != nil {
harshavardhana marked this conversation as resolved.
Show resolved Hide resolved
method = http.MethodPost
reqData.customHeaders = make(http.Header)
reqData.customHeaders.Set("Content-Type", "application/x-www-form-urlencoded")
reqData.content = []byte(form.Encode())
} else {
method = http.MethodGet
reqData.queryValues = form
}

resp, err := adm.executeMethod(ctx, method, reqData)
if err != nil {
return key, nil, err
return nil, nil, err
}

if resp.StatusCode != http.StatusOK {
closeResponse(resp)
return key, nil, httpRespToErrorResponse(resp)
return nil, nil, httpRespToErrorResponse(resp)
}
_, err = io.ReadFull(resp.Body, key[:1])

var p [1]byte
_, err = io.ReadFull(resp.Body, p[:])
if err != nil {
closeResponse(resp)
return key, nil, err
}
// This is the only version we know.
if key[0] != 1 {
closeResponse(resp)
return key, nil, errors.New("unknown data version")
return nil, nil, err
}
// Read key...
_, err = io.ReadFull(resp.Body, key[:])
if err != nil {

format := p[0]
switch format {
case 1:
key = make([]byte, 32)
// Read key...
_, err = io.ReadFull(resp.Body, key[:])
if err != nil {
closeResponse(resp)
return nil, nil, err
}
case 2:
default:
closeResponse(resp)
return key, nil, err
return nil, nil, errors.New("unknown data version")

}

// Return body
Expand Down