adding uncompressing support!

git-svn-id: https://weed-fs.googlecode.com/svn/trunk@61 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
chris.lu@gmail.com 2012-07-30 08:52:11 +00:00
parent 50c3f1d4a7
commit c627942691
2 changed files with 16 additions and 1 deletions

View file

@ -65,7 +65,11 @@ func GetHandler(w http.ResponseWriter, r *http.Request) {
mtype := mime.TypeByExtension(ext)
w.Header().Set("Content-Type", mtype)
if storage.IsCompressable(ext, mtype){
w.Header().Set("Content-Encoding", "gzip")
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip"){
w.Header().Set("Content-Encoding", "gzip")
}else{
n.Data = storage.UnGzipData(n.Data)
}
}
}
w.Write(n.Data)

View file

@ -4,6 +4,7 @@ import (
"bytes"
"compress/flate"
"compress/gzip"
"io/ioutil"
"log"
"strings"
)
@ -34,3 +35,13 @@ func GzipData(input []byte) []byte {
}
return buf.Bytes()
}
func UnGzipData(input []byte) []byte {
buf := bytes.NewBuffer(input)
r, _ := gzip.NewReader(buf)
defer r.Close()
output, err := ioutil.ReadAll(r)
if err!=nil {
log.Printf("error uncompressing data:%s\n", err)
}
return output
}