working commit
This commit is contained in:
+63
-8
@@ -10,7 +10,9 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -18,6 +20,7 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"mstore/app/descr"
|
||||
"mstore/pkg/auxhttp"
|
||||
)
|
||||
|
||||
@@ -58,7 +61,6 @@ func (cli *Client) PutFile(ctx context.Context, filename, fileuri string) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fileuri, err = convertFileURI(fileuri)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -81,6 +83,7 @@ func (cli *Client) PutFile(ctx context.Context, filename, fileuri string) error
|
||||
|
||||
req.ContentLength = filesize
|
||||
req.Header.Set("Content-Type", "application/octet-stream")
|
||||
req.Header.Set("Content-Size", strconv.FormatInt(filesize, 10))
|
||||
if username != "" && password != "" {
|
||||
basic := auxhttp.EncodeBasicAuth(username, password)
|
||||
req.Header.Add("Authorization", basic)
|
||||
@@ -108,22 +111,18 @@ func (cli *Client) GetFile(ctx context.Context, fileuri, filename string) (int64
|
||||
if err != nil {
|
||||
return size, err
|
||||
}
|
||||
|
||||
fileuri, err = convertFileURI(fileuri)
|
||||
if err != nil {
|
||||
return size, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fileuri, nil)
|
||||
if err != nil {
|
||||
return size, err
|
||||
}
|
||||
|
||||
if username != "" && password != "" {
|
||||
basic := auxhttp.EncodeBasicAuth(username, password)
|
||||
req.Header.Add("Authorization", basic)
|
||||
}
|
||||
|
||||
client := makeHTTPClient()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
@@ -172,7 +171,6 @@ func (cli *Client) DeleteFile(ctx context.Context, fileuri, filename string) err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fileuri, err = convertFileURI(fileuri)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -185,17 +183,74 @@ func (cli *Client) DeleteFile(ctx context.Context, fileuri, filename string) err
|
||||
basic := auxhttp.EncodeBasicAuth(username, password)
|
||||
req.Header.Add("Authorization", basic)
|
||||
}
|
||||
|
||||
client := makeHTTPClient()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err := fmt.Errorf("Received wrong status code: %s", resp.Status)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) ListFiles(ctx context.Context, catalogURI string) ([]descr.File, error) {
|
||||
var err error
|
||||
res := make([]descr.File, 0)
|
||||
|
||||
catalogURI, username, password, err := repackServiceURI(catalogURI)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
catalogURI, err = convertFilesURI(catalogURI)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, catalogURI, nil)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if username != "" && password != "" {
|
||||
basic := auxhttp.EncodeBasicAuth(username, password)
|
||||
req.Header.Add("Authorization", basic)
|
||||
}
|
||||
client := makeHTTPClient()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
if contentLength == "" {
|
||||
err = fmt.Errorf("Empty Content-Length received")
|
||||
return res, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err := fmt.Errorf("Received wrong status code: %s", resp.Status)
|
||||
return res, err
|
||||
}
|
||||
declSize, err := strconv.ParseInt(contentLength, 10, 64)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Wrong Content-Length value: %v", err)
|
||||
return res, err
|
||||
}
|
||||
respBuffer := bytes.NewBuffer(nil)
|
||||
size, err := io.Copy(respBuffer, resp.Body)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
respBytes := respBuffer.Bytes()
|
||||
if size != declSize {
|
||||
err := fmt.Errorf("Mismatch Content-Length and recorded filesize")
|
||||
return res, err
|
||||
}
|
||||
fmt.Printf("list: %s\n",string(respBytes))
|
||||
err = json.Unmarshal(respBytes, &res)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user