diff --git a/cmd/answer/wire_gen.go b/cmd/answer/wire_gen.go index c263f0d2..3fa06bf7 100644 --- a/cmd/answer/wire_gen.go +++ b/cmd/answer/wire_gen.go @@ -204,7 +204,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, roleController := controller_backyard.NewRoleController(roleService) answerAPIRouter := router.NewAnswerAPIRouter(langController, userController, commentController, reportController, voteController, tagController, followController, collectionController, questionController, answerController, searchController, revisionController, rankController, controller_backyardReportController, userBackyardController, reasonController, themeController, siteInfoController, siteinfoController, notificationController, dashboardController, uploadController, activityController, roleController) swaggerRouter := router.NewSwaggerRouter(swaggerConf) - uiRouter := router.NewUIRouter(siteinfoController) + uiRouter := router.NewUIRouter(siteinfoController, siteInfoCommonService) authUserMiddleware := middleware.NewAuthUserMiddleware(authService, siteInfoCommonService) avatarMiddleware := middleware.NewAvatarMiddleware(serviceConf, uploaderService) templateRenderController := templaterender.NewTemplateRenderController(questionService, userService, tagService, answerService, commentService, dataData, siteInfoCommonService) diff --git a/internal/router/ui.go b/internal/router/ui.go index 1255d6ce..2654a37e 100644 --- a/internal/router/ui.go +++ b/internal/router/ui.go @@ -8,6 +8,8 @@ import ( "os" "github.com/answerdev/answer/internal/controller" + "github.com/answerdev/answer/internal/service/siteinfo_common" + "github.com/answerdev/answer/pkg/htmltext" "github.com/answerdev/answer/ui" "github.com/gin-gonic/gin" "github.com/segmentfault/pacman/log" @@ -20,11 +22,18 @@ const UIStaticPath = "build/static" // UIRouter is an interface that provides ui static file routers type UIRouter struct { siteInfoController *controller.SiteinfoController + siteInfoService *siteinfo_common.SiteInfoCommonService } // NewUIRouter creates a new UIRouter instance with the embed resources -func NewUIRouter(siteInfoController *controller.SiteinfoController) *UIRouter { - return &UIRouter{siteInfoController: siteInfoController} +func NewUIRouter( + siteInfoController *controller.SiteinfoController, + siteInfoService *siteinfo_common.SiteInfoCommonService, +) *UIRouter { + return &UIRouter{ + siteInfoController: siteInfoController, + siteInfoService: siteInfoService, + } } // _resource is an interface that provides static file, it's a private interface @@ -74,8 +83,18 @@ func (a *UIRouter) Register(r *gin.Engine) { filePath := "" switch urlPath { case "/favicon.ico": - c.Header("content-type", "image/vnd.microsoft.icon") - filePath = UIRootFilePath + urlPath + branding, err := a.siteInfoService.GetSiteBranding(c) + if err != nil { + log.Error(err) + } + if branding.Favicon != "" { + c.String(http.StatusOK, htmltext.GetPicByUrl(branding.Favicon)) + return + } else { + c.Header("content-type", "image/vnd.microsoft.icon") + filePath = UIRootFilePath + urlPath + + } case "/manifest.json": // filePath = UIRootFilePath + urlPath a.siteInfoController.GetManifestJson(c) diff --git a/pkg/htmltext/htmltext.go b/pkg/htmltext/htmltext.go index e58c996b..7f8e6e07 100644 --- a/pkg/htmltext/htmltext.go +++ b/pkg/htmltext/htmltext.go @@ -1,6 +1,8 @@ package htmltext import ( + "io/ioutil" + "net/http" "net/url" "regexp" "strings" @@ -80,3 +82,16 @@ func FetchExcerpt(html, trimMarker string, limit int) (text string) { text += trimMarker return } + +func GetPicByUrl(Url string) string { + res, err := http.Get(Url) + if err != nil { + return "" + } + defer res.Body.Close() + pix, err := ioutil.ReadAll(res.Body) + if err != nil { + return "" + } + return string(pix) +}