From 79d18c69b4ba68e6fb84893ff7b43027b4db5387 Mon Sep 17 00:00:00 2001 From: zhangmingfeng Date: Thu, 5 Jul 2018 10:34:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0chunk=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=94=AF=E6=8C=81width=E5=92=8Cheight?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- weed/images/preprocess.go | 10 ++++++---- weed/images/resizing.go | 13 ++++++------ weed/server/volume_server_handlers_read.go | 23 ++++++++++++++++++---- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/weed/images/preprocess.go b/weed/images/preprocess.go index 56f14eaa4..0cfb66c6b 100644 --- a/weed/images/preprocess.go +++ b/weed/images/preprocess.go @@ -3,6 +3,8 @@ package images import ( "path/filepath" "strings" + "bytes" + "io" ) /* @@ -13,15 +15,15 @@ import ( * 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 = strings.ToLower(ext) switch ext { case ".png", ".gif": - return Resized(ext, data, width, height, "") + return Resized(ext, bytes.NewReader(data), width, height, "") case ".jpg", ".jpeg": 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 } diff --git a/weed/images/resizing.go b/weed/images/resizing.go index 0ea6b1777..ff0eff5e1 100644 --- a/weed/images/resizing.go +++ b/weed/images/resizing.go @@ -9,13 +9,14 @@ import ( "github.com/chrislusf/seaweedfs/weed/glog" "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 { - return data, 0, 0 + return read, 0, 0 } - srcImage, _, err := image.Decode(bytes.NewReader(data)) + srcImage, _, err := image.Decode(read) if err == nil { bounds := srcImage.Bounds() var dstImage *image.NRGBA @@ -34,7 +35,7 @@ func Resized(ext string, data []byte, width, height int, mode string) (resized [ } } } else { - return data, bounds.Dx(), bounds.Dy() + return read, bounds.Dx(), bounds.Dy() } var buf bytes.Buffer switch ext { @@ -45,9 +46,9 @@ func Resized(ext string, data []byte, width, height int, mode string) (resized [ case ".gif": 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 { glog.Error(err) } - return data, 0, 0 + return read, 0, 0 } diff --git a/weed/server/volume_server_handlers_read.go b/weed/server/volume_server_handlers_read.go index b784dd60e..734291a36 100644 --- a/weed/server/volume_server_handlers_read.go +++ b/weed/server/volume_server_handlers_read.go @@ -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 if 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") != "" { 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) } } @@ -164,6 +165,9 @@ func (vs *VolumeServer) tryHandleChunkedFile(n *storage.Needle, fileName string, if fileName == "" && chunkManifest.Name != "" { fileName = chunkManifest.Name } + + ext := path.Ext(fileName) + mType := "" if chunkManifest.Mime != "" { mt := chunkManifest.Mime @@ -179,7 +183,18 @@ func (vs *VolumeServer) tryHandleChunkedFile(n *storage.Needle, fileName string, Master: vs.GetMaster(), } 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) } return true