172 lines
3.1 KiB
Go
172 lines
3.1 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
const fileAPI = "/v3/api/file/"
|
|
const serviceAPI = "/v3/api/service/"
|
|
|
|
type Client struct{}
|
|
|
|
func NewClient() *Client {
|
|
return &Client{}
|
|
}
|
|
|
|
func (cli *Client) ServiceHello(ctx context.Context, ref string) (bool, error) {
|
|
var res bool
|
|
var err error
|
|
|
|
ref, err = convertServiceLink(ref)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, "GET", ref, nil)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
transport := &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
},
|
|
}
|
|
client := &http.Client{
|
|
Transport: transport,
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode == http.StatusOK {
|
|
res = true
|
|
}
|
|
return res, err
|
|
}
|
|
|
|
func (cli *Client) FileExists(ctx context.Context, ref string) (bool, error) {
|
|
var res bool
|
|
var err error
|
|
|
|
ref, err = convertFileLink(ref)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, "HEAD", ref, nil)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
transport := &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
},
|
|
}
|
|
client := &http.Client{
|
|
Transport: transport,
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode == http.StatusOK {
|
|
res = true
|
|
}
|
|
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)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", 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.StatusCode)
|
|
return err
|
|
}
|
|
|
|
dirname := filepath.Dir(filename)
|
|
err = os.MkdirAll(dirname, 0750)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0640)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = io.Copy(file, resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return err
|
|
}
|