add log image route

This commit is contained in:
Jay 2018-10-02 22:20:34 +08:00
parent 76b26ee4a3
commit 83e786967d
2 changed files with 38 additions and 0 deletions

View File

@ -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)
}

View File

@ -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")