Merge pull request #695 from zhangmingfeng/master

增加chunk图片文件支持width和height
This commit is contained in:
Chris Lu 2018-07-22 19:54:04 -07:00 committed by GitHub
commit ff331178aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 14 deletions

View file

@ -3,6 +3,8 @@ package images
import ( import (
"path/filepath" "path/filepath"
"strings" "strings"
"bytes"
"io"
) )
/* /*
@ -13,15 +15,15 @@ import (
* Call this function on any file uploaded to SeaweedFS * Call this function on any file uploaded to SeaweedFS
* *
*/ */
func MaybePreprocessImage(filename string, data []byte, width, height int) (resized []byte, w int, h int) { func MaybePreprocessImage(filename string, data []byte, width, height int) (resized io.ReadSeeker, w int, h int) {
ext := filepath.Ext(filename) ext := filepath.Ext(filename)
ext = strings.ToLower(ext) ext = strings.ToLower(ext)
switch ext { switch ext {
case ".png", ".gif": case ".png", ".gif":
return Resized(ext, data, width, height, "") return Resized(ext, bytes.NewReader(data), width, height, "")
case ".jpg", ".jpeg": case ".jpg", ".jpeg":
data = FixJpgOrientation(data) data = FixJpgOrientation(data)
return Resized(ext, data, width, height, "") return Resized(ext, bytes.NewReader(data), width, height, "")
} }
return data, 0, 0 return bytes.NewReader(data), 0, 0
} }

View file

@ -9,13 +9,14 @@ import (
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"io"
) )
func Resized(ext string, data []byte, width, height int, mode string) (resized []byte, w int, h int) { func Resized(ext string, read io.ReadSeeker, width, height int, mode string) (resized io.ReadSeeker, w int, h int) {
if width == 0 && height == 0 { if width == 0 && height == 0 {
return data, 0, 0 return read, 0, 0
} }
srcImage, _, err := image.Decode(bytes.NewReader(data)) srcImage, _, err := image.Decode(read)
if err == nil { if err == nil {
bounds := srcImage.Bounds() bounds := srcImage.Bounds()
var dstImage *image.NRGBA var dstImage *image.NRGBA
@ -34,7 +35,7 @@ func Resized(ext string, data []byte, width, height int, mode string) (resized [
} }
} }
} else { } else {
return data, bounds.Dx(), bounds.Dy() return read, bounds.Dx(), bounds.Dy()
} }
var buf bytes.Buffer var buf bytes.Buffer
switch ext { switch ext {
@ -45,9 +46,9 @@ func Resized(ext string, data []byte, width, height int, mode string) (resized [
case ".gif": case ".gif":
gif.Encode(&buf, dstImage, nil) gif.Encode(&buf, dstImage, nil)
} }
return buf.Bytes(), dstImage.Bounds().Dx(), dstImage.Bounds().Dy() return bytes.NewReader(buf.Bytes()), dstImage.Bounds().Dx(), dstImage.Bounds().Dy()
} else { } else {
glog.Error(err) glog.Error(err)
} }
return data, 0, 0 return read, 0, 0
} }

View file

@ -135,7 +135,8 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
} }
} }
} }
if ext == ".png" || ext == ".jpg" || ext == ".gif" { var rs io.ReadSeeker
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
width, height := 0, 0 width, height := 0, 0
if r.FormValue("width") != "" { if r.FormValue("width") != "" {
width, _ = strconv.Atoi(r.FormValue("width")) width, _ = strconv.Atoi(r.FormValue("width"))
@ -143,10 +144,10 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
if r.FormValue("height") != "" { if r.FormValue("height") != "" {
height, _ = strconv.Atoi(r.FormValue("height")) height, _ = strconv.Atoi(r.FormValue("height"))
} }
n.Data, _, _ = images.Resized(ext, n.Data, width, height, r.FormValue("mode")) rs, _, _ = images.Resized(ext, bytes.NewReader(n.Data), width, height, r.FormValue("mode"))
} }
if e := writeResponseContent(filename, mtype, bytes.NewReader(n.Data), w, r); e != nil { if e := writeResponseContent(filename, mtype, rs, w, r); e != nil {
glog.V(2).Infoln("response write error:", e) glog.V(2).Infoln("response write error:", e)
} }
} }
@ -164,6 +165,9 @@ func (vs *VolumeServer) tryHandleChunkedFile(n *storage.Needle, fileName string,
if fileName == "" && chunkManifest.Name != "" { if fileName == "" && chunkManifest.Name != "" {
fileName = chunkManifest.Name fileName = chunkManifest.Name
} }
ext := path.Ext(fileName)
mType := "" mType := ""
if chunkManifest.Mime != "" { if chunkManifest.Mime != "" {
mt := chunkManifest.Mime mt := chunkManifest.Mime
@ -179,7 +183,18 @@ func (vs *VolumeServer) tryHandleChunkedFile(n *storage.Needle, fileName string,
Master: vs.GetMaster(), Master: vs.GetMaster(),
} }
defer chunkedFileReader.Close() defer chunkedFileReader.Close()
if e := writeResponseContent(fileName, mType, chunkedFileReader, w, r); e != nil { var rs io.ReadSeeker
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
width, height := 0, 0
if r.FormValue("width") != "" {
width, _ = strconv.Atoi(r.FormValue("width"))
}
if r.FormValue("height") != "" {
height, _ = strconv.Atoi(r.FormValue("height"))
}
rs, _, _ = images.Resized(ext, chunkedFileReader, width, height, r.FormValue("mode"))
}
if e := writeResponseContent(fileName, mType, rs, w, r); e != nil {
glog.V(2).Infoln("response write error:", e) glog.V(2).Infoln("response write error:", e)
} }
return true return true