2023-11-30 21:50:08 +08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2024-01-25 18:17:13 +08:00
|
|
|
"crypto/tls"
|
2023-11-30 21:50:08 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-01-25 18:17:13 +08:00
|
|
|
"net/http"
|
2023-11-30 21:50:08 +08:00
|
|
|
"os"
|
2024-01-25 18:17:13 +08:00
|
|
|
"strings"
|
2023-11-30 21:50:08 +08:00
|
|
|
|
|
|
|
"github.com/studio-b12/gowebdav"
|
|
|
|
)
|
|
|
|
|
|
|
|
type webDAVClient struct {
|
|
|
|
Bucket string
|
|
|
|
client *gowebdav.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWebDAVClient(vars map[string]interface{}) (*webDAVClient, error) {
|
2023-12-01 17:50:09 +08:00
|
|
|
address := loadParamFromVars("address", true, vars)
|
2024-01-15 15:07:00 +08:00
|
|
|
port := loadParamFromVars("port", true, vars)
|
2023-12-01 17:50:09 +08:00
|
|
|
password := loadParamFromVars("password", true, vars)
|
|
|
|
username := loadParamFromVars("username", true, vars)
|
|
|
|
bucket := loadParamFromVars("bucket", true, vars)
|
2023-11-30 21:50:08 +08:00
|
|
|
|
2024-01-15 15:07:00 +08:00
|
|
|
url := fmt.Sprintf("%s:%s", address, port)
|
|
|
|
if len(port) == 0 {
|
|
|
|
url = address
|
|
|
|
}
|
|
|
|
client := gowebdav.NewClient(url, username, password)
|
2024-01-25 18:17:13 +08:00
|
|
|
tlsConfig := &tls.Config{}
|
|
|
|
if strings.HasPrefix(address, "https") {
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
}
|
|
|
|
var transport http.RoundTripper = &http.Transport{
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
}
|
|
|
|
client.SetTransport(transport)
|
2023-11-30 21:50:08 +08:00
|
|
|
if err := client.Connect(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-01 17:50:09 +08:00
|
|
|
return &webDAVClient{Bucket: bucket, client: client}, nil
|
2023-11-30 21:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s webDAVClient) Upload(src, target string) (bool, error) {
|
|
|
|
targetFilePath := s.Bucket + "/" + target
|
2024-01-25 18:17:13 +08:00
|
|
|
srcFile, err := os.Open(src)
|
2023-11-30 21:50:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2024-01-25 18:17:13 +08:00
|
|
|
defer srcFile.Close()
|
2023-11-30 21:50:08 +08:00
|
|
|
|
2024-01-25 18:17:13 +08:00
|
|
|
if err := s.client.WriteStream(targetFilePath, srcFile, 0644); err != nil {
|
2023-11-30 21:50:08 +08:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s webDAVClient) ListBuckets() ([]interface{}, error) {
|
|
|
|
var result []interface{}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s webDAVClient) Download(src, target string) (bool, error) {
|
|
|
|
srcPath := s.Bucket + "/" + src
|
|
|
|
info, err := s.client.Stat(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-12-28 16:29:18 +08:00
|
|
|
targetStat, err := os.Stat(target)
|
|
|
|
if err == nil {
|
|
|
|
if info.Size() == targetStat.Size() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
2023-11-30 21:50:08 +08:00
|
|
|
file, err := os.Create(target)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2024-01-25 18:17:13 +08:00
|
|
|
reader, _ := s.client.ReadStream(srcPath)
|
|
|
|
if _, err := io.Copy(file, reader); err != nil {
|
2023-11-30 21:50:08 +08:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s webDAVClient) Exist(path string) (bool, error) {
|
|
|
|
if _, err := s.client.Stat(s.Bucket + "/" + path); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:50:09 +08:00
|
|
|
func (s webDAVClient) Size(path string) (int64, error) {
|
|
|
|
file, err := s.client.Stat(s.Bucket + "/" + path)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return file.Size(), nil
|
|
|
|
}
|
|
|
|
|
2023-11-30 21:50:08 +08:00
|
|
|
func (s webDAVClient) Delete(filePath string) (bool, error) {
|
|
|
|
if err := s.client.Remove(s.Bucket + "/" + filePath); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s webDAVClient) ListObjects(prefix string) ([]string, error) {
|
|
|
|
files, err := s.client.ReadDir(s.Bucket + "/" + prefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var result []string
|
|
|
|
for _, file := range files {
|
|
|
|
result = append(result, file.Name())
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|