Default more not to gzip since gzip can be done on client side.

This commit is contained in:
Chris Lu 2012-12-22 13:15:09 -08:00
parent 46b8c4cc98
commit 264678c9b1

View file

@ -1,49 +1,69 @@
package storage package storage
import ( import (
"bytes" "bytes"
"compress/flate" "compress/flate"
"compress/gzip" "compress/gzip"
"io/ioutil" "io/ioutil"
"strings" "strings"
) )
/*
* Default more not to gzip since gzip can be done on client side.
*/
func IsGzippable(ext, mtype string) bool { func IsGzippable(ext, mtype string) bool {
if ext == ".zip" { if strings.HasPrefix(mtype, "text/"){
return false return true
} }
if ext == ".rar" { if ext == ".zip" {
return false return false
} }
if ext == ".gz" { if ext == ".rar" {
return false return false
} }
if strings.Index(mtype,"text/")==0 { if ext == ".gz" {
return true return false
} }
if strings.Index(mtype,"application/")==0 { if ext == ".pdf" {
return true return true
} }
return false if ext == ".css" {
return true
}
if ext == ".js" {
return true
}
if ext == ".json" {
return true
}
if strings.HasPrefix(mtype, "application/") {
if strings.HasSuffix(mtype, "xml") {
return true
}
if strings.HasSuffix(mtype, "script") {
return true
}
}
return false
} }
func GzipData(input []byte) []byte { func GzipData(input []byte) []byte {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
w, _ := gzip.NewWriterLevel(buf, flate.BestCompression) w, _ := gzip.NewWriterLevel(buf, flate.BestCompression)
if _, err := w.Write(input); err!=nil { if _, err := w.Write(input); err != nil {
println("error compressing data:", err) println("error compressing data:", err)
} }
if err := w.Close(); err!=nil { if err := w.Close(); err != nil {
println("error closing compressed data:", err) println("error closing compressed data:", err)
} }
return buf.Bytes() return buf.Bytes()
} }
func UnGzipData(input []byte) []byte { func UnGzipData(input []byte) []byte {
buf := bytes.NewBuffer(input) buf := bytes.NewBuffer(input)
r, _ := gzip.NewReader(buf) r, _ := gzip.NewReader(buf)
defer r.Close() defer r.Close()
output, err := ioutil.ReadAll(r) output, err := ioutil.ReadAll(r)
if err!=nil { if err != nil {
println("error uncompressing data:", err) println("error uncompressing data:", err)
} }
return output return output
} }