fix: WebDav 支持 https (#3700)

This commit is contained in:
ssongliu 2024-01-25 18:17:13 +08:00 committed by GitHub
parent df2a705713
commit ca1710cabc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 26 deletions

View File

@ -108,7 +108,7 @@ func (s sftpClient) Download(src, target string) (bool, error) {
}
defer dstFile.Close()
if _, err = srcFile.WriteTo(dstFile); err != nil {
if _, err = io.Copy(srcFile, dstFile); err != nil {
return false, err
}
return true, err

View File

@ -1,9 +1,12 @@
package client
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/studio-b12/gowebdav"
)
@ -25,6 +28,14 @@ func NewWebDAVClient(vars map[string]interface{}) (*webDAVClient, error) {
url = address
}
client := gowebdav.NewClient(url, username, password)
tlsConfig := &tls.Config{}
if strings.HasPrefix(address, "https") {
tlsConfig.InsecureSkipVerify = true
}
var transport http.RoundTripper = &http.Transport{
TLSClientConfig: tlsConfig,
}
client.SetTransport(transport)
if err := client.Connect(); err != nil {
return nil, err
}
@ -33,22 +44,13 @@ func NewWebDAVClient(vars map[string]interface{}) (*webDAVClient, error) {
func (s webDAVClient) Upload(src, target string) (bool, error) {
targetFilePath := s.Bucket + "/" + target
fileInfo, err := os.Stat(src)
srcFile, err := os.Open(src)
if err != nil {
return false, err
}
// 50M
if fileInfo.Size() > 52428800 {
bytes, _ := os.ReadFile(src)
if err := s.client.Write(targetFilePath, bytes, 0644); err != nil {
return false, err
}
return true, nil
}
file, _ := os.Open(src)
defer file.Close()
defer srcFile.Close()
if err := s.client.WriteStream(targetFilePath, file, 0644); err != nil {
if err := s.client.WriteStream(targetFilePath, srcFile, 0644); err != nil {
return false, err
}
return true, nil
@ -76,16 +78,8 @@ func (s webDAVClient) Download(src, target string) (bool, error) {
return false, err
}
defer file.Close()
// 50M
if info.Size() > 52428800 {
reader, _ := s.client.ReadStream(srcPath)
if _, err := io.Copy(file, reader); err != nil {
return false, err
}
}
bytes, _ := s.client.Read(srcPath)
if err := os.WriteFile(target, bytes, 0644); err != nil {
reader, _ := s.client.ReadStream(srcPath)
if _, err := io.Copy(file, reader); err != nil {
return false, err
}
return true, err

View File

@ -107,9 +107,15 @@ const onDownload = async (row: Backup.RecordInfo) => {
fileDir: row.fileDir,
fileName: row.fileName,
};
await downloadBackupRecord(params).then(async (res) => {
downloadFile(res.data);
});
loading.value = true;
await downloadBackupRecord(params)
.then(async (res) => {
loading.value = false;
downloadFile(res.data);
})
.catch(() => {
loading.value = false;
});
};
const buttons = [