working commit
This commit is contained in:
+31
-11
@@ -3,6 +3,7 @@ package client
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -13,22 +14,28 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const fileAPI = "/v3/api/file/"
|
||||
|
||||
type Client struct{}
|
||||
|
||||
func NewClient() *Client {
|
||||
return &Client{}
|
||||
}
|
||||
|
||||
func (cli *Client) FileExists(ctx context.Context, reference string) (bool, error) {
|
||||
func (cli *Client) FileExists(ctx context.Context, ref string) (bool, error) {
|
||||
var res bool
|
||||
var err error
|
||||
|
||||
reqpath := fmt.Sprintf("/v3/api/file/%s", reference)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "HEAD", reqpath, nil)
|
||||
ref, err = convertFileLink(ref)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, "HEAD", ref, nil)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
authValue := createBasicAuthPair
|
||||
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
@@ -48,21 +55,33 @@ func (cli *Client) FileExists(ctx context.Context, reference string) (bool, erro
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) GetFile(ctx context.Context, ref, filename string) error {
|
||||
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
|
||||
|
||||
const api = "/v3/api/file/"
|
||||
|
||||
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 (cli *Client) GetFile(ctx context.Context, ref, filename string) error {
|
||||
var err error
|
||||
ref, err = convertFileLink(ref)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
url.Path = path.Join(api, url.Path)
|
||||
url.User = nil
|
||||
ref = url.String()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", ref, nil)
|
||||
if err != nil {
|
||||
@@ -81,6 +100,7 @@ func (cli *Client) GetFile(ctx context.Context, ref, filename string) error {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err := fmt.Errorf("Received wrong status code: %s", resp.StatusCode)
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user