From c92a9e61b0ff1a29e7d7f5fddae6cc990000f746 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 28 Jul 2024 16:04:34 +0800 Subject: [PATCH] code refactor --- cmd/webserver.go | 8 ++++---- pkg/api/qrcodes.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/webserver.go b/cmd/webserver.go index 9942fb8b..24bda308 100644 --- a/cmd/webserver.go +++ b/cmd/webserver.go @@ -195,7 +195,7 @@ func startWebServer(c *cli.Context) error { qrCodeRoute.Use(bindMiddleware(middlewares.RequestId(config))) { qrCodeCacheStore := persistence.NewInMemoryStore(time.Minute) - qrCodeRoute.GET("/mobile_url.png", bindCachedPngImage(api.QrCodes.MobileUrlQrCodeHandler, qrCodeCacheStore)) + qrCodeRoute.GET("/mobile_url.png", bindCachedImage(api.QrCodes.MobileUrlQrCodeHandler, qrCodeCacheStore)) } apiRoute := router.Group("/api") @@ -423,15 +423,15 @@ func bindImage(fn core.ImageHandlerFunc) gin.HandlerFunc { } } -func bindCachedPngImage(fn core.DataHandlerFunc, store persistence.CacheStore) gin.HandlerFunc { +func bindCachedImage(fn core.ImageHandlerFunc, store persistence.CacheStore) gin.HandlerFunc { return cache.CachePage(store, time.Minute, func(ginCtx *gin.Context) { c := core.WrapContext(ginCtx) - result, _, err := fn(c) + result, contentType, err := fn(c) if err != nil { utils.PrintDataErrorResult(c, "text/text", err) } else { - utils.PrintDataSuccessResult(c, "img/png", "", result) + utils.PrintDataSuccessResult(c, contentType, "", result) } }) } diff --git a/pkg/api/qrcodes.go b/pkg/api/qrcodes.go index 3811ea5e..1c7e71cf 100644 --- a/pkg/api/qrcodes.go +++ b/pkg/api/qrcodes.go @@ -35,7 +35,7 @@ func (a *QrCodesApi) MobileUrlQrCodeHandler(c *core.Context) ([]byte, string, *e return nil, "", errs.ErrOperationFailed } - return data, "", nil + return data, "image/png", nil } func (a *QrCodesApi) generateUrlQrCode(c *core.Context, url string) ([]byte, *errs.Error) {