From 6b132482ca9b62d4019ede37d9623ff15db83909 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Thu, 25 Feb 2021 20:54:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E8=AE=BE=E7=BD=AE=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E7=95=8C=E9=9D=A2=E5=92=8C=E7=94=A8=E6=88=B7=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E7=9A=84=E6=B5=8F=E8=A7=88=E5=99=A8=E5=9B=BE=E6=A0=87?= =?UTF-8?q?=E5=92=8CLogo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/web/actions/default/index/index.go | 1 + .../settings/ip-library/uploadPopup.go | 1 + .../web/actions/default/settings/ui/index.go | 101 ++++++++++++++++++ .../actions/default/settings/user-ui/index.go | 99 +++++++++++++++++ internal/web/actions/default/ui/image.go | 68 ++++++++++++ internal/web/actions/default/ui/init.go | 3 + internal/web/helpers/user_must_auth.go | 2 + web/views/@default/@layout.html | 6 +- web/views/@default/index/index.html | 4 + web/views/@default/settings/ui/index.html | 30 ++++++ .../@default/settings/user-ui/index.html | 30 ++++++ 11 files changed, 344 insertions(+), 1 deletion(-) create mode 100644 internal/web/actions/default/ui/image.go diff --git a/internal/web/actions/default/index/index.go b/internal/web/actions/default/index/index.go index eab96dd6..2b40053c 100644 --- a/internal/web/actions/default/index/index.go +++ b/internal/web/actions/default/index/index.go @@ -65,6 +65,7 @@ func (this *IndexAction) RunGet(params struct { } else { this.Data["version"] = teaconst.Version } + this.Data["faviconFileId"] = config.FaviconFileId this.Show() } diff --git a/internal/web/actions/default/settings/ip-library/uploadPopup.go b/internal/web/actions/default/settings/ip-library/uploadPopup.go index df82350b..9f509158 100644 --- a/internal/web/actions/default/settings/ip-library/uploadPopup.go +++ b/internal/web/actions/default/settings/ip-library/uploadPopup.go @@ -91,6 +91,7 @@ func (this *UploadPopupAction) RunPost(params struct { _, err = this.RPC().FileRPC().UpdateFileFinished(this.AdminContext(), &pb.UpdateFileFinishedRequest{FileId: fileId}) if err != nil { this.ErrorPage(err) + return } // 保存 diff --git a/internal/web/actions/default/settings/ui/index.go b/internal/web/actions/default/settings/ui/index.go index 71ddda23..73b77812 100644 --- a/internal/web/actions/default/settings/ui/index.go +++ b/internal/web/actions/default/settings/ui/index.go @@ -3,7 +3,9 @@ package ui import ( "github.com/TeaOSLab/EdgeAdmin/internal/configloaders" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/iwind/TeaGo/actions" + "io" ) type IndexAction struct { @@ -32,10 +34,14 @@ func (this *IndexAction) RunPost(params struct { ShowFinance bool ShowVersion bool Version string + FaviconFile *actions.File + LogoFile *actions.File Must *actions.Must CSRF *actionutils.CSRF }) { + defer this.CreateLogInfo("修改管理界面设置") + params.Must. Field("productName", params.ProductName). Require("请输入产品名称"). @@ -53,6 +59,101 @@ func (this *IndexAction) RunPost(params struct { config.ShowFinance = params.ShowFinance config.ShowVersion = params.ShowVersion config.Version = params.Version + + // 上传Favicon文件 + if params.FaviconFile != nil { + createResp, err := this.RPC().FileRPC().CreateFile(this.AdminContext(), &pb.CreateFileRequest{ + Filename: params.FaviconFile.Filename, + Size: params.FaviconFile.Size, + IsPublic: true, + }) + if err != nil { + this.ErrorPage(err) + return + } + fileId := createResp.FileId + + // 上传内容 + buf := make([]byte, 512*1024) + reader, err := params.FaviconFile.OriginFile.Open() + if err != nil { + this.ErrorPage(err) + return + } + for { + n, err := reader.Read(buf) + if n > 0 { + _, err = this.RPC().FileChunkRPC().CreateFileChunk(this.AdminContext(), &pb.CreateFileChunkRequest{ + FileId: fileId, + Data: buf[:n], + }) + if err != nil { + this.Fail("上传失败:" + err.Error()) + } + } + if err != nil { + if err == io.EOF { + break + } + this.Fail("上传失败:" + err.Error()) + } + } + + // 置为已完成 + _, err = this.RPC().FileRPC().UpdateFileFinished(this.AdminContext(), &pb.UpdateFileFinishedRequest{FileId: fileId}) + if err != nil { + this.ErrorPage(err) + } + config.FaviconFileId = fileId + } + + // 上传Logo文件 + if params.LogoFile != nil { + createResp, err := this.RPC().FileRPC().CreateFile(this.AdminContext(), &pb.CreateFileRequest{ + Filename: params.LogoFile.Filename, + Size: params.LogoFile.Size, + IsPublic: true, + }) + if err != nil { + this.ErrorPage(err) + return + } + fileId := createResp.FileId + + // 上传内容 + buf := make([]byte, 512*1024) + reader, err := params.LogoFile.OriginFile.Open() + if err != nil { + this.ErrorPage(err) + return + } + for { + n, err := reader.Read(buf) + if n > 0 { + _, err = this.RPC().FileChunkRPC().CreateFileChunk(this.AdminContext(), &pb.CreateFileChunkRequest{ + FileId: fileId, + Data: buf[:n], + }) + if err != nil { + this.Fail("上传失败:" + err.Error()) + } + } + if err != nil { + if err == io.EOF { + break + } + this.Fail("上传失败:" + err.Error()) + } + } + + // 置为已完成 + _, err = this.RPC().FileRPC().UpdateFileFinished(this.AdminContext(), &pb.UpdateFileFinishedRequest{FileId: fileId}) + if err != nil { + this.ErrorPage(err) + } + config.LogoFileId = fileId + } + err = configloaders.UpdateAdminUIConfig(config) if err != nil { this.ErrorPage(err) diff --git a/internal/web/actions/default/settings/user-ui/index.go b/internal/web/actions/default/settings/user-ui/index.go index 61cfee2d..7b0d5cbd 100644 --- a/internal/web/actions/default/settings/user-ui/index.go +++ b/internal/web/actions/default/settings/user-ui/index.go @@ -3,7 +3,9 @@ package userui import ( "github.com/TeaOSLab/EdgeAdmin/internal/configloaders" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/iwind/TeaGo/actions" + "io" ) type IndexAction struct { @@ -32,6 +34,8 @@ func (this *IndexAction) RunPost(params struct { ShowVersion bool Version string ShowFinance bool + FaviconFile *actions.File + LogoFile *actions.File Must *actions.Must CSRF *actionutils.CSRF @@ -53,6 +57,101 @@ func (this *IndexAction) RunPost(params struct { config.ShowVersion = params.ShowVersion config.Version = params.Version config.ShowFinance = params.ShowFinance + + // 上传Favicon文件 + if params.FaviconFile != nil { + createResp, err := this.RPC().FileRPC().CreateFile(this.AdminContext(), &pb.CreateFileRequest{ + Filename: params.FaviconFile.Filename, + Size: params.FaviconFile.Size, + IsPublic: true, + }) + if err != nil { + this.ErrorPage(err) + return + } + fileId := createResp.FileId + + // 上传内容 + buf := make([]byte, 512*1024) + reader, err := params.FaviconFile.OriginFile.Open() + if err != nil { + this.ErrorPage(err) + return + } + for { + n, err := reader.Read(buf) + if n > 0 { + _, err = this.RPC().FileChunkRPC().CreateFileChunk(this.AdminContext(), &pb.CreateFileChunkRequest{ + FileId: fileId, + Data: buf[:n], + }) + if err != nil { + this.Fail("上传失败:" + err.Error()) + } + } + if err != nil { + if err == io.EOF { + break + } + this.Fail("上传失败:" + err.Error()) + } + } + + // 置为已完成 + _, err = this.RPC().FileRPC().UpdateFileFinished(this.AdminContext(), &pb.UpdateFileFinishedRequest{FileId: fileId}) + if err != nil { + this.ErrorPage(err) + } + config.FaviconFileId = fileId + } + + // 上传Logo文件 + if params.LogoFile != nil { + createResp, err := this.RPC().FileRPC().CreateFile(this.AdminContext(), &pb.CreateFileRequest{ + Filename: params.LogoFile.Filename, + Size: params.LogoFile.Size, + IsPublic: true, + }) + if err != nil { + this.ErrorPage(err) + return + } + fileId := createResp.FileId + + // 上传内容 + buf := make([]byte, 512*1024) + reader, err := params.LogoFile.OriginFile.Open() + if err != nil { + this.ErrorPage(err) + return + } + for { + n, err := reader.Read(buf) + if n > 0 { + _, err = this.RPC().FileChunkRPC().CreateFileChunk(this.AdminContext(), &pb.CreateFileChunkRequest{ + FileId: fileId, + Data: buf[:n], + }) + if err != nil { + this.Fail("上传失败:" + err.Error()) + } + } + if err != nil { + if err == io.EOF { + break + } + this.Fail("上传失败:" + err.Error()) + } + } + + // 置为已完成 + _, err = this.RPC().FileRPC().UpdateFileFinished(this.AdminContext(), &pb.UpdateFileFinishedRequest{FileId: fileId}) + if err != nil { + this.ErrorPage(err) + } + config.LogoFileId = fileId + } + err = configloaders.UpdateUserUIConfig(config) if err != nil { this.ErrorPage(err) diff --git a/internal/web/actions/default/ui/image.go b/internal/web/actions/default/ui/image.go new file mode 100644 index 00000000..1febd048 --- /dev/null +++ b/internal/web/actions/default/ui/image.go @@ -0,0 +1,68 @@ +package ui + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "mime" + "path/filepath" + "strconv" +) + +// 公开的图片,不需要检查用户权限 +type ImageAction struct { + actionutils.ParentAction +} + +func (this *ImageAction) Init() { + this.Nav("", "", "") +} + +func (this *ImageAction) RunGet(params struct { + FileId int64 +}) { + fileResp, err := this.RPC().FileRPC().FindEnabledFile(this.AdminContext(), &pb.FindEnabledFileRequest{FileId: params.FileId}) + if err != nil { + this.ErrorPage(err) + return + } + file := fileResp.File + if file == nil { + this.NotFound("file", params.FileId) + return + } + + if !file.IsPublic { + this.NotFound("file", params.FileId) + return + } + + chunkIdsResp, err := this.RPC().FileChunkRPC().FindAllFileChunkIds(this.AdminContext(), &pb.FindAllFileChunkIdsRequest{FileId: file.Id}) + if err != nil { + this.ErrorPage(err) + return + } + + mimeType := "" + if len(file.Filename) > 0 { + ext := filepath.Ext(file.Filename) + mimeType = mime.TypeByExtension(ext) + } + if len(mimeType) == 0 { + mimeType = "image/png" + } + + this.AddHeader("Last-Modified", "Fri, 06 Sep 2019 08:29:50 GMT") + this.AddHeader("Content-Type", mimeType) + this.AddHeader("Content-Length", strconv.FormatInt(file.Size, 10)) + for _, chunkId := range chunkIdsResp.FileChunkIds { + chunkResp, err := this.RPC().FileChunkRPC().DownloadFileChunk(this.AdminContext(), &pb.DownloadFileChunkRequest{FileChunkId: chunkId}) + if err != nil { + this.ErrorPage(err) + return + } + if chunkResp.FileChunk == nil { + continue + } + this.Write(chunkResp.FileChunk.Data) + } +} diff --git a/internal/web/actions/default/ui/init.go b/internal/web/actions/default/ui/init.go index c38cad97..89f2111d 100644 --- a/internal/web/actions/default/ui/init.go +++ b/internal/web/actions/default/ui/init.go @@ -13,6 +13,9 @@ func init() { server. Prefix("/ui"). + // 公共可以访问的链接 + Get("/image/:fileId", new(ImageAction)). + // 以下的需要压缩 Helper(&actions.Gzip{Level: gzip.BestCompression}). Get("/components.js", new(ComponentsAction)). diff --git a/internal/web/helpers/user_must_auth.go b/internal/web/helpers/user_must_auth.go index 9b3ccd45..61b2317d 100644 --- a/internal/web/helpers/user_must_auth.go +++ b/internal/web/helpers/user_must_auth.go @@ -88,6 +88,8 @@ func (this *userMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramNam action.Data["teaShowVersion"] = config.ShowVersion action.Data["teaTitle"] = config.AdminSystemName action.Data["teaName"] = config.ProductName + action.Data["teaFaviconFileId"] = config.FaviconFileId + action.Data["teaLogoFileId"] = config.LogoFileId action.Data["teaUsername"] = configloaders.FindAdminFullname(adminId) action.Data["teaUserAvatar"] = "" diff --git a/web/views/@default/@layout.html b/web/views/@default/@layout.html index bd056647..ac3ca6d4 100644 --- a/web/views/@default/@layout.html +++ b/web/views/@default/@layout.html @@ -4,7 +4,11 @@ {$.teaTitle} + {$if eq .teaFaviconFileId 0} + {$else} + + {$end} {$TEA.SEMANTIC} @@ -23,7 +27,7 @@