working commit
This commit is contained in:
+82
-48
@@ -3,20 +3,14 @@ package client
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const fileAPI = "/v3/api/file/"
|
||||
const serviceAPI = "/v3/api/service/"
|
||||
|
||||
type Client struct{}
|
||||
|
||||
func NewClient() *Client {
|
||||
@@ -31,7 +25,7 @@ func (cli *Client) ServiceHello(ctx context.Context, ref string) (bool, error) {
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", ref, nil)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ref, nil)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
@@ -63,7 +57,7 @@ func (cli *Client) FileExists(ctx context.Context, ref string) (bool, error) {
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, "HEAD", ref, nil)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, ref, nil)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
@@ -87,43 +81,6 @@ func (cli *Client) FileExists(ctx context.Context, ref string) (bool, error) {
|
||||
return res, err
|
||||
}
|
||||
|
||||
func createBasicAuthPair(username, password string) string {
|
||||
auth := username + ":" + password
|
||||
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
|
||||
}
|
||||
|
||||
func convertFileLink(ref string) (string, error) {
|
||||
var err error
|
||||
var res string
|
||||
if !strings.Contains(ref, "://") {
|
||||
ref = "https://" + ref
|
||||
}
|
||||
url, err := url.Parse(ref)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
url.Path = path.Join(fileAPI, url.Path)
|
||||
url.User = nil
|
||||
res = url.String()
|
||||
return res, err
|
||||
}
|
||||
|
||||
func convertServiceLink(ref string) (string, error) {
|
||||
var err error
|
||||
var res string
|
||||
if !strings.Contains(ref, "://") {
|
||||
ref = "https://" + ref
|
||||
}
|
||||
url, err := url.Parse(ref)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
url.Path = path.Join(serviceAPI, url.Path)
|
||||
url.User = nil
|
||||
res = url.String()
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) GetFile(ctx context.Context, ref, filename string) error {
|
||||
var err error
|
||||
ref, err = convertFileLink(ref)
|
||||
@@ -131,7 +88,7 @@ func (cli *Client) GetFile(ctx context.Context, ref, filename string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", ref, nil)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ref, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -150,7 +107,7 @@ func (cli *Client) GetFile(ctx context.Context, ref, filename string) error {
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err := fmt.Errorf("Received wrong status code: %s", resp.StatusCode)
|
||||
err := fmt.Errorf("Received wrong status code: %s", resp.Status)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -169,3 +126,80 @@ func (cli *Client) GetFile(ctx context.Context, ref, filename string) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) PutFile(ctx context.Context, filename, ref string) error {
|
||||
var err error
|
||||
ref, err = convertFileLink(ref)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPut, ref, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
fileinfo, err := os.Stat(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
filesize := fileinfo.Size()
|
||||
|
||||
req.Header.Set("Content-Type", "application/octet-stream")
|
||||
req.Header.Set("Content-Size", strconv.FormatInt(filesize, 10))
|
||||
|
||||
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) DeleteFile(ctx context.Context, ref, filename string) error {
|
||||
var err error
|
||||
ref, err = convertFileLink(ref)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, ref, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user