mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 09:13:49 +08:00
feat(backup): 新建/更新备份账号时验证 s3、sftp、webdav 存储连接 (#1264)
* Initial plan * feat(backup): 新建/更新备份账号时验证 s3、sftp、webdav 存储连接 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * refactor(backup): 优化 validateStorage 函数,统一连接验证逻辑 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * feat: 优化备份账号保存 * fix: lint --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> Co-authored-by: 耗子 <haozi@loli.email>
This commit is contained in:
@@ -91,7 +91,7 @@ func initWeb() (*app.Web, error) {
|
||||
databaseUserService := service.NewDatabaseUserService(databaseUserRepo)
|
||||
backupService := service.NewBackupService(locale, backupRepo)
|
||||
backupAccountRepo := data.NewBackupAccountRepo(locale, db, logger)
|
||||
backupAccountService := service.NewBackupAccountService(backupAccountRepo)
|
||||
backupAccountService := service.NewBackupAccountService(locale, backupAccountRepo)
|
||||
certService := service.NewCertService(locale, certRepo)
|
||||
certDNSRepo := data.NewCertDNSRepo(db, logger)
|
||||
certDNSService := service.NewCertDNSService(certDNSRepo)
|
||||
|
||||
@@ -93,11 +93,11 @@ func (r *backupRepo) Create(ctx context.Context, typ biz.BackupType, target stri
|
||||
name := fmt.Sprintf("%s_%s", target, time.Now().Format("20060102150405"))
|
||||
if app.IsCli {
|
||||
fmt.Println(r.hr)
|
||||
fmt.Println(fmt.Sprintf(r.t.Get("★ Start backup [%s]", time.Now().Format(time.DateTime))))
|
||||
fmt.Println(r.t.Get("★ Start backup [%s]", time.Now().Format(time.DateTime)))
|
||||
fmt.Println(r.hr)
|
||||
fmt.Println(fmt.Sprintf(r.t.Get("|-Backup type: %s", string(typ))))
|
||||
fmt.Println(fmt.Sprintf(r.t.Get("|-Backup account: %s", backupAccount.Name)))
|
||||
fmt.Println(fmt.Sprintf(r.t.Get("|-Backup target: %s", target)))
|
||||
fmt.Println(r.t.Get("|-Backup type: %s", string(typ)))
|
||||
fmt.Println(r.t.Get("|-Backup account: %s", backupAccount.Name))
|
||||
fmt.Println(r.t.Get("|-Backup target: %s", target))
|
||||
}
|
||||
|
||||
switch typ {
|
||||
@@ -122,7 +122,7 @@ func (r *backupRepo) Create(ctx context.Context, typ biz.BackupType, target stri
|
||||
slog.String("target", target),
|
||||
)
|
||||
if app.IsCli {
|
||||
fmt.Println(fmt.Sprintf(r.t.Get("☆ Backup failed: %v [%s]", err, time.Now().Format(time.DateTime))))
|
||||
fmt.Println(r.t.Get("☆ Backup failed: %v [%s]", err, time.Now().Format(time.DateTime)))
|
||||
}
|
||||
} else {
|
||||
r.log.Info("backup created",
|
||||
@@ -132,7 +132,7 @@ func (r *backupRepo) Create(ctx context.Context, typ biz.BackupType, target stri
|
||||
slog.String("target", target),
|
||||
)
|
||||
if app.IsCli {
|
||||
fmt.Println(fmt.Sprintf(r.t.Get("☆ Backup completed [%s]\n", time.Now().Format(time.DateTime))))
|
||||
fmt.Println(r.t.Get("☆ Backup completed [%s]\n", time.Now().Format(time.DateTime)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/leonelquinteros/gotext"
|
||||
"github.com/libtnb/chix"
|
||||
|
||||
"github.com/acepanel/panel/internal/biz"
|
||||
"github.com/acepanel/panel/internal/http/request"
|
||||
"github.com/acepanel/panel/pkg/storage"
|
||||
"github.com/acepanel/panel/pkg/types"
|
||||
)
|
||||
|
||||
type BackupAccountService struct {
|
||||
t *gotext.Locale
|
||||
backupAccountRepo biz.BackupAccountRepo
|
||||
}
|
||||
|
||||
func NewBackupAccountService(backupAccount biz.BackupAccountRepo) *BackupAccountService {
|
||||
func NewBackupAccountService(t *gotext.Locale, backupAccount biz.BackupAccountRepo) *BackupAccountService {
|
||||
return &BackupAccountService{
|
||||
t: t,
|
||||
backupAccountRepo: backupAccount,
|
||||
}
|
||||
}
|
||||
@@ -45,6 +51,11 @@ func (s *BackupAccountService) Create(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err = s.validateStorage(req.Type, req.Info); err != nil {
|
||||
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
account, err := s.backupAccountRepo.Create(r.Context(), req)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "%v", err)
|
||||
@@ -61,6 +72,11 @@ func (s *BackupAccountService) Update(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err = s.validateStorage(req.Type, req.Info); err != nil {
|
||||
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = s.backupAccountRepo.Update(r.Context(), req); err != nil {
|
||||
Error(w, http.StatusInternalServerError, "%v", err)
|
||||
return
|
||||
@@ -99,3 +115,55 @@ func (s *BackupAccountService) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Success(w, nil)
|
||||
}
|
||||
|
||||
// validateStorage 验证存储账号配置是否正确
|
||||
func (s *BackupAccountService) validateStorage(accountType string, info types.BackupAccountInfo) error {
|
||||
var err error
|
||||
var client storage.Storage
|
||||
|
||||
switch biz.BackupAccountType(accountType) {
|
||||
case biz.BackupAccountTypeS3:
|
||||
client, err = storage.NewS3(storage.S3Config{
|
||||
Region: info.Region,
|
||||
Bucket: info.Bucket,
|
||||
AccessKeyID: info.AccessKey,
|
||||
SecretAccessKey: info.SecretKey,
|
||||
Endpoint: info.Endpoint,
|
||||
BasePath: info.Path,
|
||||
AddressingStyle: storage.S3AddressingStyle(info.Style),
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New(s.t.Get("s3 configuration error: %v", err))
|
||||
}
|
||||
case biz.BackupAccountTypeSFTP:
|
||||
client, err = storage.NewSFTP(storage.SFTPConfig{
|
||||
Host: info.Host,
|
||||
Port: info.Port,
|
||||
Username: info.Username,
|
||||
Password: info.Password,
|
||||
PrivateKey: info.PrivateKey,
|
||||
BasePath: info.Path,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New(s.t.Get("sftp configuration error: %v", err))
|
||||
}
|
||||
case biz.BackupAccountTypeWebDav:
|
||||
client, err = storage.NewWebDav(storage.WebDavConfig{
|
||||
URL: info.URL,
|
||||
Username: info.Username,
|
||||
Password: info.Password,
|
||||
BasePath: info.Path,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New(s.t.Get("webdav configuration error: %v", err))
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err = client.List(""); err != nil {
|
||||
return errors.New(s.t.Get("storage connection error: %v", err))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user