mtfosbot/router/rimg/rimg.go

335 lines
6.1 KiB
Go
Raw Normal View History

2018-09-18 15:54:36 +00:00
package rimg
import (
"bytes"
"image"
_ "image/gif"
"image/jpeg"
_ "image/jpeg"
_ "image/png"
2018-09-19 09:26:36 +00:00
"io"
2018-10-02 14:20:34 +00:00
"io/ioutil"
"net/http"
2018-09-18 15:54:36 +00:00
"os"
"path"
"strconv"
"github.com/nfnt/resize"
"git.trj.tw/golang/mtfosbot/module/config"
"git.trj.tw/golang/mtfosbot/module/context"
"git.trj.tw/golang/mtfosbot/module/utils"
)
2018-09-19 09:26:36 +00:00
func resizeImage(fio io.Reader, size int, wgth bool) (img image.Image, err error) {
img, _, err = image.Decode(fio)
w := 0
h := 0
if wgth {
w = size
} else {
h = size
}
img = resize.Resize(uint(w), uint(h), img, resize.Lanczos3)
return
}
2018-09-18 15:54:36 +00:00
// GetOriginImage -
func GetOriginImage(c *context.Context) {
2018-09-19 09:26:36 +00:00
// max 1024
2018-09-18 15:54:36 +00:00
fname := c.Param("imgname")
conf := config.GetConf()
imgP := utils.ParsePath(conf.ImageRoot)
exists := utils.CheckExists(imgP, true)
if !exists {
c.NotFound("image path not found")
return
}
2018-09-27 13:43:02 +00:00
subd := c.DefaultQuery("d", "")
if len(subd) > 0 {
imgP = path.Join(imgP, subd)
exists = utils.CheckExists(imgP, true)
if !exists {
c.NotFound("image path not found")
return
}
}
2018-09-18 15:54:36 +00:00
newP := path.Join(imgP, fname)
exists = utils.CheckExists(newP, false)
if !exists {
c.NotFound("image file not found")
return
}
fileBuf, err := os.Open(newP)
if err != nil {
c.ServerError(nil)
return
}
defer fileBuf.Close()
2018-09-19 09:26:36 +00:00
imgf, format, err := image.DecodeConfig(fileBuf)
if err != nil {
c.ServerError(nil)
return
}
// reset pointer to 0
_, err = fileBuf.Seek(0, 0)
2018-09-18 15:54:36 +00:00
if err != nil {
c.ServerError(nil)
return
}
if imgf.Height > 1024 || imgf.Width > 1024 {
2018-09-19 09:26:36 +00:00
img, err := resizeImage(fileBuf, 1024, imgf.Width > imgf.Height)
buf := new(bytes.Buffer)
err = jpeg.Encode(buf, img, nil)
if err != nil {
c.ServerError(nil)
return
}
2018-10-09 16:42:44 +00:00
defer func() {
buf.Reset()
buf = nil
2018-10-11 03:01:34 +00:00
img = nil
2018-10-09 16:42:44 +00:00
}()
2018-09-19 09:26:36 +00:00
c.Writer.Header().Set("Content-Type", "image/jpeg")
c.Writer.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
_, err = c.Writer.Write(buf.Bytes())
if err != nil {
c.ServerError(nil)
}
return
}
// check type
if format != "jpeg" {
2018-09-18 15:54:36 +00:00
img, _, err := image.Decode(fileBuf)
if err != nil {
c.ServerError(nil)
return
}
2018-09-19 09:26:36 +00:00
buf := new(bytes.Buffer)
err = jpeg.Encode(buf, img, nil)
if err != nil {
c.ServerError(nil)
return
}
2018-10-09 16:42:44 +00:00
defer func() {
buf = nil
2018-10-11 03:01:34 +00:00
img = nil
2018-10-09 16:42:44 +00:00
}()
2018-09-19 09:26:36 +00:00
c.Writer.Header().Set("Content-Type", "image/jpeg")
io.Copy(c.Writer, buf)
return
}
c.Writer.Header().Set("Content-Type", "image/jpeg")
io.Copy(c.Writer, fileBuf)
}
// GetThumbnailImage -
func GetThumbnailImage(c *context.Context) {
// max 240
fname := c.Param("imgname")
conf := config.GetConf()
imgP := utils.ParsePath(conf.ImageRoot)
exists := utils.CheckExists(imgP, true)
if !exists {
c.NotFound("image path not found")
return
}
2018-09-27 13:43:02 +00:00
thumbDir := "thumbnail"
subd := c.DefaultQuery("d", "")
if exists := utils.CheckExists(path.Join(imgP, thumbDir, subd), true); !exists {
if err := os.MkdirAll(path.Join(imgP, thumbDir, subd), 0775); err != nil {
c.ServerError(nil)
return
}
}
thumbP := path.Join(imgP, thumbDir, subd, fname)
2018-09-19 09:26:36 +00:00
genNew := false
if !utils.CheckExists(thumbP, false) {
genNew = true
2018-09-27 13:43:02 +00:00
thumbP = path.Join(imgP, subd, fname)
2018-09-19 09:26:36 +00:00
exists = utils.CheckExists(thumbP, false)
if !exists {
c.NotFound("image file not found")
return
}
}
filebuf, err := os.Open(thumbP)
if err != nil {
c.ServerError(nil)
return
}
defer filebuf.Close()
imgconf, format, err := image.DecodeConfig(filebuf)
if err != nil {
c.ServerError(nil)
return
}
// reset file reader ptr
_, err = filebuf.Seek(0, 0)
if err != nil {
c.ServerError(nil)
return
}
if imgconf.Height > 240 || imgconf.Width > 240 {
img, err := resizeImage(filebuf, 240, imgconf.Width > imgconf.Height)
if err != nil {
c.ServerError(nil)
return
2018-09-18 15:54:36 +00:00
}
2018-09-19 09:26:36 +00:00
2018-09-18 15:54:36 +00:00
buf := new(bytes.Buffer)
2018-09-19 09:26:36 +00:00
err = jpeg.Encode(buf, img, nil)
2018-09-18 15:54:36 +00:00
if err != nil {
c.ServerError(nil)
return
}
2018-10-09 16:42:44 +00:00
2018-09-19 09:26:36 +00:00
breader := bytes.NewReader(buf.Bytes())
2018-10-09 16:42:44 +00:00
defer func() {
buf = nil
breader = nil
2018-10-11 03:01:34 +00:00
img = nil
2018-10-09 16:42:44 +00:00
}()
2018-09-19 09:26:36 +00:00
if genNew {
2018-09-27 13:43:02 +00:00
savep := path.Join(conf.ImageRoot, "thumbnail", subd, fname)
2018-09-19 09:26:36 +00:00
err := saveNewThumbnail(breader, savep)
if err != nil {
c.ServerError(nil)
return
}
_, err = breader.Seek(0, 0)
if err != nil {
c.ServerError(nil)
return
}
}
2018-09-18 15:54:36 +00:00
c.Writer.Header().Set("Content-Type", "image/jpeg")
2018-09-19 09:26:36 +00:00
io.Copy(c.Writer, breader)
return
}
if format != "jpeg" {
img, _, err := image.Decode(filebuf)
if err != nil {
c.ServerError(nil)
return
}
buf := new(bytes.Buffer)
err = jpeg.Encode(buf, img, nil)
2018-09-18 15:54:36 +00:00
if err != nil {
c.ServerError(nil)
2018-09-19 09:26:36 +00:00
return
2018-09-18 15:54:36 +00:00
}
2018-09-19 09:26:36 +00:00
breader := bytes.NewReader(buf.Bytes())
2018-10-09 16:42:44 +00:00
defer func() {
buf = nil
breader = nil
2018-10-11 03:01:34 +00:00
img = nil
2018-10-09 16:42:44 +00:00
}()
2018-09-19 09:26:36 +00:00
savep := path.Join(conf.ImageRoot, "thumbnail", fname)
err = saveNewThumbnail(breader, savep)
if err != nil {
c.ServerError(nil)
return
}
_, err = breader.Seek(0, 0)
if err != nil {
c.ServerError(nil)
return
}
c.Writer.Header().Set("Content-Type", "image/jpeg")
io.Copy(c.Writer, breader)
2018-09-18 15:54:36 +00:00
return
}
2018-09-19 09:26:36 +00:00
if genNew {
savep := path.Join(conf.ImageRoot, "thumbnail", fname)
err := saveNewThumbnail(filebuf, savep)
if err != nil {
c.ServerError(nil)
return
}
_, err = filebuf.Seek(0, 0)
if err != nil {
c.ServerError(nil)
return
}
}
2018-09-18 15:54:36 +00:00
c.Writer.Header().Set("Content-Type", "image/jpeg")
2018-09-19 09:26:36 +00:00
io.Copy(c.Writer, filebuf)
}
func saveNewThumbnail(fio io.Reader, newp string) (err error) {
out, err := os.Create(newp)
2018-09-18 15:54:36 +00:00
if err != nil {
2018-09-19 09:26:36 +00:00
return err
2018-09-18 15:54:36 +00:00
}
2018-09-19 09:26:36 +00:00
_, err = io.Copy(out, fio)
if err != nil {
return err
}
return
2018-09-18 15:54:36 +00:00
}
2018-10-02 14:20:34 +00:00
// 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)
}