fix: make WebDAV upload retry reliable
This commit is contained in:
+43
-1
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -68,6 +70,9 @@ func (a *App) putSettings(c *gin.Context) {
|
||||
} else {
|
||||
a.storage = nil
|
||||
}
|
||||
// Credentials/config changes should repair already-downloaded local files
|
||||
// immediately instead of waiting for the periodic retry interval.
|
||||
go a.resyncPendingUploads()
|
||||
c.JSON(200, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
@@ -77,11 +82,48 @@ func (a *App) testWebDAV(c *gin.Context) {
|
||||
c.Status(400)
|
||||
return
|
||||
}
|
||||
// An empty password means "use the saved password". The password is never
|
||||
// returned to the browser, so this also makes a fresh settings page test
|
||||
// the credential that is actually stored by the server.
|
||||
if v.URL == "" {
|
||||
v.URL = a.cfg.WebDAVURL
|
||||
}
|
||||
if v.User == "" {
|
||||
v.User = a.cfg.WebDAVUser
|
||||
}
|
||||
if v.Pass == "" {
|
||||
v.Pass = a.cfg.WebDAVPass
|
||||
}
|
||||
v.Path = strings.Trim(v.Path, "/")
|
||||
if v.Path == "" {
|
||||
v.Path = a.cfg.WebDAVPath
|
||||
}
|
||||
if v.URL == "" || v.User == "" || v.Pass == "" {
|
||||
c.JSON(400, gin.H{"ok": false, "error": "WebDAV URL、用户名或密码未配置"})
|
||||
return
|
||||
}
|
||||
client := gowebdav.NewClient(v.URL, v.User, v.Pass)
|
||||
client.SetTimeout(20 * time.Second)
|
||||
if err := client.Connect(); err != nil {
|
||||
c.JSON(502, gin.H{"ok": false, "error": err.Error()})
|
||||
c.JSON(502, gin.H{"ok": false, "error": "WebDAV authentication failed: " + err.Error()})
|
||||
return
|
||||
}
|
||||
if v.Path != "" {
|
||||
if err := client.MkdirAll(v.Path, 0755); err != nil {
|
||||
c.JSON(502, gin.H{"ok": false, "error": "WebDAV target path is not writable: " + err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
var token [12]byte
|
||||
if _, err := rand.Read(token[:]); err != nil {
|
||||
c.JSON(500, gin.H{"ok": false, "error": "failed to create test token"})
|
||||
return
|
||||
}
|
||||
testPath := strings.Trim(v.Path, "/") + "/.am-downloader-test-" + fmt.Sprintf("%x", token[:])
|
||||
if err := client.Write(testPath, []byte("ok"), 0644); err != nil {
|
||||
c.JSON(502, gin.H{"ok": false, "error": "WebDAV write test failed: " + err.Error()})
|
||||
return
|
||||
}
|
||||
_ = client.Remove(testPath)
|
||||
c.JSON(200, gin.H{"ok": true, "path": v.Path})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user