From 4c39955f2feaa2ffd7e9db1c6f31e8ecddd3b5b3 Mon Sep 17 00:00:00 2001 From: Mystery0 M Date: Fri, 12 May 2023 10:54:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=8E=B7=E5=8F=96=E7=BD=91=E7=AB=99?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=97=B6=EF=BC=8C=E5=85=88=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E4=B8=80=E4=B8=8B=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F=EF=BC=8C=E8=B6=85=E8=BF=8710M=E6=97=B6=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E6=8A=A5=E9=94=99=20(#1011)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What this PR does / why we need it? 修复网站日志文件较大时,在界面上查看日志会导致系统短时间卡住的问题 相关Issue https://github.com/1Panel-dev/1Panel/issues/495 该提交可优化这个Issue的问题 #### Summary of your change 在获取文件内容之前,通过os.Stat获取文件的信息,并对文件大小进行判断,如果过大(目前是>10MB),则抛出错误中止流程 #### Please indicate you've done the following: - [ ] Made sure tests are passing and test coverage is added if needed. - [x] Made sure commit message follow the rule of [Conventional Commits specification](https://www.conventionalcommits.org/). - [ ] Considered the docs impact and opened a new docs issue or PR with docs changes if needed. --- backend/app/service/website.go | 11 ++++++++++- backend/constant/errs.go | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 3efc11991..76b40dea0 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -854,7 +854,16 @@ func (w WebsiteService) OpWebsiteLog(req request.WebsiteLogReq) (*response.Websi return res, nil } } - content, err := os.ReadFile(path.Join(sitePath, "log", req.LogType)) + filePath := path.Join(sitePath, "log", req.LogType) + fileInfo, err := os.Stat(filePath) + if err != nil { + return nil, err + } + if fileInfo.Size() > 10*1024*1024 { + return nil, buserr.New(constant.ErrFileTooLarge) + } + fileInfo.Size() + content, err := os.ReadFile(filePath) if err != nil { return nil, err } diff --git a/backend/constant/errs.go b/backend/constant/errs.go index 142cc78a5..78bb9e0d6 100644 --- a/backend/constant/errs.go +++ b/backend/constant/errs.go @@ -89,6 +89,7 @@ var ( ErrLinkPathNotFound = "ErrLinkPathNotFound" ErrFileIsExit = "ErrFileIsExit" ErrFileUpload = "ErrFileUpload" + ErrFileTooLarge = "ErrFileTooLarge" ) // mysql