Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions agent/app/dto/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,20 @@ type CronjobInfo struct {
ContainerName string `json:"containerName"`
User string `json:"user"`

AppID string `json:"appID"`
Website string `json:"website"`
ExclusionRules string `json:"exclusionRules"`
DBType string `json:"dbType"`
DBName string `json:"dbName"`
URL string `json:"url"`
IsDir bool `json:"isDir"`
SourceDir string `json:"sourceDir"`
SourceAccounts []string `json:"sourceAccounts"`
DownloadAccount string `json:"downloadAccount"`
RetainCopies int `json:"retainCopies"`
AppID string `json:"appID"`
Website string `json:"website"`
ExclusionRules string `json:"exclusionRules"`
DBType string `json:"dbType"`
DBName string `json:"dbName"`
URL string `json:"url"`
IsDir bool `json:"isDir"`
SourceDir string `json:"sourceDir"`
RetainCopies int `json:"retainCopies"`

SourceAccounts []string `json:"sourceAccounts"`
DownloadAccount string `json:"downloadAccount"`
SourceAccountIDs string `json:"sourceAccountIDs"`
DownloadAccountID uint `json:"downloadAccountID"`

LastRecordStatus string `json:"lastRecordStatus"`
LastRecordTime string `json:"lastRecordTime"`
Expand Down
21 changes: 14 additions & 7 deletions agent/app/service/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (u *BackupService) RefreshToken(req dto.OperateByID) error {
}

func (u *BackupService) checkBackupConn(backup *model.BackupAccount) (bool, error) {
client, err := newClient(backup)
client, err := newClient(backup, false)
if err != nil {
return false, err
}
Expand All @@ -340,7 +340,12 @@ func (u *BackupService) checkBackupConn(backup *model.BackupAccount) (bool, erro
if backup.Type != constant.Sftp && backup.Type != constant.Local && targetPath != "/" {
targetPath = strings.TrimPrefix(targetPath, "/")
}
return client.Upload(fileItem, targetPath)

if _, err := client.Upload(fileItem, targetPath); err != nil {
return false, err
}
_, _ = client.Delete(path.Join(backup.BackupPath, "test"))
return true, nil
}

func (u *BackupService) Sync(req dto.SyncFromMaster) error {
Expand Down Expand Up @@ -408,7 +413,7 @@ func (u *BackupService) CheckUsed(id uint) error {

func NewBackupClientWithID(id uint) (*model.BackupAccount, cloud_storage.CloudStorageClient, error) {
account, _ := backupRepo.Get(repo.WithByID(id))
backClient, err := newClient(&account)
backClient, err := newClient(&account, true)
if err != nil {
return nil, nil, err
}
Expand All @@ -433,7 +438,7 @@ func NewBackupClientMap(ids []string) (map[string]backupClientHelper, error) {
accounts, _ = backupRepo.List(repo.WithByIDs(idItems))
clientMap := make(map[string]backupClientHelper)
for _, item := range accounts {
backClient, err := newClient(&item)
backClient, err := newClient(&item, true)
if err != nil {
return nil, err
}
Expand All @@ -448,7 +453,7 @@ func NewBackupClientMap(ids []string) (map[string]backupClientHelper, error) {
return clientMap, nil
}

func newClient(account *model.BackupAccount) (cloud_storage.CloudStorageClient, error) {
func newClient(account *model.BackupAccount, isEncrypt bool) (cloud_storage.CloudStorageClient, error) {
varMap := make(map[string]interface{})
if len(account.Vars) != 0 {
if err := json.Unmarshal([]byte(account.Vars), &varMap); err != nil {
Expand All @@ -457,8 +462,10 @@ func newClient(account *model.BackupAccount) (cloud_storage.CloudStorageClient,
}
varMap["bucket"] = account.Bucket
varMap["backupPath"] = account.BackupPath
account.AccessKey, _ = encrypt.StringDecrypt(account.AccessKey)
account.Credential, _ = encrypt.StringDecrypt(account.Credential)
if isEncrypt {
account.AccessKey, _ = encrypt.StringDecrypt(account.AccessKey)
account.Credential, _ = encrypt.StringDecrypt(account.Credential)
}
switch account.Type {
case constant.Sftp, constant.WebDAV:
varMap["username"] = account.AccessKey
Expand Down
6 changes: 5 additions & 1 deletion core/app/service/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,11 @@ func (u *BackupService) checkBackupConn(backup *model.BackupAccount) (bool, erro
if backup.Type != constant.Sftp && backup.Type != constant.Local && targetPath != "/" {
targetPath = strings.TrimPrefix(targetPath, "/")
}
return client.Upload(fileItem, targetPath)
if _, err := client.Upload(fileItem, targetPath); err != nil {
return false, err
}
_, _ = client.Delete(path.Join(backup.BackupPath, "test"))
return true, nil
}

func syncAccountToAgent(backup model.BackupAccount, operation string) {
Expand Down
16 changes: 8 additions & 8 deletions core/app/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,37 +207,37 @@ func (t *Task) DeleteLogFile() {

func (t *Task) LogWithStatus(msg string, err error) {
if err != nil {
t.Logger.Printf(i18n.GetWithNameAndErr("FailedStatus", msg, err))
t.Logger.Print(i18n.GetWithNameAndErr("FailedStatus", msg, err))
} else {
t.Logger.Printf(i18n.GetWithName("SuccessStatus", msg))
t.Logger.Print(i18n.GetWithName("SuccessStatus", msg))
}
}

func (t *Task) Log(msg string) {
t.Logger.Printf(msg)
t.Logger.Print(msg)
}

func (t *Task) Logf(format string, v ...any) {
t.Logger.Printf(format, v...)
}

func (t *Task) LogFailed(msg string) {
t.Logger.Printf(msg + i18n.GetMsgByKey("Failed"))
t.Logger.Print(msg + i18n.GetMsgByKey("Failed"))
}

func (t *Task) LogFailedWithErr(msg string, err error) {
t.Logger.Printf(fmt.Sprintf("%s %s : %s", msg, i18n.GetMsgByKey("Failed"), err.Error()))
t.Logger.Printf("%s %s : %s", msg, i18n.GetMsgByKey("Failed"), err.Error())
}

func (t *Task) LogSuccess(msg string) {
t.Logger.Printf(msg + i18n.GetMsgByKey("Success"))
t.Logger.Print(msg + i18n.GetMsgByKey("Success"))
}
func (t *Task) LogSuccessF(format string, v ...any) {
t.Logger.Printf(fmt.Sprintf(format, v...) + i18n.GetMsgByKey("Success"))
t.Logger.Print(fmt.Sprintf(format, v...) + i18n.GetMsgByKey("Success"))
}

func (t *Task) LogStart(msg string) {
t.Logger.Printf(fmt.Sprintf("%s%s", i18n.GetMsgByKey("Start"), msg))
t.Logger.Printf("%s%s", i18n.GetMsgByKey("Start"), msg)
}

func (t *Task) LogWithOps(operate, msg string) {
Expand Down
5 changes: 4 additions & 1 deletion core/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package i18n

import (
"embed"
"fmt"
"strings"

"github.com/1Panel-dev/1Panel/core/global"
Expand Down Expand Up @@ -64,9 +65,10 @@ func GetErrMsg(key string, maps map[string]interface{}) string {
}

func GetMsgByKey(key string) string {
content, _ := global.I18n.Localize(&i18n.LocalizeConfig{
content, err := global.I18n.Localize(&i18n.LocalizeConfig{
MessageID: key,
})
fmt.Println(err)
return content
}

Expand Down Expand Up @@ -132,6 +134,7 @@ func Init() {
_, _ = bundle.LoadMessageFileFS(fs, "lang/ru.yaml")
_, _ = bundle.LoadMessageFileFS(fs, "lang/ms.yaml")
_, _ = bundle.LoadMessageFileFS(fs, "lang/ko.yaml")
global.I18n = i18n.NewLocalizer(bundle, "en")
}

func UseI18nForCmd(lang string) {
Expand Down
1 change: 0 additions & 1 deletion core/i18n/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ErrBackupCheck: "Backup account connection test failed {{ .err}}"
ErrBackupLocal: "Local backup account does not support this operation!"
ErrBackupPublic: "Non-public backup account detected, please check and retry!"
ErrOSSConn: "Cannot retrieve latest version, please check if the server can connect to external network."
ErrEntrance: "Security entrance error, please check and retry!"

#license
ErrLicense: "License format error, please check and retry!"
Expand Down
1 change: 0 additions & 1 deletion core/i18n/lang/ja.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ErrBackupCheck: "バックアップアカウントの接続テストに失敗し
ErrBackupLocal: "ローカルサーバーのバックアップアカウントはこの操作をサポートしていません!"
ErrBackupPublic: "バックアップアカウントが公開ではないことが検出されました、確認して再試行してください!"
ErrOSSConn: "最新バージョンを取得できませんでした、サーバーが外部ネットワークに接続できるか確認してください。"
ErrEntrance: "セキュアエントランス情報エラー、確認して再試行してください!"

#license
ErrLicense: "ライセンスフォーマットエラー、確認して再試行してください!"
Expand Down
1 change: 0 additions & 1 deletion core/i18n/lang/ko.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ErrBackupCheck: "백업 계정 연결 테스트 실패 {{ .err}}"
ErrBackupLocal: "로컬 서버 백업 계정은 현재 작업을 지원하지 않습니다!"
ErrBackupPublic: "이 백업 계정이 공용이 아님을 감지했습니다. 다시 확인하고 시도해 주세요!"
ErrOSSConn: "최신 버전을 가져올 수 없습니다. 서버가 외부 네트워크에 연결되어 있는지 확인하세요."
ErrEntrance: "보안 입구 정보가 잘못되었습니다. 다시 확인하고 시도해 주세요!"

#license
ErrLicense: "라이선스 형식이 잘못되었습니다. 다시 확인하고 시도해 주세요!"
Expand Down
1 change: 0 additions & 1 deletion core/i18n/lang/ms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ErrBackupCheck: "Cubaan sambungan akaun sandaran gagal {{ .err}}"
ErrBackupLocal: "Akaun sandaran pelayan tempatan tidak menyokong operasi ini!"
ErrBackupPublic: "Akaun sandaran ini didapati bukan awam, sila semak dan cuba lagi!"
ErrOSSConn: "Tidak dapat mendapatkan versi terkini, sila pastikan pelayan boleh disambung ke rangkaian luar."
ErrEntrance: "Maklumat pintu masuk selamat salah, sila semak dan cuba lagi!"

#license
ErrLicense: "Format lesen salah, sila semak dan cuba lagi!"
Expand Down
1 change: 0 additions & 1 deletion core/i18n/lang/pt-BR.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ErrBackupCheck: "Falha na conexão de teste da conta de backup {{ .err}}"
ErrBackupLocal: "A conta de backup do servidor local não suporta essa operação!"
ErrBackupPublic: "Detectamos que a conta de backup não é pública, verifique e tente novamente!"
ErrOSSConn: "Não foi possível obter a versão mais recente, verifique se o servidor pode se conectar à rede externa."
ErrEntrance: "Erro nas informações de entrada de segurança, verifique e tente novamente!"

#license
ErrLicense: "Erro no formato da licença, verifique e tente novamente!"
Expand Down
1 change: 0 additions & 1 deletion core/i18n/lang/pt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ErrBackupCheck: "Falha na conexão de teste da conta de backup {{ .err}}"
ErrBackupLocal: "A conta de backup do servidor local não suporta essa operação!"
ErrBackupPublic: "Detectamos que a conta de backup não é pública, verifique e tente novamente!"
ErrOSSConn: "Não foi possível obter a versão mais recente, verifique se o servidor pode se conectar à rede externa."
ErrEntrance: "Erro nas informações de entrada de segurança, verifique e tente novamente!"

#license
ErrLicense: "Erro no formato da licença, verifique e tente novamente!"
Expand Down
1 change: 0 additions & 1 deletion core/i18n/lang/zh-Hant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ErrBackupCheck: "備份帳號測試連接失敗 {{ .err}}"
ErrBackupLocal: "本地伺服器備份帳號暫不支持該操作!"
ErrBackupPublic: "檢測到該備份帳號為非公用,請檢查後重試!"
ErrOSSConn: "無法獲取最新版本,請確認伺服器是否能夠連接外部網絡。"
ErrEntrance: "安全入口信息錯誤,請檢查後重試!"

#license
ErrLicense: "許可證格式錯誤,請檢查後重試!"
Expand Down
3 changes: 1 addition & 2 deletions core/i18n/lang/zh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ErrApiConfigKeyTimeInvalid: "API 接口时间戳错误: {{ .detail }}"
ErrDemoEnvironment: "演示服务器,禁止此操作!"
ErrCmdTimeout: "命令执行超时!"
ErrEntrance: "安全入口信息错误,请检查后重试!"
ErrGroupIsDefault: '默认分组,无法删除'
ErrGroupIsDefault: "默认分组,无法删除"
ErrLocalDelete: "无法删除本地节点!"

#backup
Expand All @@ -28,7 +28,6 @@ ErrBackupCheck: "备份账号测试连接失败 {{ .err}}"
ErrBackupLocal: "本地服务器备份账号暂不支持该操作!"
ErrBackupPublic: "检测到该备份账号为非公用,请检查后重试!"
ErrOSSConn: "无法获取最新版本,请确认服务器是否能够连接外部网络。"
ErrEntrance: "安全入口信息错误,请检查后重试!"

#license
ErrLicense: "许可证格式错误,请检查后重试!"
Expand Down
63 changes: 63 additions & 0 deletions core/utils/cloud_storage/client/ali.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,69 @@ func (a aliClient) Upload(src, target string) (bool, error) {
return true, nil
}

func (a aliClient) Delete(pathItem string) (bool, error) {
pathItem = path.Join("root", pathItem)
fileInfo, err := a.loadFileWithName(pathItem)
if err != nil {
return false, err
}
client := resty.New()
data := map[string]interface{}{
"drive_id": a.driveID,
"file_id": fileInfo.FileID,
}
url := "https://api.alipan.com/v2/file/delete"
resp, err := client.R().
SetHeader("Authorization", a.token).
SetBody(data).
Post(url)
if err != nil {
return false, err
}
if resp.StatusCode() != 204 {
return false, fmt.Errorf("delete file %s failed, err: %v", pathItem, string(resp.Body()))
}
return true, nil
}

func (a aliClient) loadFileWithName(pathItem string) (fileInfo, error) {
pathItems := strings.Split(pathItem, "/")
var (
fileInfos []fileInfo
err error
)
parentID := "root"
for i := 0; i < len(pathItems); i++ {
if len(pathItems[i]) == 0 {
continue
}
fileInfos, err = a.loadFileWithParentID(parentID)
if err != nil {
return fileInfo{}, err
}
isEnd := false
if i == len(pathItems)-2 {
isEnd = true
}
exist := false
for _, item := range fileInfos {
if item.Name == pathItems[i+1] {
if isEnd {
return item, nil
} else {
parentID = item.FileID
exist = true
}
}
}
if !exist {
return fileInfo{}, errors.New("no such file or dir")
}

}
return fileInfo{}, errors.New("no such file or dir")
}

func (a aliClient) loadFileWithParentID(parentID string) ([]fileInfo, error) {
client := resty.New()
data := map[string]interface{}{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the first version, there were several issues:

  1. The func header was placed at the end with parentheses instead of a space after it.
  2. A blank line could be used to separate multiple lines.

Here's the correct implementation:

package alipan_test 

import (
	"testing"
)

// This function loads JSON data from 'filePath'.
func loadJSON(filePath string) (data interface{}) {
    // Implement loading logic here
}

type AliPanTest struct {}

func (thisAli PanAliClient) LoadJson(filePath string) (string, error) {  
   var result []interface{}
   json, _ := io.ReadFile(filePath)
   result = loadJSON(filePath)
   return "",nil
  
}

Regarding the changes required in the second code snippet related to the deletion functionality, here are some possible suggestions that can be made:

  • Use different file paths like "https://test.example/ali/..." instead of just uploading files directly from an URL.
  • Make sure you implement proper authorization and authentication steps when accessing APIs or files on their respective services.
    Note that I am assuming the original intention behind this change, as it may depend on how they intended to use those functions elsewhere within their application structure which isn't described clearly.
    Remember to always test new functionalities thoroughly and consider all edge cases before rolling them out. Always ensure security protocols and other important aspects are well implemented during modifications so as not break existing processes.

Expand Down
7 changes: 7 additions & 0 deletions core/utils/cloud_storage/client/cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,10 @@ func (c cosClient) Upload(src, target string) (bool, error) {
}
return true, nil
}

func (c cosClient) Delete(path string) (bool, error) {
if _, err := c.clientWithBucket.Object.Delete(context.Background(), path); err != nil {
return false, err
}
return true, nil
}
Loading
Loading