diff --git a/backend/app/api/v1/helper/helper.go b/backend/app/api/v1/helper/helper.go index 4257011f0..822d7fab9 100644 --- a/backend/app/api/v1/helper/helper.go +++ b/backend/app/api/v1/helper/helper.go @@ -2,6 +2,7 @@ package helper import ( "fmt" + "github.com/1Panel-dev/1Panel/backend/buserr" "net/http" "strconv" @@ -51,6 +52,8 @@ func ErrorWithDetail(ctx *gin.Context, code int, msgKey string, err error) { res.Msg = i18n.GetMsgWithMap("ErrAuth", map[string]interface{}{"detail": err}) case errors.Is(constant.ErrInitialPassword, err): res.Msg = i18n.GetMsgWithMap("ErrInitialPassword", map[string]interface{}{"detail": err}) + case errors.As(err, &buserr.BusinessError{}): + res.Msg = err.Error() default: res.Msg = i18n.GetMsgWithMap(msgKey, map[string]interface{}{"detail": err}) } diff --git a/backend/app/service/app.go b/backend/app/service/app.go index 09d3e09c7..0a3cffe11 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -142,7 +142,7 @@ func (a AppService) Install(name string, appDetailId uint, params map[string]int list, _ := appInstallRepo.GetBy(commonRepo.WithByName(name)) if len(list) > 0 { - return nil, buserr.New(constant.ErrNameIsExist, "", nil) + return nil, buserr.New(constant.ErrNameIsExist) } httpPort, err := checkPort("PANEL_APP_PORT_HTTP", params) diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 477d0d5ed..ada5f86fc 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -89,7 +89,7 @@ func checkPort(key string, params map[string]interface{}) (int, error) { if ok { portN := int(math.Ceil(port.(float64))) if common.ScanPort(portN) { - return portN, buserr.New(constant.ErrPortInUsed, portN, nil) + return portN, buserr.WithMessage(constant.ErrPortInUsed, portN, nil) } else { return portN, nil } @@ -378,7 +378,7 @@ func checkLimit(app model.App) error { return err } if len(installs) >= app.Limit { - return buserr.New(constant.ErrAppLimit, "", nil) + return buserr.New(constant.ErrAppLimit) } } return nil @@ -414,7 +414,7 @@ func checkRequiredAndLimit(app model.App) error { _, err = appInstallRepo.GetFirst(appInstallRepo.WithDetailIdsIn(detailIds)) if err != nil { - return buserr.New(constant.ErrAppRequired, requireApp.Name, nil) + return buserr.WithMessage(constant.ErrAppRequired, requireApp.Name, nil) } } } @@ -439,6 +439,7 @@ func copyAppData(key, version, installName string, params map[string]interface{} fileOp := files.NewFileOp() resourceDir := path.Join(constant.AppResourceDir, key, "versions", version) installAppDir := path.Join(constant.AppInstallDir, key) + if !fileOp.Stat(installAppDir) { if err = fileOp.CreateDir(installAppDir, 0755); err != nil { return @@ -450,7 +451,11 @@ func copyAppData(key, version, installName string, params map[string]interface{} return } } - if err = fileOp.Copy(resourceDir, appDir); err != nil { + if err = fileOp.Copy(resourceDir, installAppDir); err != nil { + return + } + versionDir := path.Join(installAppDir, version) + if err = fileOp.Rename(versionDir, appDir); err != nil { return } envPath := path.Join(appDir, ".env") diff --git a/backend/buserr/errors.go b/backend/buserr/errors.go index c06f068a7..6def4541b 100644 --- a/backend/buserr/errors.go +++ b/backend/buserr/errors.go @@ -13,7 +13,12 @@ type BusinessError struct { func (e BusinessError) Error() string { - content := i18n.GetErrMsg(e.Msg, map[string]interface{}{"detail": e.Detail}) + content := "" + if e.Detail != nil { + content = i18n.GetErrMsg(e.Msg, map[string]interface{}{"detail": e.Detail}) + } else { + content = i18n.GetErrMsg(e.Msg, nil) + } if content == "" { if e.Err != nil { return e.Err.Error() @@ -23,7 +28,15 @@ func (e BusinessError) Error() string { return content } -func New(Key string, detail interface{}, err error) BusinessError { +func New(Key string) BusinessError { + return BusinessError{ + Msg: Key, + Detail: nil, + Err: nil, + } +} + +func WithMessage(Key string, detail interface{}, err error) BusinessError { return BusinessError{ Msg: Key, Detail: detail, diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index 75f37bfdd..c4ff5e1c9 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -231,8 +231,8 @@ func (f FileOp) CopyDir(src, dst string) error { if err != nil { return err } - //dstDir := filepath.Join(dst, srcInfo.Name()) - if err := f.Fs.MkdirAll(dst, srcInfo.Mode()); err != nil { + dstDir := filepath.Join(dst, srcInfo.Name()) + if err := f.Fs.MkdirAll(dstDir, srcInfo.Mode()); err != nil { return err } @@ -245,15 +245,13 @@ func (f FileOp) CopyDir(src, dst string) error { for _, obj := range obs { fSrc := filepath.Join(src, obj.Name()) - fDst := filepath.Join(dst, obj.Name()) - if obj.IsDir() { - err = f.CopyDir(fSrc, fDst) + err = f.CopyDir(fSrc, dstDir) if err != nil { errs = append(errs, err) } } else { - err = f.CopyFile(fSrc, fDst) + err = f.CopyFile(fSrc, dstDir) if err != nil { errs = append(errs, err) } @@ -284,7 +282,12 @@ func (f FileOp) CopyFile(src, dst string) error { return err } - dstFile, err := f.Fs.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0775) + srcInfo, err := f.Fs.Stat(src) + if err != nil { + return err + } + + dstFile, err := f.Fs.OpenFile(path.Join(dst, srcInfo.Name()), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0775) if err != nil { return err } @@ -297,7 +300,7 @@ func (f FileOp) CopyFile(src, dst string) error { if err != nil { return err } - if err = f.Fs.Chmod(dst, info.Mode()); err != nil { + if err = f.Fs.Chmod(dstFile.Name(), info.Mode()); err != nil { return err } diff --git a/backend/utils/files/fileinfo.go b/backend/utils/files/fileinfo.go index 7938622c4..1d1bc55ca 100644 --- a/backend/utils/files/fileinfo.go +++ b/backend/utils/files/fileinfo.go @@ -171,13 +171,13 @@ func (f *FileInfo) getContent() error { if err != nil { return nil } - if detectBinary(cByte) { - return buserr.New(constant.ErrFileCanNotRead, "", nil) + if len(cByte) > 0 && detectBinary(cByte) { + return buserr.New(constant.ErrFileCanNotRead) } f.Content = string(cByte) return nil } else { - return buserr.New(constant.ErrFileToLarge, "", nil) + return buserr.New(constant.ErrFileToLarge) } } diff --git a/frontend/src/views/host/file-management/move/index.vue b/frontend/src/views/host/file-management/move/index.vue index 6767ccaa0..4880ca7af 100644 --- a/frontend/src/views/host/file-management/move/index.vue +++ b/frontend/src/views/host/file-management/move/index.vue @@ -90,7 +90,6 @@ const handleClose = () => { }; const getPath = (path: string) => { - console.log(path); addForm.newPath = path; };