prefer openlist api credentials

This commit is contained in:
ShukeBta
2026-06-13 20:28:23 +08:00
parent b2b43e58da
commit 4b2839d753
5 changed files with 237 additions and 18 deletions
+74 -8
View File
@@ -227,10 +227,12 @@ func joinRemotePath(base, rel string) string {
}
type alistUploader struct {
name string
server string
token string
client *http.Client
name string
server string
token string
username string
password string
client *http.Client
}
func newAlistUploader(cfg map[string]any) *alistUploader {
@@ -239,10 +241,12 @@ func newAlistUploader(cfg map[string]any) *alistUploader {
func newNamedAlistUploader(name string, cfg map[string]any) *alistUploader {
return &alistUploader{
name: name,
server: strings.TrimRight(strr(cfg["server"]), "/"),
token: strr(cfg["token"]),
client: &http.Client{},
name: name,
server: strings.TrimRight(strr(cfg["server"]), "/"),
token: strr(cfg["token"]),
username: strr(cfg["username"]),
password: strr(cfg["password"]),
client: &http.Client{},
}
}
@@ -250,6 +254,9 @@ func (a *alistUploader) ensureDir(ctx context.Context, remoteDir string) error {
if a.server == "" {
return fmt.Errorf("%s missing server", a.name)
}
if err := a.ensureToken(ctx); err != nil {
return err
}
remoteDir = normalizeRemotePath(remoteDir)
if remoteDir == "/" {
return nil
@@ -277,6 +284,9 @@ func (a *alistUploader) ensureDir(ctx context.Context, remoteDir string) error {
}
func (a *alistUploader) exists(ctx context.Context, remotePath string) (bool, error) {
if err := a.ensureToken(ctx); err != nil {
return false, err
}
payload, _ := json.Marshal(map[string]string{"path": normalizeRemotePath(remotePath)})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, a.server+"/api/fs/get", bytes.NewReader(payload))
if err != nil {
@@ -301,6 +311,9 @@ func (a *alistUploader) exists(ctx context.Context, remotePath string) (bool, er
}
func (a *alistUploader) upload(ctx context.Context, localPath, remotePath string, size int64) error {
if err := a.ensureToken(ctx); err != nil {
return err
}
f, err := os.Open(localPath) // #nosec G304 -- localPath is selected from configured local media files before upload.
if err != nil {
return err
@@ -321,6 +334,59 @@ func (a *alistUploader) upload(ctx context.Context, localPath, remotePath string
return a.checkJSON(resp, "alist upload")
}
func (a *alistUploader) ensureToken(ctx context.Context) error {
if strings.TrimSpace(a.token) != "" {
return nil
}
if strings.TrimSpace(a.username) == "" || a.password == "" {
return nil
}
if a.server == "" {
return fmt.Errorf("%s missing server", a.name)
}
payload, _ := json.Marshal(map[string]string{
"username": a.username,
"password": a.password,
})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, a.server+"/api/auth/login", bytes.NewReader(payload))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := a.client.Do(req)
if err != nil {
return decorateStorageTransportError(a.name, a.server, err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("%s login: http %d: %s", a.name, resp.StatusCode, strings.TrimSpace(string(body)))
}
var out struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Token string `json:"token"`
} `json:"data"`
}
if err := json.Unmarshal(body, &out); err != nil {
return fmt.Errorf("%s login: decode response: %w", a.name, err)
}
if out.Code != 0 && out.Code != 200 {
msg := strings.TrimSpace(out.Message)
if msg == "" {
msg = fmt.Sprintf("code %d", out.Code)
}
return fmt.Errorf("%s login: %s", a.name, msg)
}
a.token = strings.TrimSpace(out.Data.Token)
if a.token == "" {
return fmt.Errorf("%s login returned empty token", a.name)
}
return nil
}
func (a *alistUploader) auth(req *http.Request) {
if a.token != "" {
req.Header.Set("Authorization", a.token)