269 lines
5.8 KiB
Go
269 lines
5.8 KiB
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*
|
|
* This work is published and licensed under a Creative Commons
|
|
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
|
*
|
|
* Distribution of this work is permitted, but commercial use and
|
|
* modifications are strictly prohibited.
|
|
*/
|
|
package client
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"mstore/pkg/auxhttp"
|
|
)
|
|
|
|
func makeHTTPClient() *http.Client {
|
|
transport := &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
},
|
|
}
|
|
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
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode == http.StatusOK {
|
|
res = true
|
|
}
|
|
return res, err
|
|
}
|
|
|
|
func (cli *Client) PutFile(ctx context.Context, filename, fileuri string) error {
|
|
var err error
|
|
fileuri, username, password, err := repackServiceURI(fileuri)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fileuri, err = convertFileURI(fileuri)
|
|
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, fileuri, file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fileinfo, err := os.Stat(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
filesize := fileinfo.Size()
|
|
|
|
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 {
|
|
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) GetFile(ctx context.Context, fileuri, filename string) (int64, error) {
|
|
var err error
|
|
var size int64
|
|
|
|
fileuri, username, password, err := repackServiceURI(fileuri)
|
|
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 {
|
|
return size, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
contentLength := resp.Header.Get("Content-Length")
|
|
if contentLength == "" {
|
|
err = fmt.Errorf("Empty Content-Length received")
|
|
return size, err
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
err := fmt.Errorf("Received wrong status code: %s", resp.Status)
|
|
return size, err
|
|
}
|
|
declSize, err := strconv.ParseInt(contentLength, 10, 64)
|
|
if err != nil {
|
|
err = fmt.Errorf("Wrong Content-Length value: %v", err)
|
|
return size, err
|
|
}
|
|
dirname := filepath.Dir(filename)
|
|
err = os.MkdirAll(dirname, 0750)
|
|
if err != nil {
|
|
return size, err
|
|
}
|
|
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0640)
|
|
if err != nil {
|
|
return size, err
|
|
}
|
|
size, err = io.Copy(file, resp.Body)
|
|
if err != nil {
|
|
return size, err
|
|
}
|
|
if size != declSize {
|
|
err := fmt.Errorf("Mismatch Content-Length and recorded filesize")
|
|
return size, err
|
|
}
|
|
return size, err
|
|
}
|
|
|
|
func (cli *Client) DeleteFile(ctx context.Context, fileuri, filename string) error {
|
|
var err error
|
|
|
|
fileuri, username, password, err := repackServiceURI(fileuri)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|