From 83e786967dba7c6c3df8dae6a5a043ec14555df2 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 2 Oct 2018 22:20:34 +0800 Subject: [PATCH] add log image route --- router/rimg/rimg.go | 37 +++++++++++++++++++++++++++++++++++++ router/routes/routes.go | 1 + 2 files changed, 38 insertions(+) diff --git a/router/rimg/rimg.go b/router/rimg/rimg.go index 5ed9e14..a68e3b6 100644 --- a/router/rimg/rimg.go +++ b/router/rimg/rimg.go @@ -8,6 +8,8 @@ import ( _ "image/jpeg" _ "image/png" "io" + "io/ioutil" + "net/http" "os" "path" "strconv" @@ -274,3 +276,38 @@ func saveNewThumbnail(fio io.Reader, newp string) (err error) { return } + +// GetLineLogImage - +func GetLineLogImage(c *context.Context) { + conf := config.GetConf() + fname := c.Param("imgname") + + if !utils.CheckExists(path.Join(conf.LogImageRoot, fname), false) { + c.NotFound("image not found") + return + } + + f, err := os.Open(path.Join(conf.LogImageRoot, fname)) + if err != nil { + c.ServerError(nil) + return + } + defer f.Close() + + r := io.LimitReader(f, 512) + bytes, err := ioutil.ReadAll(r) + if err != nil { + c.ServerError(nil) + return + } + _, err = f.Seek(0, 0) + if err != nil { + c.ServerError(nil) + return + } + + contentType := http.DetectContentType(bytes) + + c.Writer.Header().Set("Content-Type", contentType) + io.Copy(c.Writer, f) +} diff --git a/router/routes/routes.go b/router/routes/routes.go index 36eceb7..9980007 100644 --- a/router/routes/routes.go +++ b/router/routes/routes.go @@ -54,6 +54,7 @@ func SetRoutes(r *gin.Engine) { { imageProcGroup.GET("/origin/:imgname", context.PatchCtx(rimg.GetOriginImage)) imageProcGroup.GET("/thumbnail/:imgname", context.PatchCtx(rimg.GetThumbnailImage)) + imageProcGroup.GET("/line_log_image/:imgname", context.PatchCtx(rimg.GetLineLogImage)) } apiGroup := r.Group("/api")