working commit
This commit is contained in:
+120
-61
@@ -15,28 +15,17 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"mstore/pkg/auxhttp"
|
||||
)
|
||||
|
||||
func (cli *Client) FileExists(ctx context.Context, ref string) (bool, error) {
|
||||
var res bool
|
||||
var err error
|
||||
|
||||
ref, err = convertFileRefer(ref)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, ref, nil)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
if cli.username != "" && cli.password != "" {
|
||||
req.Header.Add("Authorization", encodeBasicAuth(cli.username, cli.password))
|
||||
}
|
||||
|
||||
func makeHTTPClient() *http.Client {
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
@@ -45,6 +34,80 @@ func (cli *Client) FileExists(ctx context.Context, ref string) (bool, error) {
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
func convertFileURI(fileuri string) (string, error) {
|
||||
var err error
|
||||
var res string
|
||||
uri, err := url.Parse(fileuri)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
const fileAPI = "/v3/api/file/"
|
||||
uri.Path, err = url.JoinPath(fileAPI, uri.Path)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res = uri.String()
|
||||
return res, err
|
||||
}
|
||||
|
||||
func convertFilesURI(fileuri string) (string, error) {
|
||||
var err error
|
||||
var res string
|
||||
uri, err := url.Parse(fileuri)
|
||||
const filesAPI = "/v3/api/files/"
|
||||
uri.Path, err = url.JoinPath(filesAPI, uri.Path)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res = uri.String()
|
||||
return res, err
|
||||
}
|
||||
|
||||
func repackServiceURI(fileuri string) (string, string, string, error) {
|
||||
var err error
|
||||
var res, username, password string
|
||||
if !strings.Contains(fileuri, "://") {
|
||||
fileuri = "https://" + fileuri
|
||||
}
|
||||
uri, err := url.Parse(fileuri)
|
||||
if err != nil {
|
||||
return res, username, password, err
|
||||
}
|
||||
uri.Path = path.Clean(uri.Path)
|
||||
if uri.User != nil {
|
||||
username = uri.User.Username()
|
||||
password, _ = uri.User.Password()
|
||||
}
|
||||
uri.User = nil
|
||||
uri.Scheme = "https"
|
||||
res = uri.String()
|
||||
return res, username, password, err
|
||||
}
|
||||
|
||||
func (cli *Client) FileExists(ctx context.Context, fileuri string) (bool, error) {
|
||||
var res bool
|
||||
var err error
|
||||
fileuri, username, password, err := repackServiceURI(fileuri)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
fileuri, err = convertFileURI(fileuri)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, fileuri, 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
|
||||
@@ -56,9 +119,14 @@ func (cli *Client) FileExists(ctx context.Context, ref string) (bool, error) {
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) PutFile(ctx context.Context, filename, ref string) error {
|
||||
func (cli *Client) PutFile(ctx context.Context, filename, fileuri string) error {
|
||||
var err error
|
||||
ref, err = convertFileRefer(ref)
|
||||
fileuri, username, password, err := repackServiceURI(fileuri)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fileuri, err = convertFileURI(fileuri)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -68,23 +136,10 @@ func (cli *Client) PutFile(ctx context.Context, filename, ref string) error {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPut, ref, file)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPut, fileuri, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cli.username != "" && cli.password != "" {
|
||||
req.Header.Add("Authorization", encodeBasicAuth(cli.username, cli.password))
|
||||
}
|
||||
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
fileinfo, err := os.Stat(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -93,6 +148,11 @@ func (cli *Client) PutFile(ctx context.Context, filename, ref string) error {
|
||||
|
||||
req.ContentLength = filesize
|
||||
req.Header.Set("Content-Type", "application/octet-stream")
|
||||
if username != "" && password != "" {
|
||||
basic := auxhttp.EncodeBasicAuth(username, password)
|
||||
req.Header.Add("Authorization", basic)
|
||||
}
|
||||
client := makeHTTPClient()
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
@@ -107,31 +167,31 @@ func (cli *Client) PutFile(ctx context.Context, filename, ref string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) GetFile(ctx context.Context, ref, filename string) (int64, error) {
|
||||
func (cli *Client) GetFile(ctx context.Context, fileuri, filename string) (int64, error) {
|
||||
var err error
|
||||
var size int64
|
||||
ref, err = convertFileRefer(ref)
|
||||
|
||||
fileuri, username, password, err := repackServiceURI(fileuri)
|
||||
if err != nil {
|
||||
return size, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ref, nil)
|
||||
fileuri, err = convertFileURI(fileuri)
|
||||
if err != nil {
|
||||
return size, err
|
||||
}
|
||||
|
||||
if cli.username != "" && cli.password != "" {
|
||||
req.Header.Add("Authorization", encodeBasicAuth(cli.username, cli.password))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fileuri, nil)
|
||||
if err != nil {
|
||||
return size, err
|
||||
}
|
||||
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
if username != "" && password != "" {
|
||||
basic := auxhttp.EncodeBasicAuth(username, password)
|
||||
req.Header.Add("Authorization", basic)
|
||||
}
|
||||
|
||||
client := makeHTTPClient()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return size, err
|
||||
@@ -172,29 +232,28 @@ func (cli *Client) GetFile(ctx context.Context, ref, filename string) (int64, er
|
||||
return size, err
|
||||
}
|
||||
|
||||
func (cli *Client) DeleteFile(ctx context.Context, ref, filename string) error {
|
||||
func (cli *Client) DeleteFile(ctx context.Context, fileuri, filename string) error {
|
||||
var err error
|
||||
ref, err = convertFileRefer(ref)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, ref, nil)
|
||||
|
||||
fileuri, username, password, err := repackServiceURI(fileuri)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cli.username != "" && cli.password != "" {
|
||||
req.Header.Add("Authorization", encodeBasicAuth(cli.username, cli.password))
|
||||
fileuri, err = convertFileURI(fileuri)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fileuri, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if username != "" && password != "" {
|
||||
basic := auxhttp.EncodeBasicAuth(username, password)
|
||||
req.Header.Add("Authorization", basic)
|
||||
}
|
||||
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
client := makeHTTPClient()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user