From 317017a2b45ff67ac694b092a0e41b4fc4d68ae3 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Thu, 1 Jun 2023 17:05:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#1220)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/app.go | 5 ++-- backend/app/service/app_utils.go | 5 ++-- backend/i18n/lang/en.yaml | 5 ++++ backend/i18n/lang/zh.yaml | 5 ++++ backend/utils/files/file_op.go | 6 ++--- backend/utils/http/get.go | 39 ++++++++++++++++++++++++++++++++ frontend/src/lang/modules/zh.ts | 1 + 7 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 backend/utils/http/get.go diff --git a/backend/app/service/app.go b/backend/app/service/app.go index a7cdba379..cc7196b75 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -17,6 +17,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/docker" "github.com/1Panel-dev/1Panel/backend/utils/files" + http2 "github.com/1Panel-dev/1Panel/backend/utils/http" "gopkg.in/yaml.v3" "io" "net/http" @@ -391,7 +392,7 @@ func (a AppService) SyncAppListFromLocal() { appDirEntries, err := os.ReadDir(appDir) app, err := handleLocalApp(localAppDir) if err != nil { - global.LOG.Errorf(i18n.GetMsgWithMap("LocalAppErr", map[string]interface{}{"name": app.Name, "err": err.Error()})) + global.LOG.Errorf(i18n.GetMsgWithMap("LocalAppErr", map[string]interface{}{"name": dirEntry.Name(), "err": err.Error()})) continue } var appDetails []model.AppDetail @@ -591,7 +592,7 @@ func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) { return nil, err } versionUrl := fmt.Sprintf("%s/%s/1panel.json.version.txt", global.CONF.System.AppRepo, global.CONF.System.Mode) - versionRes, err := http.Get(versionUrl) + versionRes, err := http2.GetHttpRes(versionUrl) if err != nil { return nil, err } diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 93db05a08..86bbdb618 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -49,7 +49,6 @@ func checkPort(key string, params map[string]interface{}) (int, error) { port, ok := params[key] if ok { portN := int(math.Ceil(port.(float64))) - oldInstalled, _ := appInstallRepo.ListBy(appInstallRepo.WithPort(portN)) if len(oldInstalled) > 0 { var apps []string @@ -283,7 +282,9 @@ func upgradeInstall(installId uint, detailId uint) error { } } servicesMap[install.ServiceName] = servicesMap[oldServiceName] - delete(servicesMap, oldServiceName) + if install.ServiceName != oldServiceName { + delete(servicesMap, oldServiceName) + } envs := make(map[string]interface{}) if upErr = json.Unmarshal([]byte(install.Env), &envs); upErr != nil { diff --git a/backend/i18n/lang/en.yaml b/backend/i18n/lang/en.yaml index 68e92a1de..9e89fd78c 100644 --- a/backend/i18n/lang/en.yaml +++ b/backend/i18n/lang/en.yaml @@ -39,6 +39,11 @@ ErrAppDirNull: 'version folder does not exist' LocalAppErr: "App {{.name}} sync failed! {{.err}}" ErrContainerName: "ContainerName is already exist" ErrAppSystemRestart: "1Panel restart causes the task to terminate" +ErrCreateHttpClient: "Failed to create HTTP request {{.err}}" +ErrHttpReqTimeOut: "Request timed out {{.err}}" +ErrHttpReqFailed: "Request failed {{.err}}" +ErrHttpReqNotFound: "The file does not exist" +ErrNoSuchHost: "Network connection failed" #file ErrFileCanNotRead: "File can not read" diff --git a/backend/i18n/lang/zh.yaml b/backend/i18n/lang/zh.yaml index 5cb579ece..d84969447 100644 --- a/backend/i18n/lang/zh.yaml +++ b/backend/i18n/lang/zh.yaml @@ -39,6 +39,11 @@ ErrAppDirNull: '版本文件夹不存在' LocalAppErr: "应用 {{.name}} 同步失败!{{.err}}" ErrContainerName: "容器名称已存在" ErrAppSystemRestart: "1Panel 重启导致任务终止" +ErrCreateHttpClient: "创建HTTP请求失败 {{.err}}" +ErrHttpReqTimeOut: "请求超时 {{.err}}" +ErrHttpReqFailed: "请求失败 {{.err}}" +ErrHttpReqNotFound: "文件不存在" +ErrNoSuchHost: "网络连接失败" #file ErrFileCanNotRead: "此文件不支持预览" diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index 56bb7cb7e..aba4d1cd8 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "github.com/1Panel-dev/1Panel/backend/utils/cmd" + http2 "github.com/1Panel-dev/1Panel/backend/utils/http" cZip "github.com/klauspost/compress/zip" "golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/transform" @@ -252,11 +253,10 @@ func (f FileOp) DownloadFileWithProcess(url, dst, key string) error { } func (f FileOp) DownloadFile(url, dst string) error { - resp, err := http.Get(url) + resp, err := http2.GetHttpRes(url) if err != nil { - return fmt.Errorf("get download file [%s] error, err %s", dst, err.Error()) + return err } - defer resp.Body.Close() out, err := os.Create(dst) if err != nil { diff --git a/backend/utils/http/get.go b/backend/utils/http/get.go new file mode 100644 index 000000000..4cecce116 --- /dev/null +++ b/backend/utils/http/get.go @@ -0,0 +1,39 @@ +package http + +import ( + "context" + "github.com/1Panel-dev/1Panel/backend/buserr" + "net/http" + "strings" + "time" +) + +func GetHttpRes(url string) (*http.Response, error) { + client := &http.Client{ + Transport: &http.Transport{ + IdleConnTimeout: 10 * time.Second, + }, + } + + req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil) + if err != nil { + return nil, buserr.WithMap("ErrCreateHttpClient", map[string]interface{}{"err": err.Error()}, err) + } + + resp, err := client.Do(req) + if err != nil { + if err == context.DeadlineExceeded { + return nil, buserr.WithMap("ErrHttpReqTimeOut", map[string]interface{}{"err": err.Error()}, err) + } else { + if strings.Contains(err.Error(), "no such host") { + return nil, buserr.New("ErrNoSuchHost") + } + return nil, buserr.WithMap("ErrHttpReqFailed", map[string]interface{}{"err": err.Error()}, err) + } + } + if resp.StatusCode == 404 { + return nil, buserr.New("ErrHttpReqNotFound") + } + + return resp, nil +} diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index d9880d49c..26b2cb7a3 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -209,6 +209,7 @@ const message = { building: '制作镜像中', downloaderr: '下载失败', upgrading: '升级中', + upgradeerr: '升级失败', }, units: { second: '秒',