From f3a4125ee79b103df70f1850776639a51ac572cd Mon Sep 17 00:00:00 2001 From: "chris.lu@gmail.com" Date: Mon, 30 Jul 2012 08:36:25 +0000 Subject: [PATCH] adding compressiong support! git-svn-id: https://weed-fs.googlecode.com/svn/trunk@59 282b0af5-e82d-9cf1-ede4-77906d7719d0 --- weed-fs/src/cmd/weedvolume/weedvolume.go | 6 +++- weed-fs/src/pkg/storage/compress.go | 36 ++++++++++++++++++++++++ weed-fs/src/pkg/storage/needle.go | 14 +++++++-- 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 weed-fs/src/pkg/storage/compress.go diff --git a/weed-fs/src/cmd/weedvolume/weedvolume.go b/weed-fs/src/cmd/weedvolume/weedvolume.go index 9d07856b8..1bd790946 100644 --- a/weed-fs/src/cmd/weedvolume/weedvolume.go +++ b/weed-fs/src/cmd/weedvolume/weedvolume.go @@ -62,7 +62,11 @@ func GetHandler(w http.ResponseWriter, r *http.Request) { return } if ext != "" { - w.Header().Set("Content-Type", mime.TypeByExtension(ext)) + mtype := mime.TypeByExtension(ext) + w.Header().Set("Content-Type", mtype) + if storage.IsCompressable(ext, mtype){ + w.Header().Set("Content-Encoding", "gzip") + } } w.Write(n.Data) } diff --git a/weed-fs/src/pkg/storage/compress.go b/weed-fs/src/pkg/storage/compress.go new file mode 100644 index 000000000..9e63a9fe8 --- /dev/null +++ b/weed-fs/src/pkg/storage/compress.go @@ -0,0 +1,36 @@ +package storage + +import ( + "bytes" + "compress/flate" + "compress/gzip" + "log" + "strings" +) + +func IsCompressable(ext, mtype string) bool { + if ext == ".zip" { + return true + } + if ext == ".rar" { + return true + } + if strings.Index(mtype,"text/")==0 { + return true + } + if strings.Index(mtype,"application/")==0 { + return true + } + return false +} +func GzipData(input []byte) []byte { + buf := new(bytes.Buffer) + w, _ := gzip.NewWriterLevel(buf, flate.BestCompression) + if _, err := w.Write(input); err!=nil { + log.Printf("error compressing data:%s\n", err) + } + if err := w.Close(); err!=nil { + log.Printf("error closing compressed data:%s\n", err) + } + return buf.Bytes() +} diff --git a/weed-fs/src/pkg/storage/needle.go b/weed-fs/src/pkg/storage/needle.go index ef3914e05..b0d499c05 100644 --- a/weed-fs/src/pkg/storage/needle.go +++ b/weed-fs/src/pkg/storage/needle.go @@ -5,6 +5,7 @@ import ( "io" "io/ioutil" "log" + "mime" "net/http" "os" "pkg/util" @@ -31,8 +32,17 @@ func NewNeedle(r *http.Request) (n *Needle, e error) { return } part, _ := form.NextPart() - //log.Println("uploading file " + part.FileName()) - data, _ := ioutil.ReadAll(part) + fname := part.FileName() + data, _ := ioutil.ReadAll(part) + //log.Println("uploading file " + part.FileName()) + dotIndex := strings.LastIndex(fname, ".") + if dotIndex > 0 { + ext := fname[dotIndex:] + mtype := mime.TypeByExtension(ext) + if IsCompressable(ext, mtype){ + data = GzipData(data) + } + } n.Data = data commaSep := strings.LastIndex(r.URL.Path, ",")